Search code examples
greasemonkeyuserscriptstampermonkey

Tampermonkey & download dialogs


After reading through the documentation, I figure not, but better safe than sorry :

Can user scripts interact with a download dialog, changing the file name? The file name is not known before clicking the download link so it's not simply a matter of finding foo.bar.zip on the page and replacing foo.bar by the new name.


Solution

  • Yes, you can explicitly specify file name via the download attribute:

    <a href="index.html" download="test.html">Download index page</a>
    

    However there are caveats:

    • If server sends file name in the Content-Disposition header, that header is prefered over what you specified
    • This only works on links within the same domain and protocol

    If you're on the same domain, the Content-Disposition header can be erased if you download the file via XMLHttpRequest and then use createObjectURL and have the user download that. Ask yourself if it's worth the effort.