暂存器中的Firefox插件错误

时间:2015-06-25 08:07:11

标签: firefox firefox-addon firefox-developer-tools scratchpad

我正在尝试在此处测试此代码。这段代码"阻止"如果我尝试加入它们,可以使用一些URL。

//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()

在Firefox的暂存器中,我收到此错误:

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

在控制台上没有错误。

有没有人知道这个错误是什么?
我已经读过Firefox并不能很好地处理常量,但这段代码有时候工作不行。

也有人可以帮我修理它,所以它可以一直工作吗?

3 个答案:

答案 0 :(得分:1)

我遇到了这个问题,这很简单,只需将暂存器的环境从Content更改为Browser

答案 1 :(得分:0)

有很多问题。首先,为了定义Components,您需要拥有scratchpad running in the browser context。要执行此操作,请转到Developer Tool Settings并选中“启用Chrome和附加调试”复选框。重启Firefox,只是为了确保暂存器位于浏览器上下文中。

完成后,以下代码将起作用:

// -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();

答案 2 :(得分:0)

代码按原样工作,除了关闭,但这很明显,因为它来自暂存器范围。此代码设计为运行形式引导范围。但要使它从暂存器运行,你可以注释掉第一行并运行启动。然后关闭只是在关闭内运行循环,因为没有定义APP_SHUTDOWN。 &LT;&LT;&LT;对于scrathcpad范围,仅用于测试目的。一旦你想部署它,使用未注释的第1行代码,不需要运行启动或关闭,因为它们是引导入口和退出函数,因此它们会自动被调用。查看youtube视频:

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