Firefox SDK | Lock image upload wrt Domain

One feature of my upcoming Firefox addon will be blocking image downloads based on these two criteria:

  • IMG comes from a specific (additional) domain (for example, * .example.com);
  • URL or IMG path contains specific words (RegEx);

I developed this feature in the Google Chrome extension based 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);
    }
}

Firefox, SDK. , , XPCOM. , , URL-, API- Firefox. ...

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();
    }
}
+4
1

, , , , ...

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) {}
    }
}
+3

Source: https://habr.com/ru/post/1538402/


All Articles