Intercept download link click and prevent default download dialog

这一生的挚爱 提交于 2019-12-25 02:59:24

问题


We have built a download manager desktop application for windows. Now we want to add a feature that intercepts download links and adds them to the application. We think we should write an addon for each browser starting from Firefox.

  1. To intercepts download links for a download manager, is writing addons the best choice?
  2. How can we do that?

Things we've tried so far:
- Using Downloads.jsm to observe new downloads, then cancel them => We don't want the user to interact with Firefox's download dialog
https://stackoverflow.com/a/24466197/2550529
- Adding a click event listener to each tab and looking for links => Download links are not distinguishable.
https://stackoverflow.com/a/10345358/2550529
After grabbing the link, it is just passed to our application using nsIProcess.

In one sentence: We want it to behave like IDM's new download dialog.


回答1:


Here's what we've done as of yet. It works as expected.

const {components, Cc, Ci} = require("chrome");
httpRequestObserver =
{
    observe : function(aSubject, aTopic, aData) {
        if (aTopic == "http-on-modify-request") {
            let url;

            aSubject.QueryInterface(Ci.nsIHttpChannel);
            url = aSubject.URI.spec;

            if(dlExtensions == null)
                return;

            var match = false;
            for(x in dlExtensions)
                if(url.endsWith(dlExtensions[x]))
                {
                    match = true;
                    break;
                }
            if(match == true) {
                aSubject.cancel(components.results.NS_BINDING_ABORTED);
                //Pass url to exe file
            }
        }
    }
};

var observerService = components.classes["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
observerService.addObserver(httpRequestObserver, "http-on-modify-request", false);


来源:https://stackoverflow.com/questions/30141782/intercept-download-link-click-and-prevent-default-download-dialog

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