我希望Chrome扩展图标在除docs.google.com页面之外的所有页面上都被禁用(灰色)。这是我在background.js中的代码。
'use strict';
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [new chrome.declarativeContent.PageStateMatcher({
pageUrl: { urlContains: 'docs.google' },
})
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}]);
});
});
从pageActions的文档来看,这应该会导致我的扩展图标在所有页面上都是灰色的,除了那些在docs.google中有docs.google的页面。但是图标在所有页面上都是活动的(不是灰色的)。在非docs.google页面上点击它会导致它什么也不做,但我希望它一开始就变成灰色的。
对此有什么想法吗?
发布于 2020-10-22 04:49:36
这是一个铬中的缺陷,到目前为止还不清楚它是否可以修复。
同时,您可以自己维护图标:
- ManifestV2:
"page_action":{ "default_icon":{ "16":“图标/16-灰色page_action”,"32":“图标/32-gray.png”}}
- ManifestV3 uses `action` instead of `page_action`
"action":{ "default_icon":{ "16":“图标/16-灰色”,"32":“图标/32-gray.png”}
发布于 2021-02-22 11:34:26
如果使用清单版本2,只需在page_action中声明彩色图标,而不是灰色图标。
// manifest.json
"manifest_version": 2
"page_action": {
"default_icon": "icon-color.png"
},
然后图标将在权限和匹配的URL中为灰色。您可以在页面操作/清单上看到描述。
但是在清单v3中,上面的配置似乎不再工作了。
发布于 2022-10-02 18:56:48
Chrome将这种行为记录在他们的文档:https://developer.chrome.com/docs/extensions/reference/action/#emulating-pageactions-with-declarativecontent中。以下说明如何在清单v3中实现此功能。
// background.js
chrome.runtime.onInstalled.addListener(() => {
// disable the action by default
chrome.action.disable();
// remove existing rules so only ours are applied
chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
// add a custom rule
chrome.declarativeContent.onPageChanged.addRules([
{
// define the rule's conditions
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostSuffix: "reddit.com" },
}),
],
// show the action when conditions are met
actions: [new chrome.declarativeContent.ShowAction()],
},
]);
});
});
这将需要declarativeContent
在manifest.json
中的权限
{
...
"permissions": ["declarativeContent"]
...
}
https://stackoverflow.com/questions/64473519
复制相似问题