我正在尝试在这里测试这段代码。如果我尝试加入他们,这个代码会"屏蔽"一些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
,您需要在浏览器上下文中运行草稿栏。要执行此操作,请转到"开发人员工具设置"并选中"启用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();
我遇到了这个问题,它很容易,将您的scratchpad环境从Content
更改为Browser
就足够了
除了关闭之外,代码照常工作,但这很明显,因为它来自草稿行范围。此代码设计用于从引导程序范围运行。但要让它在草稿栏中运行,您可以注释掉第一行,然后运行启动程序。则由于没有定义APP_shutdown,所以关闭时只从关闭中运行循环<lt<对于scrathcpad范围,它仅用于测试目的。一旦您想部署它,请使用未注释的第1行代码,无需运行启动或关闭,因为它们是引导程序入口和出口函数,因此会自动被调用。查看youtube视频:
https://www.youtube.com/watch?v=pxt8lJ64nVw