问题
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.
- To intercepts download links for a download manager, is writing addons the best choice?
- 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