Search code examples
google-chrome-extension

Specifying path/filename in downloads.download() api not working in Chrome but does in Firefox


Specifying path/filename to downloading file does not work in chrome.downloads api but does work for firefox browser.downloads api.

I have a very simple call in popup.js for chrome and firefox:

chrome.downloads.download({url: address, filename: path + "/" + filename, saveAs: false});

Here's another one for Firefox:

browser.downloads.download({url: addresss, filename: path + "/" + filename, saveAs: false});

It works perfectly as expected in firefox, but no matter what, I can't ever get it to work in chrome. Even with a simple download of google's image to a different file name never works chrome but does in firefox, such as:

chrome.downloads.download({url: "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", filname: "temp/temp.png"});

What am I doing wrong?


Solution

  • It's been awhile but I'll post my solution for whoever comes across this issue for their extension. It turns out that specifying a custom filename isn't working is because there are extension conflicts.

    If there is another extension using the chrome.downloads API, specifically the chrome.downloads.onDeterminingFilename.addListener, which tries to specify the filename that overrides the original filename suggestion. This also happens even if the conflicting extension did not change your download options but just added a listener for onDeterminingFilename without doing any logic. This is a known chromium issue here: https://bugs.chromium.org/p/chromium/issues/detail?id=579563. That issue has been opened since 2016 and the devs still have not addressed it.

    Workaround solution: implement your own chrome.downloads.onDeterminingFilename.addListener to suggest your custom filename. Make sure that you're only capturing downloads that you started yourself but comparing the download item's id and extension id. The only caveat is that the conflicting extension will produce an error saying they could not suggest their own filename.

    Firefox does not have this issue because their extension API does not have onDeterminingFilename.