Search code examples
javascripttampermonkeyuserscripts

How make userscript to not download already existing file


I use tampermonkey on mobile firefox. In userscript I use GM_download(url, name) to download file from url. When it start download same file in firefox the result of this is :

Hello.txt

Hello(1).txt

...

Hello(9).txt

I want to know how not download duplicates. Or way to block firefox download duplicate.

Or get directory list. If there file alredy exist dont download. In bash(linux cml) you write ls ,in windows you write dir. But how it would be in userscript? If yo write file:///storage/emulated/0/Download/Hello.txt in your browser it show all files that you want (in this address is Hello.txt file). But I can't get this html or whatever is this via XMLHttpRequest

Ps: Sorry for my english


Solution

  • You cannot access the filesystem to check if some file exists. That would be pretty dangerous for userscripts - imagine you installed userscript that did that. Firefox doesn't even allow plugins to access filesystem AFAIK, so tampermonkey cannot provide that API.

    What you can do is to remember the name of the downloaded file. Your options are:

    • localStorage - for example set localStorage[filename]=true and then check for that. But if you delete the file on your phone, this will prevent you from downloading it again
    • indexedDB this is nicer for saving data, but the problem is same as for local storage.