在 Add-On SDK 中监听图像加载

Listen to image load in Add-On SDK

是否可以通过 Mozilla Add-On SDK 监听图像(或样式表)的加载?

让用户加载新的 URL 可以通过 Page-Mod module, AJAX calls via 找到。

但两者都只听加载全新页面,不听页面的附加图像。此外,图像源可能会更改,例如 Javascript。

(也许可以使用 http-on-modify-request, as pointed to here, but how can you access the nsIHttpChannel 的 URL 和参数?)

您可以通过

监听每个网络请求
var {Cc, Ci} = require("chrome");
var httpRequestObserver = {
    observe: function(subject, topic, data) {
        if (topic == "http-on-modify-request") {
            var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
            var myURL = httpChannel.URI.spec;
            console.log("url: " + myURL);
        }
    },

    register: function() {
        var observerService = Cc["@mozilla.org/observer-service;1"]
            .getService(Ci.nsIObserverService);
        observerService.addObserver(this, "http-on-modify-request", false);
    },

    unregister: function() {
        var observerService = Cc["@mozilla.org/observer-service;1"]
            .getService(Ci.nsIObserverService);
        observerService.removeObserver(this, "http-on-modify-request");
    }
};

httpRequestObserver.register();

exports.onUnload = function(reason) {
    httpRequestObserver.unregister();
};

另见 。

URI 访问

nsIHttpChannel 扩展了 nsIChannel, which has a URI Attribute of type nsIURI,它有一个 spec 属性,其中包含整个 URL(包括架构、参数、引用等)。