Intercepting New Downloads in the Firefox Addon SDK

I wrote a simple download manager for Windows, and I would like to create an addon for Firefox, which, when enabled, captures new downloads in Firefox and sends them to the download manager.

I already did this for Google Chrome using:

chrome.downloads.onCreated.addListener(function(details) {
    // stop the download
    chrome.downloads.cancel(details.id, null);
}

The question is, how can I achieve something like this using the Firefox SDK add-in .

I see that there is a way to intercept page loads to view content / headers that may be useful, but then I won’t know if the request will turn into a load or not.

Firefox add-on SDK: get HTTP response headers

I could perhaps look for a type of content that is not text / html, or check the content placement header, but this can cause problems if I handle all cases incorrectly.

Is there a way to access the boot manager using the JS SDK or somehow find out when the boot was started / started and stopped?

+4
source share
2 answers

An observer http-on-examine-responsewho discusses a related issue is the wrong way. This applies to all requests, not just downloads.

Instead, use Downloads.jsmto watch for new downloads, then cancel them, etc.

To download Downloads.jsmto the SDK, use:

const {Cu} = require("chrome");
Cu.import("resource://gre/modules/Downloads.jsm");
Cu.import("resource://gre/modules/Task.jsm");

.

let view = {
  onDownloadAdded: function(download) { 
    console.log("Added", download);
  },
  onDownloadChanged: function(download) {
    console.log("Changed", download);
  },
  onDownloadRemoved: function(download) {
    console.log("Removed", download);
  }
};

Task.spawn(function() {
  try {
    let list = yield Downloads.getList(Downloads.ALL);
    yield list.addView(view);
  } catch (ex) {
    console.error(ex);
  }
});

MDN .

SDK, , .removeView , .

+5

JSM.

Components.utils.import("resource://gre/modules/Downloads.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");

var view = {
    onDownloadChanged: function (download) {
        console.log(download, 'Changed');
        if (download.succeeded) {
            var file = new FileUtils.File(this.target.path);
            console.log('file', file);
        }
    }
};

var list;
Task.spawn(function () {
    list = yield Downloads.getList(Downloads.ALL);
    list.addView(view);
}).then(null, Components.utils.reportError);

removeView, . , , shutdown - , Task.spawn, list var.

list.removeView(view); //to stop listening

, , , . , , DownloadManager:

var observerService = Components.classes["@mozilla.org/download-manager;1"].getService(Components.interfaces.nsIDownloadManager);

observerService.addListener({
    onDownloadStateChange: function (state, dl) {
        console.log('dl=', dl);
        console.log('state=', state);
        console.log('targetFile', dl.targetFile);
        if (state == 7 && dl.targetFile.leafName.substr(-4) == ".txt") {
            //guys just downloaded (succesfully) a .txt file
        }
    }
});

: http://forums.mozillazine.org/viewtopic.php?f=19&t=2792021

+2

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


All Articles