暂存器中的Firefox插件错误 [英] Firefox addon error in scratchpad

查看:138
本文介绍了暂存器中的Firefox插件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在此处测试此代码.如果我尝试加入这些代码,它将阻止"某些URL.

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

在Firefox的暂存器中,出现此错误:

In scratchpad of Firefox and I get this error:

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

控制台没有错误.

NO ERROR ON CONSOLE.

有人知道这个错误是什么吗??
我读过Firefox不能很好地使用常量,但有时此代码有时无法正常工作.

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?

推荐答案

除了关机,代码按原样工作,但是那很明显,因为它来自暂存器范围.这段代码是为运行表单引导程序作用域而设计的.但是要使其从暂存器运行,您需要注释掉第一行,然后运行启动程序.然后要关机,只需在关机内运行循环即可,因为未定义APP_SHUTDOWN. <<<用于scrathcpad示波器,仅用于测试目的.一旦您要部署它,请使用未注释的第1行代码,并且因为它们是引导程序入口和出口函数,所以无需运行启动或关闭窗口,因此它们会自动被调用.观看youtube视频:

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

这篇关于暂存器中的Firefox插件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆