问题描述
在firefox扩展中,我想拦截url浏览器正在请求和完全阻止请求,如果一些条件匹配我如何拦截URL被请求
你可以看看这些插件的源代码。
https://addons.mozilla.org/en-us/firefox/addon/blocksite/?src=search> https://addons.mozilla.org/en-us/firefox/addon/blocksite/ ?src = search
或者使用服务观察者来处理 nsIHTTPChannel
来快速处理
$ $ p $ $ codeonst $ { Ci,Cu,Cc,Cr} = require('chrome'); // const {interfaces:Ci,utils:Cu,classes:Cc,results:Cr} = Components;
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/devtools/Console.jsm');
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
if(requestUrl.indexOf('google.com')> -1){
//httpChannel.cancel(Cr.NS_BINDING_ABORTED); //取消加载
httpChannel.redirectTo(Services.io.newURI('data:text,url_blocked',null,null)); //可以重定向到这一行,如果不想重定向然后阻塞,那么取消注释这一行并注释掉上面的行(第17行)
reg: {
Services.obs.addObserver(观察者['http-on-modify-request'],'http-on-modify-request',false);
},
unreg:function(){
Services.obs.removeObserver(observers ['http-on-modify-request'],'http-on-modify-request');
}
}
};
开始观察
观察所有的请求(例如在你的插件启动时)
for(var o in observers){
观察员[O]的.reg();
停止观察
重要的是停止观察(确保至少在关闭插件时运行这个,你不想离开观察者注册内存的原因)
for(var o in observers){
observers [o] .unreg();
$ b $ p
$ b 阻止/重定向url的观察者服务的完整工作示例:
in firefox extension I want to intercept the url browser is making request to and block the request entirely if some condition matches
how can I intercept URL being requested
解决方案 you can have a look at the source of those addons
https://addons.mozilla.org/en-us/firefox/addon/blocksite/?src=search
https://addons.mozilla.org/en-us/firefox/addon/url-n-extension-blockune-bl/?src=search
or use service observer with nsIHTTPChannel
for fast handling
const { Ci, Cu, Cc, Cr } = require('chrome'); //const {interfaces: Ci, utils: Cu, classes: Cc, results: Cr } = Components;
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/devtools/Console.jsm');
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
if (requestUrl.indexOf('google.com') > -1) {
//httpChannel.cancel(Cr.NS_BINDING_ABORTED); //this aborts the load
httpChannel.redirectTo(Services.io.newURI('data:text,url_blocked', null, null)); //can redirect with this line, if dont want to redirect and just block, then uncomment this line and comment out line above (line 17)
}
},
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');
}
}
};
To start observing
To start start obseving all requests do this (for example on startup of your addon)
for (var o in observers) {
observers[o].reg();
}
To stop observing
Its important to stop observring (make sure to run this at least on shutdown of addon, you dont want to leave the observer registered for memory reasons)
for (var o in observers) {
observers[o].unreg();
}
Full working example of the observer service to block/redirect urls: https://github.com/Noitidart/PortableTester/tree/block-urls
这篇关于Firefox扩展:拦截它所请求和有条件的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!