Firefox SDK |阻止图像加载w.r.t.域

时间:2014-04-28 12:56:54

标签: javascript firefox-addon-sdk

我即将成为Firefox插件的一个功能是阻止基于这两个标准的图像加载:

  1. IMG来自特定(子)域(例如,* .example.com);
  2. IMG的网址或路径包含特定字词(RegEx);
  3. 我在基于WebRequest的Google Chrome扩展程序中开发了此功能(请参阅下面的代码)。

    background.js [Chrome扩展程序]

    var regex = '/example|example2/gi';
    
    // -- Block request
    function blockRequest(details) {
        if( details.type === 'image' && details.url.split('://').pop().match(regex) ) {
            return {cancel: true};
        }
    }
    // -- Apply blocking
    function applyBlocking(type) {
        if(chrome.webRequest.onBeforeRequest.hasListener(type))
            chrome.webRequest.onBeforeRequest.removeListener(type);
            chrome.webRequest.onBeforeRequest.addListener(type, {
                urls: [
                    'https://*.example.com/proxy/*'
                ]}, ['blocking']);
    }
    
    // Block
    function blockTracking(a) {
        if(a) {
            applyBlocking(blockRequest);
        }
    }
    

    我现在正在尝试在Firefox中复制它,基于SDK。基于this postthis one,我似乎必须注册观察员并使用XPCOM。但是,我在查找有关如何使用Firefox API访问所请求的文件类型和URL的更多文档时遇到了一些困难。任何帮助表示赞赏...

    main.js [Firefox扩展]

    var { Cc, Ci } = require('chrome'),
             regex = '/example|example2/gi';
    
    var ApplyBlocking = {
    
        observe: function(subject, topic) {
            if (topic === 'http-on-modify-request') {
    
            /* ??? */
    
            }
        },
        get observerService() {
            return Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService);
        },
    
        register: function() {
            this.observerService.addObserver(this, 'http-on-modify-request', false);
        },
    
        unregister: function() {
            this.observerService.removeObserver(this, 'http-on-modify-request');
        }
    };
    
    // Block
    function blockTracking(a) {
        if(a) {
            ApplyBlocking.register();
        }
    }
    

1 个答案:

答案 0 :(得分:3)

我最终实施的内容,以防其他人感兴趣...

var { Cc, Ci, Cr } = require('chrome'),
         regex = '/example|example2/gi';

var ApplyBlocking = {

    observe: function(subject, topic) {
        if (topic === 'http-on-modify-request') {

           var channel = subject.QueryInterface(Ci.nsIHttpChannel);

           if ( channel.originalURI.spec.match('example.com/') && channel.originalURI.spec.split('://').pop().match(regex) ) {
               channel.cancel(Cr.NS_BINDING_ABORTED);
           }

        }
    },
    get observerService() {
        return Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService);
    },

    register: function() {
        this.observerService.addObserver(this, 'http-on-modify-request', false);
    },

    unregister: function() {
        this.observerService.removeObserver(this, 'http-on-modify-request');
    }
};

// Block
function blockTracking(a) {
    if(a) {
        try { ApplyBlocking.register(); } catch(e) {}
    } else {
       try { ApplyBlocking.unregister(); } catch(e) {}
    }
}