Firefox扩展的“加载事件”之前的事件? [英] Event before 'load event' for Firefox extension?

查看:134
本文介绍了Firefox扩展的“加载事件”之前的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Firefox扩展名,

I'm writing a Firefox extension,

这是我的XUL(没有问题)

This is my XUL (no problem there)

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE overlay SYSTEM "chrome://locale/myDtd.dtd">
<page id="overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
         xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <script type="application/x-javascript" src="chrome://addon/content/test.js" />
</page>

这里是javascript test.js的有问题的部分

And here is the problematic part of the javascript test.js

window.addEventListener("load",
    function(event) {
        var appcontent = window.document.getElementById("appcontent");
        appcontent.addEventListener("load",onEventLoad,true);
    }, true);

appcontent上的第二个加载侦听器对我的需求来说太慢了。
当DOM完成加载时,'load'事件触发。

The second 'load' listener on appcontent is too slow for my needs. The 'load' event is triggered when the DOM is done loading.

我的问题:有没有人知道如何一旦文档开始加载(在DOM的加载事件之前)就可以运行代码? (希望有一个onBeforeLoad或onRequestStart事件存在)

My question: Does anyone have an idea of how to run code as soon as a document starts loading (before the load event of the DOM) ? (wish a onBeforeLoad or onRequestStart event existed)

在Chrome扩展中,我们可以在'manifest.json'中使用run_at:document_start,
和在Safari扩展中,我们可以在扩展构建器
中使用启动脚本,但在Firefox中...我不知道如何做同样的技巧。

In Chrome extensions, we can use "run_at": "document_start" in 'manifest.json', and in Safari extensions, we can use 'Starting script' in extension builder but in Firefox ... i don't know how to do the same trick.

我需要这样才能在DOM到达时立即查看DOM中的元素(但这是另一个故事)。

I need this to start looking at elements in the DOM as soon as they arrive (but that's another story).

感谢任何帮助,谢谢大家! p>

I appreciate any help, thank you all !

推荐答案

这里有一个很好的解释:

There's a good explanation here:

http://blog.webmynd.com/2011/04/ 04 /等效于beforeload-event-for-firefox-extensions /

建议的解决方案依赖于Firefox Observers和http-on-modify-request :

The suggested solution relies on Firefox Observers and http-on-modify-request:

Components.classes["@mozilla.org/observer-service;1"]
  .getService(Components.interfaces.nsIObserverService)
  .addObserver({
    observe: function(aSubject, aTopic, aData) {
      if ("http-on-modify-request" == aTopic) {
        var url = aSubject
          .QueryInterface(Components.interfaces.nsIHttpChannel)
          .originalURI.spec;
        if (url && url.match('facebook')) {
          aSubject.cancel(Components.results.NS_BINDING_SUCCEEDED);
        }
    }
  }
}, "http-on-modify-request", false);

这篇关于Firefox扩展的“加载事件”之前的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆