admin管理员组文章数量:1333689
In my Firefox extension I want to intercept the url that the browser is requesting and block the request entirely if some condition matches
How can I intercept URL being requested?
In my Firefox extension I want to intercept the url that the browser is requesting and block the request entirely if some condition matches
How can I intercept URL being requested?
Share Improve this question edited Jun 3, 2020 at 22:11 intika 9,8027 gold badges40 silver badges55 bronze badges asked Jun 2, 2015 at 0:15 user3833308user3833308 1,2123 gold badges17 silver badges41 bronze badges 1- 3 I think best option is is nsIContentPolicy but you can also use observer service: stackoverflow./a/25328750/1828637 – Noitidart Commented Jun 2, 2015 at 0:30
2 Answers
Reset to default 5you can have a look at the source of those addons
https://addons.mozilla/en-us/firefox/addon/blocksite/?src=search https://addons.mozilla/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.') > -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 unment this line and ment 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./Noitidart/PortableTester/tree/block-urls
An other possible solution :
Here is an other implementation as modules example from HTTPS-Everywhere
Init function :
init: function() {
// start observing all http requests
Services.obs.addObserver(httpNowhere, "http-on-modify-request", false);
},
Observer function :
observe: function(subject, topic, data) {
var request = subject.QueryInterface(Ci.nsIHttpChannel);
if (topic == "http-on-modify-request") {
if (request.URI.spec == "xxx.example.") {
request.redirectTo("yyy.example.");
}
else {
request.cancel(Components.results.NS_ERROR_ABORT);
}
}
},
Example addons :
HTTPS-Nowhere - https://github./cwilper/http-nowhere
HTTPS-Everywhere - https://github./EFForg/https-everywhere
Migrating your extension to chrome :
i answered your question for chrome in this page : Chrome Extension : How to intercept requested urls?
版权声明:本文标题:javascript - Firefox extension: How to intercept the requested url conditionally and block it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742356719a2459561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论