+1

[FIX] Download Name in Chrome

Riky X 2 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:

/js/contentScripts/contentScript.js

Line 41:


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

/js/background.js

Line 105:


            else if (request.action == "DownloadItem") {
                chrome.downloads.download({
                    url: request.media.url,
                    filename: request.media.downloadName
                });
            }