暂存器中的 Firefox 插件错误

Firefox addon error in scratchpad

我想在这里测试这段代码。此代码 "blocks" 一些 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 不能很好地使用常量,但这段代码有时有效有时无效。

还有谁能帮我修一下,让它一直能用吗?

存在多个问题。首先,为了定义 Components,您需要有 scratchpad running in the browser context. To do this go to the Developer Tool Settings 并选中 "Enable chrome and add-on debugging" 框。重新启动 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();

除关机外,代码按原样工作,但这是显而易见的,因为它来自暂存器范围。此代码设计为 ot 运行 形式 bootstrap 范围。但是要使其成为 运行 形式的暂存器,您需要注释掉第一行,然后 运行 启动。然后关闭 运行 关闭内部的循环,因为 APP_SHUTDOWN 未定义。 <<< 用于 scrathcpad 范围,仅用于测试目的。一旦您想部署它,请使用未注释的第 1 行代码,无需 运行 启动或关闭,因为它们是 bootstrap 入口和出口函数,因此它们会自动被调用。观看 youtube 视频:

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

我遇到了这个问题,很简单,把你的scratchpad环境从Content改成Browser

就足够了