How can I make a greasemonkey script to automatically download a file?

I go to the page, I have 1 zip file, but I do not know it except for its .zip .

I want Greasemonkey to download this ZIP file automatically, perhaps using flashgot or something like that?

So I need to activate page loading, then find *.zip and add it to load automatically.

Any ideas?

+4
source share
2 answers

Greasemonkey alone cannot automatically save zip files or anything else on the local file system.
It is by design; allowing a user / JavaScript page to save files is a proven security risk.

Your options:

  • At Greasemonkey, select the correct link and open the "Save File" dialog box (save the search efforts and click "1 click").
  • Have GM relay the zip file to your own server . Then your server application will automatically save the file.
    Please note that the โ€œserverโ€ may be your own machine running something like XAMPP .
  • Write your own Firefox add-on .


Option 1, GM only:

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

Windows File-Save dialog

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

For example, suppose a 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 >. After that, the web application will automatically save the file to the server. You can configure the local computer on 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 use the optional Firefox route, see MDN: Download Files .

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

+11
source

This is the code I used in greasmonkey to download the zip file from the location specified by url in 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 understand, please go through this.

 // @include https://control.com/export.php 

Use the link to the source page here

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

Helps you call the DoMeEverySecond () function; after 60,000 ms (60 s = 1 min)

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

Used to reload the page every 10 seconds. It is used by me only to ensure that the web page is updated to the last state (I had a download file that was updated every hour). You can avoid it if you do not need it.

 $(document).ready(function() 

function () is only called after a full reload of the web page, 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 want to download (use the check item by right-clicking on the source page to find out if any of the / id class can point to your zip file)

[0] can help you if you have several variables to call in the same class.

 click() 

clicks on the specified element (this should help to download the file)

0
source

Source: https://habr.com/ru/post/1383116/


All Articles