【发布时间】:2022-08-15 17:50:03
【问题描述】:
我在升级到 manifest v3 时遇到了一些麻烦,非常感谢您的帮助和意见。
背景:
- 使用清单 v3
- 使用 ShowAction() 代替已弃用的 ShowPageAction()
- 用于清单 v2 和 ShowPageAction()
- 已阅读此post 不适用于清单 v3(或似乎不适用)
- 还关注了这个 google guide 从 ShowPageAction 升级到 ShowAction
关注 chrome\'s tutorial 后,转发如下:
// 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 扩展程序工作?
提前致谢!
-
听起来像是 MV3 中的一个错误。作为一种解决方法,您可以默认设置一个灰色图标(在 manifest.json 中)而不是 disable(),并在
actions中设置一个彩色图标,如 in this answer 所示。 -
我明白了,谢谢!我尝试了给定的示例,但似乎它只是使图标变灰。我希望完全禁用扩展。
-
我认为我的问题范围错误,我有一个扩展程序,当单击图标时会显示一个弹出窗口。我希望仅在某些 url 上显示弹出窗口,通过使用 ShowPageAction() 在清单 2 中完美运行
-
我认为在不受支持的网站上单击图标时不显示任何弹出窗口会让用户感到困惑,因为它显示了绝对不相关且无用的上下文菜单。这是此 API 的设计缺陷。我建议您在弹出窗口中检查选项卡的 URL,并在人们单击灰色图标时显示当前 URL 不受明确支持的通知,这表明他们不理解该概念。
-
@wOxxOm 但这是否意味着要检查选项卡的 URL,扩展程序需要它通常不需要的权限? (这是首先使用 declarativeContent 的动机)
标签: google-chrome google-chrome-extension chrome-extension-manifest-v3