【发布时间】:2021-08-03 17:05:03
【问题描述】:
我需要一个 Chrome 扩展程序才能在除一个域之外的每个域上运行。它到处运行:
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"run_at": "document_start",
"js": ["contentScript.js"]
}
]
但是添加exclude_matches似乎没有效果:
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"exclude_matches": ["http://*.mysite.com/"],
"run_at": "document_start",
"js": ["contentScript.js"]
}
]
我尝试使用这些文档中的匹配模式,但扩展仍然加载在该页面上:
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"exclude_matches": ["*://mozilla.org/"],
"run_at": "document_start",
"js": ["contentScript.js"]
}
],
在官方文档中,该示例用于排除域的特定页面,因此可能无法以我尝试的方式使用?
https://developer.chrome.com/docs/extensions/mv2/content_scripts/
{
"name": "My extension",
...
"content_scripts": [
{
"matches": ["http://*.nytimes.com/*"],
"exclude_matches": ["*://*/*business*"],
"js": ["contentScript.js"]
}
],
...
}
【问题讨论】:
-
您的第一次尝试使用了
http,所以难怪它不起作用,因为现在大多数网站都是https。在第二个尝试排除带有任何子域前缀的父域*.并添加*后缀以匹配任何路径,如*://*.mozilla.org/*(它显示在 MDN btw 中)并且还有exclude_globs以获得更大的灵活性. -
如果我想排除某个域的所有 TLD,例如 google.org、google.com、google.anything 等,是否需要 chrome.webRequest? developer.chrome.com/docs/extensions/reference/webRequest