2

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: http://stackoverflow.com/a/30928671/1828637 and the exact solution can be found in the linked topic: http://stackoverflow.com/questions/25327282/why-is-this-javascript-page-redirect-so-slow/25328750#comment49957376_25328750 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 Jun 25 '15 at 06:08
  • 1
    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: https://www.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 Jun 25 '15 at 06:10
  • @Noitidart perfect its blocking google.com BUT i am asking to block facebook and redirect to google.com ! Do you have any idea? –  Jun 25 '15 at 07:33
  • 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 Jun 25 '15 at 09:34
  • @Noitidart could you check this guestion here? because i get an error and i cant see if its working now, or no. http://stackoverflow.com/questions/31044636/firefox-addon-error-in-scratchpad/ –  Jun 25 '15 at 09:46
  • @Noitidart can you comment it now so i can approve your answer?? you are great man!!! its working 100% –  Jun 25 '15 at 10:39
  • Cool man. Yeah its a bit tricky to learn at start but once you see things working you get the hang of things. – Noitidart Jun 25 '15 at 11:02

1 Answers1

0

Video demonstrating its working posted here: Firefox addon error in scratchpad

related topics: https://stackoverflow.com/a/30928671/1828637

Why is this JavaScript page redirect so slow?

Community
  • 1
  • 1
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • http://stackoverflow.com/questions/31049003/how-to-pack-a-firefox-extension-from-scratch if you have time and you can check this out. –  Jun 25 '15 at 18:07