【问题标题】:Listen to image load in Add-On SDK在 Add-On SDK 中监听图像加载
【发布时间】:2015-09-07 16:12:22
【问题描述】:
【问题讨论】:
标签:
javascript
html
firefox-addon-sdk
【解决方案1】:
您可以通过以下方式监听每个网络请求
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();
};
另见Firefox Addon observer http-on-modify-request not working properly。
URI 访问
nsIHttpChannel 扩展了 nsIChannel,URI Attribute 类型为 nsIURI,spec 属性包含整个 URL(包括架构、参数、引用等)。