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

荒凉一梦 提交于 2019-11-29 04:52:26
Brock Adams

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:

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.

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);    

function DoMeEverySecond ()    
{    
  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..

// @include     https://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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!