![](/img/trans.png)
[英]In Chrome extension Manifest v3, how do i instruct manifest.json to run my extension only on certain webpages without using activeTab permission?
[英]Make chrome extension work only on certain sites (manifest v3)
我在升级到 manifest v3 时遇到了一些麻烦,非常感谢您的帮助和意见。
背景:
在遵循 chrome 的教程之后,该教程在下面重新发布:
// background.js
// Wrap in an onInstalled callback in order to avoid unnecessary work
// every time the background script is run
chrome.runtime.onInstalled.addListener(() => {
// Page actions are disabled by default and enabled on select tabs
chrome.action.disable();
// Clear all rules to ensure only our expected rules are set
chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
// Declare a rule to enable the action on example.com pages
let exampleRule = {
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {hostSuffix: '.example.com'},
})
],
actions: [new chrome.declarativeContent.ShowAction()],
};
// Finally, apply our new array of rules
let rules = [exampleRule];
chrome.declarativeContent.onPageChanged.addRules(rules);
});
});
我注意到我的扩展灰色图标在与指定模式不匹配的网站上显示,并且在与 url 模式匹配的网站上显示颜色(预期行为)。 但是,当我在与 url 模式匹配的站点上单击扩展时,该扩展仍然处于禁用状态。
问题:有没有人能够让这个示例代码工作? 仅当用户在特定站点上并单击了扩展程序时,如何才能使 chrome 扩展程序起作用?
提前致谢!
您只需在 manifest.json 权限中添加“declarativeContent”即可。
"permissions": [
"declarativeContent"
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.