javascript 如何制作 Greasemonkey 脚本来自动下载文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8273337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a Greasemonkey script to auto-download a file?
提问by Dan Toner
I go to the page, it has 1 zip file, but I don't know the name, other than its a .zip
.
我转到该页面,它有 1 个 zip 文件,但我不知道名称,除了它的 .zip 文件.zip
。
I want Greasemonkey to download this zip automatically, maybe using flashgot or something?
我想让 Greasemonkey 自动下载这个 zip,也许使用 flashgot 或其他什么?
So I need it to activate on page load, then look for *.zip
, and add that to downloads automatically.
所以我需要它在页面加载时激活,然后查找*.zip
,并将其添加到自动下载中。
Any ideas?
有任何想法吗?
回答by 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.
Greasemonkey 本身无法自动将 zip 文件或其他任何内容保存到本地文件系统。
这是设计使然;允许用户/页面 JavaScript 保存文件已被证明是一种安全灾难。
Your options:
您的选择:
- Have Greasemonkey select the right link and open the File-Save dialog (saving you the search effort and 1 click).
- 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. - Write your own Firefox Add-on.
- 让 Greasemonkey 选择正确的链接并打开 File-Save 对话框(节省您的搜索工作和 1 单击)。
- 让 GM 将 zip 文件中继到您自己的服务器。然后您的服务器应用程序可以自动保存该文件。
请注意,“服务器”可能是您自己的运行XAMPP 之类的机器。 - 编写您自己的 Firefox Add-on。
Option 1, GM only:
选项 1,仅 GM:
What GM cando is pop open the File-Save dialog for the correct file:
GM可以做的是弹出正确文件的文件保存对话框:
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:
选项 2,GM 和您自己的服务器应用程序:
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.
Greasemonkey 可以使用GM_xmlhttpRequest()将文件发送到您的 Web 应用程序——您必须编写该应用程序。然后,Web 应用程序可以自动将文件保存到服务器。您可以将本地计算机设置为服务器。
For more help on this approach, read thisand then ask a new question.
有关此方法的更多帮助,请阅读此内容,然后提出一个新问题。
Option 3, write your own FF extension (add-on):
选项 3,编写自己的 FF 扩展(附加):
If you decide to take the Firefox add-on route, see "MDN: Downloading files".
如果您决定采用 Firefox 附加组件路线,请参阅“MDN:下载文件”。
For more help on this approach, read thisand then ask a new question.
有关此方法的更多帮助,请阅读此内容,然后提出一个新问题。
回答by Nithin R Krishna
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.
这是我在 greasmonkey 中使用的代码,用于从 @include 语句中的 url 提供的位置下载 zip 文件。
// ==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)
帮助您调用函数 DoMeEverySecond(); 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.
用于每 10 秒重新加载页面。我使用它只是为了确保网页更新到最新状态(我有一个文件要下载,每小时更新一次)。如果您不需要,则可以避免。
$(document).ready(function()
function() will be only called after fully reloading the webpage if we use this statement.
如果我们使用此语句,则只有在完全重新加载网页后才会调用 function()。
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).
getElementsByClassName/getElementsById 等可以在此处使用,具体取决于可以指向您要下载的文件的内容(通过在源页面中右键单击来使用检查元素以了解哪个类/id 可以指向您的 zip 文件)。
[0] may help you if you have more than one variable to call under same class.
[0] 如果您在同一类下有多个变量要调用,则可能会对您有所帮助。
click()
performs a mouse click in the specified element.(this should help to download the file)
在指定的元素中执行鼠标单击。(这应该有助于下载文件)