xpcom/jetpack 观察所有文档加载
我编写了一个基于 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
I write a Mozilla Jetpack based add-on that has to run whenever a document is loaded. For "toplevel documents" this mostly works using this code (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);
But the callback doesn't get called when the user opens a new tab using middle click or (more importantly!) for frames. And even this topic I only got through reading the source of another extension and not through the documentation.
So how do I register a callback that really gets called every time a document is loaded?
Edit: This seems to do what I want:
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);
Got it partially from here: https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@kizzx2 你最好使用 #jetpack
对于最初的问题:为什么不使用选项卡浏览器模块。像这样的事情:
比你所做的更干净恕我直言,并且(直到我们有官方 pageMods 模块)受支持的方式来做到这一点。
@kizzx2 you are better served with #jetpack
To the original question: why don't you use tab-browser module. Something like this:
Much cleaner than what you do IMHO and (until we have official pageMods module) the supported way how to do this.
从 Addon SDK 1.0 开始,执行此操作的正确方法是使用 page-mod 模块。
(在底层,它是使用 document-element-inserted 观察者服务通知实现的,您可以在常规扩展中使用它,或者如果 page-mod 不适合您。)
As of Addon SDK 1.0, the proper way to do this is to use the page-mod module.
(Under the hood it's implemented using the document-element-inserted observer service notification, you can use it in a regular extension or if page-mod doesn't suit you.)