문제

One functionality of my soon-to-be Firefox addon will be to block the loading of images based on those 2 criteria:

  1. IMG comes from a specific (sub)domain (e.g., *.example.com);
  2. url or path of the IMG contains specific words (RegEx);

I have developed this feature in a Google Chrome extension resting on WebRequest (see code below).

background.js [Chrome extension]

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);
    }
}

I'm trying now to replicate this in Firefox, resting on the SDK. Based on this post and this one, it seems that I will have to register an observer and use XPCOM. However, I have some difficulties to find more documentation on how I can get access to the types of file and urls requested using the Firefox API. Any help appreciated...

main.js [Firefox extension]

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();
    }
}
도움이 되었습니까?

해결책

What I finally implemented, in case it can be of some interest to other people...

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) {}
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top