"Uncaught (in promise) Error: The 'activeTab' permission is not in effect because this extension has not been in invoked." in Chrome Extension

"Uncaught (in promise) Error: The 'activeTab' permission is not in effect because this extension has not been in invoked." in Chrome Extension

我拥有 <all_urls> 主机权限和 activeTab 权限,但此错误出现在 chrome://extensions/ 上。这是我的清单(删除了不重要的细节):

{
    "action": {
        "default_icon": {...},
        "default_popup": "...",
        "default_title": "..."
    },
    "author": "...",
    "background": {
        "service_worker": "./some-js-1.js"
    },
    "content_scripts": [{
        "matches": ["<all_urls>"],
        "js": ["./some-js-2.js"]
    }],
    "description": "...",
    "host_permissions": [
        "<all_urls>"
    ],
    "icons": {...},
    "manifest_version": 3,
    "name": "...",
    "options_page": "...",
    "permissions": [
        "activeTab",
        "storage",
        "tabs"
    ],
    "short_name": "...",
    "version": "..."
}

在我的后台脚本 (./some-js-1.js) 中,这是截图的代码:

browser.windows.getCurrent().then(function(win){
    browser.tabs.captureVisibleTab(win.id,{
        "format":"png"
    }).then(function(dataURI){
        console.log(dataURI);
        // do something with it
    });
});

它适用于大多数地方,但在 chrome://extensions/ 上它不起作用 - 即使有 <all_urls>activeTab 权限。

引用the documentation(强调我的),

In order to call this method, the extension must have either the <all_urls> permission or the activeTab permission. In addition to sites that extensions can normally access, this method allows extensions to capture sensitive sites that are otherwise restricted, including chrome:-scheme pages, other extensions' pages, and data: URLs. These sensitive sites can only be captured with the activeTab permission. File URLs may be captured only if the extension has been granted file access.

我有 <all_urls>activeTab,但 chrome://extensions 仍然不可用。当我尝试将 <all_urls> 放入 permissions 键时,我收到一条警告说,

Permission '<all_urls>' is unknown or URL pattern is malformed.

我在 host_permissions 中有它,因为我想访问所有主机,但似乎仍然没有授予权限(或者我做错了什么)。

我该如何解决这个问题?如果您需要更多信息,请在评论中询问。

您不能在 chrome://extensions/ <-- 之前的评论

中注入

我仓促,没看对。 我从未使用过“captureVisibleTab”,但我很快阅读了规范,您似乎正确地包含了“activeTab”权限。

然而,另一方面,从清单中可以清楚地看出您想要将脚本注入每个页面。

我的建议(这可能会让你无处可去)是评论清单中的“content_scripts”部分,然后重新安装扩展,看看是否仍然会抛出该错误。

此外,我建议您将后台脚本中的 captureVisibleTab 操作绑定到 browserAction 单击或上下文菜单项。

最后,不排除这个API可能是MV3版本有一些bug