阻止子网域中某个特定字词的网址 [英] Block URL with a specific word somewhere in the subdomain
问题描述
我尝试制作一个Chrome扩展程序,用于阻止子网域中包含特定字词的网址,但不包括该网域中的其他网址。例如,假设我想阻止所有tumblr博客中的子域名为cola。
它应该能够阻止此页面: http://coca-cola.tumblr.com/ 。
我试图使用这个URL匹配: url:[*://*cola*.tumblr.com/*]
,但它不起作用。我想不出任何其他可能的工作组合。有人可以指出我正确的方向吗?
这是我的完整background.js:
chrome.webRequest.onBeforeRequest.addListener(
function(){
return {cancel:true};
},
{
url: [*://*cola*.tumblr.com/*] //这是我弄乱的部分。
},
[blocking]
);
您的代码失败,因为 * ://*cola*.tumblr.com/*
不是有效的匹配模式。通配符只能用于URL的路径组件或主机名的开始处。
如果要阻止其子域包含某个关键字的URL,你需要匹配整个域,并使用JavaScript来检查子域是否包含亵渎词。
chrome.webRequest.onBeforeRequest .addListener(
function(details){
var hostname = details.url.split('/',3)[2];
return {
cancel:hostname.indexOf ('可乐')> = 0
};
},
{
url:[*://*.tumblr.com/*]
},
[blocking]
);
或者使用 chrome.declarativeWebRequest
API(省略 chrome.runtime.onInstalled
为简洁起见):
chrome.declarativeWebRequest.onRequest.addRules({
id:'一些规则ID',
条件:[
new chrome.declarativeWebRequest .RequestMatcher({
url:{
hostContains:'cola',
hostSuffix:'.tumblr.com'
}
})
],
操作:[
new chrome.declarativeWebRequest.CancelRequest()
]
});
I am trying to make a Chrome extension that blocks URLs with a specific word in the subdomain, but not other URLs in that domain. For example, let's say I want to block all tumblr blogs with the word "cola" in the subdomain.
It should be able to block this page: http://coca-cola.tumblr.com/.
I have tried to use this url match: urls:["*://*cola*.tumblr.com/*"]
, but it is not working. And I cannot think of any other combinations that might work. Can somebody point me in the right direction?
This is my full background.js:
chrome.webRequest.onBeforeRequest.addListener(
function() {
return {cancel: true };
},
{
urls:["*://*cola*.tumblr.com/*"] // This is the part I'm messing up.
},
["blocking"]
);
Your code fails because *://*cola*.tumblr.com/*
is not a valid match pattern. Wildcards can only be used in the path component of an URL, or at the start of a host name.
If you want to block an URL whose subdomain contains some keyword, you need to match the whole domain, and use JavaScript to check whether the subdomain contains the profane word.
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var hostname = details.url.split('/', 3)[2];
return {
cancel: hostname.indexOf('cola') >= 0
};
},
{
urls:["*://*.tumblr.com/*"]
},
["blocking"]
);
Or using the chrome.declarativeWebRequest
API (omitted chrome.runtime.onInstalled
event for brevity):
chrome.declarativeWebRequest.onRequest.addRules({
id: 'some rule id',
conditions: [
new chrome.declarativeWebRequest.RequestMatcher({
url: {
hostContains: 'cola',
hostSuffix: '.tumblr.com'
}
})
],
actions: [
new chrome.declarativeWebRequest.CancelRequest()
]
});
这篇关于阻止子网域中某个特定字词的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!