Firefox addon error in scratchpad

723 views Asked by At

I am trying to test this code here. This code "blocks" some URL if I try to join them.

//const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import('resource://gre/modules/Services.jsm');
var urls_block = [ 
    //If URLs contain 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.facebook.com': 'data:text,'+ escape('url_blocked would have went to facebook')
}
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
                    //Can redirect with this next line, if don't want to redirect and
                    //  just block, then comment this line and uncomment the line above:
                    httpChannel.redirectTo(Services.io.newURI(redir_obj[urls_block[i]],
                                               null, null));
                    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();
    }
}

startup()

In scratchpad of Firefox and I get this error:

/*
Exception: ReferenceError: Cu is not defined
@Scratchpad/1:2:1
*/

NO ERROR ON CONSOLE.

Does anyone have any idea what is this error??
I've read that Firefox doesn't go well with constants but this code sometimes works sometimes not.

Also can someone help me fixing it, so it can work all the time?

3

There are 3 answers

2
Noitidart On BEST ANSWER

The code works as is except for shutdown but thats obvious because its from scratchpad scope. This code is designed ot run form bootstrap scope. But to make it run form scratchpad you comment out first line and just run startup. then to shutdown just run the loop from within shutdown as APP_SHUTDOWN is not defined. <<< for scrathcpad scope, which is for testing purposes only. Once you want to deploy it use the uncommented line 1 code and no need to run startup or shutodwn as they are bootstrap entry and exit functions so they automatically get called. See the youtube video:

https://www.youtube.com/watch?v=pxt8lJ64nVw

5
Makyen On

There are multiple problems. First, in order for Components to be defined, you need to have the scratchpad running in the browser context. To do this go to the Developer Tool Settings and check the box that is "Enable chrome and add-on debugging". Restart Firefox, just to be sure that the scratchpad is in the browser context.

Once that is done, the following code will work:

// -sp-context: browser
//The above line tells scratchpad to run this in the browser context.
//In the scratchpad browser context some of these are already defined, so we check first:
if(typeof Cc === "undefined") {
    const Cc = Components.classes;
}
if(typeof Ci === "undefined") {
    const Ci = Components.interfaces;
}
if(typeof Cu === "undefined") {
    const Cu = Components.utils;
}
if(typeof Cr === "undefined") {
    const Cr = Components.results;
}
//In your own add-on you will need to define them all:
//const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import('resource://gre/modules/Services.jsm');
var urlsBlock = [ 
    //If URLs contain any of these elements they will be blocked or redirected,
    //  your choice based on code in observer line 17
    'www.facebook.com'
];
var redirObj = {
    'www.facebook.com': 'data:text,'+ escape('url_blocked would have went to facebook')
}
var observers = {
    'http-on-modify-request': {
        observe: function (aSubject, aTopic, aData) {
            var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
            var requestUrl = httpChannel.URI.spec.toLowerCase();
            console.info('http-on-modify-request: aSubject = ' 
                          + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData
                          + ' | httpChannel = ' + httpChannel 
                          + ' | requestUrl = ' + requestUrl);
            for (let urlsBlockLength=urlsBlock.length, i=0; i<urlsBlockLength; i++) {
                if (requestUrl.indexOf(urlsBlock[i]) > -1) {
                    //httpChannel.cancel(Cr.NS_BINDING_ABORTED); //this aborts the load
                    //Can redirect with this next line, if don't want to redirect and
                    //  just block, then comment this line and uncomment the line above:
                    httpChannel.redirectTo(Services.io.newURI(redirObj[urlsBlock[i]],
                                               null, null));
                    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();
    }
}

startup();
0
Hosein Aqajani On

I had this problem, It is so easy, it is enough to change your environment of scratchpad from Content to Browser