2
votes

with my extension i want to "block" some websites url and redirect to one url that i want whenever a user tries to join the blocked url.

Lets say that i want to "ban" facebook.com and if someone tries to go to facebook.com ... firefox addon should redirect him to www.google.com.

i have this code here

===============================================================================

const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import('resource://gre/modules/Services.jsm');
var urls_block = [ //if urls ontain any of these elements they will be blocked or redirected, your choice based on code in observer line 17
 'www.facebook.com'
];
var redir_obj = {
 'www.google.com': 'data:text,' + escape('url_blocked that would have went to google')
}
var observers = {
    'http-on-modify-request': {
        observe: function (aSubject, aTopic, aData) {
            console.info('http-on-modify-request: aSubject = ' + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData);
            var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
            var requestUrl = httpChannel.URI.spec.toLowerCase();
            for (var i=0; i<urls_block.length; i++) {
             if (requestUrl.indexOf(urls_block[i]) > -1) {
              //httpChannel.cancel(Cr.NS_BINDING_ABORTED); //this aborts the load
              httpChannel.redirectTo(Services.io.newURI(redir_obj[urls_block[i]], null, null)); //can redirect with this line, if dont want to redirect and just block, then uncomment this line and comment out line above (line 17)
              break;
             }
            }
        },
        reg: function () {
            Services.obs.addObserver(observers['http-on-modify-request'], 'http-on-modify-request', false);
        },
        unreg: function () {
            Services.obs.removeObserver(observers['http-on-modify-request'], 'http-on-modify-request');
        }
    }
};
function install() {}
function uninstall() {}
function startup() {
 for (var o in observers) {
         observers[o].reg();
 }
}

function shutdown(aData, aReason) {
 if (aReason == APP_SHUTDOWN) return;

 for (var o in observers) {
         observers[o].unreg();
 }
}

==================================================================

but i dont know how to make it work or even if its the correct code to redirect "facebook" to "google"

Could someone help me with this to show me how can i pack it as a firefox extension???

Thanks a lot for your time reading.

1
I shared this solution, smae question here: stackoverflow.com/a/30928671/1828637 and the exact solution can be found in the linked topic: stackoverflow.com/questions/25327282/… althought I think the best way to do this is to use content policy, i havent worked with it much though but i hope to see a solution one day that uses that (i actually think there was one the other week ill look for it) - Noitidart
oh i see you have that code from there, no your code is wrong, change the key in the redir_obj from www.google.com to www.facebook.com. To test it, paste all that code into scratchpad, and add startup() at the bottom of it and run it. here is how to run it (with environment set to browser) in scratchpad: youtube.com/watch?v=oo4STWceGTM also to run it in scratchpad comment out the first line of const ...... this code is meant to be copied and pasted to bootstrap.js - Noitidart
@Noitidart perfect its blocking google.com BUT i am asking to block facebook and redirect to google.com ! Do you have any idea? - user4544229
Yes @Stelio what that paragraph means is repalce 'www.google.com': 'data:text,' + escape('url_blocked that would have went to google') with 'www.facebook.com': 'http://www.google.com/' - Noitidart
@Noitidart could you check this guestion here? because i get an error and i cant see if its working now, or no. stackoverflow.com/questions/31044636/… - user4544229

1 Answers