Skip to content Skip to sidebar Skip to footer

How Can I Make A Greasemonkey Script To Auto-download A File?

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? S

Solution 1:

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:

<ahref="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.

Solution 2:

This is a code that i have used in greasmonkey to download a zip file from a location provided by url at the @include statement.

// ==UserScript==    // @name        zipexport    // @namespace   refresh page    // @include     https://control.com/export.php    // @version     1    // @grant       none    // ==/UserScript==    var timerVar= setInterval(function() {DoMeEverySecond (); }, 60000);    

functionDoMeEverySecond ()    
{    
  setInterval('window.location.reload()',10000);    

 $(document).ready(function()    
{    

 setTimeout(function(){    

document.getElementsByClassName("btn btn-lg btn-primary")[0].click();
}, 1000);});    


}    

To get some idea, please go through this..

// @includehttps://control.com/export.php 

Use the link of the source page here

setInterval(function() {DoMeEverySecond (); }, 60000);

Helps you to invoke the function DoMeEverySecond (); after 60000ms(60s=1min)

setInterval('window.location.reload()',10000);

Used to reload the page every 10s. It is used by me just to ensure the web page is updated to the latest state(i had a file to download which was updated every hour). You can avoid if this is not needed for you.

$(document).ready(function()

function() will be only called after fully reloading the webpage if we use this statement.

document.getElementsByClassName("btn btn-lg btn-primary")[0].click();

getElementsByClassName/getElementsById etc can be used here based on what can point to the file you wanted to download (Use inspect element by rightclicking in the source page to know if which one of the class/id can point to your zip file).

[0] may help you if you have more than one variable to call under same class.

click()

performs a mouse click in the specified element.(this should help to download the file)

Post a Comment for "How Can I Make A Greasemonkey Script To Auto-download A File?"