Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a Greasemonkey script to auto-download a file? [duplicate]

I go to the page, it has 1 zip file, but I don't know the name, other than its a .zip.

I want Greasemonkey to download this zip automatically, maybe using flashgot or something?

So I need it to activate on page load, then look for *.zip, and add that to downloads automatically.

Any ideas?

like image 439
Dan Toner Avatar asked Nov 25 '11 19:11

Dan Toner


People also ask

Do Greasemonkey scripts work in Tampermonkey?

Tampermonkey is used to run so-called userscripts (sometimes also called Greasemonkey scripts) on websites. Userscripts are small computer programs that change the layout of a page, add or remove new functionality and content, or automate actions.

What language does Greasemonkey use?

Technical details. Greasemonkey user scripts are written in JavaScript and manipulate the contents of a web page using the Document Object Model interface.

How do I read a script on Greasemonkey?

To access Greasemonkey's management pane, click on the downward arrow on the right side of the button Greasemonkey to the right of your URL bar and select Manage User Scripts. This opens a tab with a list of your scripts.


1 Answers

Greasemonkey, by itself, cannot automatically save zip-files, or anything else, to the local file system.
This is by design; allowing user/page JavaScript to save files is a proven security disaster.

Your options:

  1. Have Greasemonkey select the right link and open the File-Save dialog (saving you the search effort and 1 click).
  2. Have GM relay the zip file to your own server. Your server application can then automatically save the file.
    Note that the "server" could be your own machine running something like XAMPP.
  3. Write your own Firefox Add-on.


Option 1, GM only:

What GM can do is pop open the File-Save dialog for the correct file:

Windows, File-Save dialog

User interaction will still be required, if only one click.

For example, suppose the page contains this link:

<a href="http://Suspicious.com/TotallyOwnYourBankAndCreditCardAccounts.zip">
    Click me, sucka!
</a>

Then this code will open the File-Save dialog for it:

var clickEvent      = document.createEvent ('MouseEvents');
var firstZipFile    = document.querySelector ("a[href*='.zip']");

clickEvent.initEvent ('click', true, true);
firstZipFile.dispatchEvent (clickEvent);


Option 2, GM and your own server application:

Greasemonkey can use GM_xmlhttpRequest() to send files to your web application -- which you will have to write. The web app can then save the file to the server automatically. You can set up your local machine to be the server.

For more help on this approach, read this and then ask a new question.


Option 3, write your own FF extension (add-on):

If you decide to take the Firefox add-on route, see "MDN: Downloading files".

For more help on this approach, read this and then ask a new question.

like image 65
Brock Adams Avatar answered Nov 14 '22 23:11

Brock Adams