【问题标题】:Listen to image load in Add-On SDK在 Add-On SDK 中监听图像加载
【发布时间】:2015-09-07 16:12:22
【问题描述】:

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

让用户加载一个新的 URL 可以通过 Page-Mod module 找到,AJAX 调用通过 overriding XMLHttpRequest.prototype.*()

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

(可能可以使用http-on-modify-request,指向here,但是如何访问nsIHttpChannel 的URL 和参数?)

【问题讨论】:

    标签: 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 扩展了 nsIChannelURI Attribute 类型为 nsIURIspec 属性包含整个 URL(包括架构、参数、引用等)。

    【讨论】:

      猜你喜欢
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 2018-10-28
      • 2016-08-07
      • 1970-01-01
      • 2014-08-31
      相关资源
      最近更新 更多