[FIX] Download Name in Chrome

Riky X 3 years ago 0

Currently there is a bug in the extension, the downloaded filename is always the original filename and not the page title. The download anchor in the extension does not save the file with the provided download attribute.. this could be a Chrome bug or the filename getting overrided by the Content-Disposition header sent by the server in some cases.

I've found a fix for this issue.

In /js/contentScripts/contentScript.js instead to create an anchor element and fire the click event, send a message to the parent extension with media as parameter.

In /js/background.js, listen to this message, and access the chrome Download API to download the file.
Using the Download API directly, the file is saved with the provided filename.

Here is my code:


Line 41:

    function downloadMediaItem( media ){
        chrome.extension.sendMessage({action:"DownloadItem", media: media  });


Line 105:

            else if (request.action == "DownloadItem") {
                    url: request.media.url,
                    filename: request.media.downloadName