所以,我正在开发一个Chrome扩展,需要在任何具有example.extension
域的网站上激活,其中extension
可以是任何东西(.com
,.de
等)。
我有一个内容脚本,在manifest.json
中我包含了一个接一个列出的所有域名:
"content_scripts": [{
"js": ["content.js"],
"matches": ["https://www.example.com/*","https://www.example.de/*"]
}]
但是我怎么能写出与example.*
相匹配的东西而不是枚举它们呢?请注意,我尝试过这样的东西,它不起作用:
"content_scripts": [{
"js": ["content.js"],
"matches": ["https://www.example*"]
}]
这些是两个选项:
第一种选择:您可以使用清单中的include_globs
,如here所示
"content_scripts": [{
"js": ["content.js"],
"matches": [ "*://*/*" ],
"include_globs": [
"*://*.example.*/*",
]
}]
第二个选项:您可以更改内容脚本以检查目标URL
是否与www.example.*
匹配:
if (window.location.host.startsWith('www.example')){
//content script code
}
您可以使用持久性后台脚本,并在url符合您的要求时注入脚本。例如:
background.js
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status == 'complete' && tab.url.indexOf('https://www.example.') == 0){
chrome.tabs.executeScript(tabId,{file:'content.js'});
}
});