我编写了一个基于 Mozilla Jetpack 的插件,它必须在加载文档时运行。对于“顶级文档”,这主要使用以下代码(OserverService = require('observer-service')
):
this.endDocumentLoadCallback = function (subject, data) {
console.log('loaded: '+subject.location);
try {
server.onEndDocumentLoad(subject);
}
catch (e) {
console.error(formatTraceback(e));
}
};
ObserverService.add("EndDocumentLoad", this.endDocumentLoadCallback);
但是,当用户使用中键或(更重要的是!)为框架打开新选项卡时,不会调用回调。即使是这个话题,我也只是通过阅读另一个扩展的源代码而不是通过文档获得的。
那么如何注册一个真正在每次加载文档时调用的回调呢?
编辑:这似乎做我想要的:
function callback (event) {
// this is the content document of the loaded page.
var doc = event.originalTarget;
if (doc instanceof Ci.nsIDOMNSHTMLDocument) {
// is this an inner frame?
if (doc.defaultView.frameElement) {
// Frame within a tab was loaded.
console.log('!!! loaded frame:',doc.location.href);
}
else {
console.log('!!! loaded top level document:',doc.location.href);
}
}
}
var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
var mainWindow = wm.getMostRecentWindow("navigator:browser");
mainWindow.gBrowser.addEventListener("load", callback, true);
部分从这里得到:https ://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads