【问题标题】:Firefox addon error in scratchpad暂存器中的 Firefox 插件错误
【发布时间】:2015-09-11 17:20:51
【问题描述】:

我正在尝试在这里测试此代码。如果我尝试加入,此代码会“阻止”某些 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 不能很好地处理常量,但这段代码有时工作有时不工作。

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

【问题讨论】:

  • 至于它有时工作或有时不工作,我们需要您提供更多关于它如何不工作的信息(例如,当它工作/不工作时控制台中显示的内容)?使用您提供的信息,很容易看出为什么会收到报告的错误,但这只是对您所看到的内容的猜测。不幸的是,鉴于您的问题中有“Cu 未定义”错误,任何进一步的问题都应该是一个新问题。问题应尽可能集中在一个特定问题上。见How to Ask
  • @Mayken 如果暂存器运行的范围没有将 Cu 定义为 const,它可能第一次对他有用。所以第二次运行会出错

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


【解决方案1】:

存在多个问题。首先,为了定义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();

【讨论】:

  • 当我取消注释第一行时,它也不起作用。当它工作时,它不会在代码上显示任何错误,当我尝试加入 facebook.com 时,它会按预期显示 url_blocked that would have went to facebook。但是当它不起作用时,它会显示我在帖子中所说的错误。浏览器控制台上也没有错误。
  • @SteliosM。你是说当第一行代码没有被注释掉时,你仍然得到报错“Cu is not defined”?您需要更清楚地了解工作或不工作是什么意思,以及如何测试它。
  • 是的,当我注释掉第一行时,我仍然在暂存器中收到错误。当我尝试加入 facebook.com 时,它会转到该页面。所以我的代码不起作用导致它被编程阻止 facebook.com 并将其替换为第 9 行中的文本'url_blocked would have went to facebook'
  • 如果您从暂存器运行此程序,则必须取消注释该行。当你最终将它部署到你的插件中时,那一行是绝对需要的。
  • @SteliosM。还有许多其他问题。我在答案中添加了更新、测试的代码。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    代码除了关机外按原样工作,但这很明显,因为它来自暂存器范围。此代码设计为在引导范围内运行。但是要让它从暂存器中运行,你注释掉第一行,然后运行启动。然后要关闭,只需从关闭中运行循环,因为 APP_SHUTDOWN 未定义。

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

    【讨论】:

    猜你喜欢
    • 2018-01-17
    • 2014-02-12
    • 1970-01-01
    • 2016-12-12
    • 2021-05-22
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多