我有一个浏览器扩展程序,带有在所有URL上运行的内容脚本。
{
"name": "foobar",
"version": "0.1.0",
"permissions": [],
"manifest_version": 2,
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
]
}
我现在想允许用户在某些站点上disable内容脚本,例如,通过单击弹出窗口中的按钮。我将如何处理?
从后台脚本(具有"activeTab"
和"https://*/"
权限),可以使用chrome.tabs.executeScript
注入任何脚本:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
const inject = /.*:\/\/.*stackoverflow.com\/.*/.test(request.url);
if (inject) {
chrome.tabs.executeScript({
file: 'foobar.js'
});
}
// Send an empty response to avoid warning
sendResponse({});
});