如何制作 Greasemonkey 脚本来自动下载文件? [英] How can I make a Greasemonkey script to auto-download a file?

查看:55
本文介绍了如何制作 Greasemonkey 脚本来自动下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转到页面,它有 1 个 zip 文件,但我不知道名称,除了它的 .zip.

我想让 Greasemonkey 自动下载这个 zip,也许使用 flashgot 之类的?

所以我需要它在页面加载时激活,然后查找 *.zip,并将其添加到自动下载中.

有什么想法吗?

解决方案

Greasemonkey 本身无法自动将 zip 文件或其他任何内容保存到本地文件系统.
这是设计使然;允许用户/页面 JavaScript 保存文件已被证明是一种安全灾难.

您的选择:

  1. 让 Greasemonkey 选择正确的链接并打开文件-保存"对话框(节省搜索工作和 1 次点击).
  2. 让 GM 将 zip 文件转发到您自己的服务器.然后,您的服务器应用程序可以自动保存文件.
    请注意,服务器"可能是您自己的机器,运行类似 将文件发送到您的网络应用程序 -- 你必须写.然后,Web 应用程序可以自动将文件保存到服务器.您可以将本地机器设置为服务器.

    有关此方法的更多帮助,请阅读此内容,然后询问新的问题.


    选项 3,编写您自己的 FF 扩展(附加):

    如果您决定采用 Firefox 附加组件路线,请参阅 "MDN:下载文件".

    有关此方法的更多帮助,请阅读此内容,然后询问新的问题.

    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?

    解决方案

    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.

    这篇关于如何制作 Greasemonkey 脚本来自动下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆