chrome.tabs.captureVisibleTab() 报错 Either the ‘<all_urls>‘ or ‘activeTab‘ permission is required.

Either the '' or 'activeTab' permission is required.

Manifest 从 v2 改成 v3 后,原来的 chrome.tabs.captureVisibleTab() 函数突然报错了。

Either the ‘<all_urls>’ or ‘activeTab’ permission is required.

根据 官方文档 对 captureVisibleTab() 的说明:

In order to call this method, the extension must have either the <all_urls> permission or the activeTab permission.

我们在 manifest.json 中加入了 “<all_urls>”,还是报错

"permissions": [
    "tabs",
    "webNavigation",
    "storage",
    "activeTab",
    "sidePanel",
    "tabCapture",
    "<all_urls>"
],

我们跳转到 <all_urls> 的说明,发现 “<all_urls>” 根本不在 “permissions” 的 known strings 里面,它应该属于 “host_permissions”,因此,我们将 manifest.json 内容改成

"host_permissions": [
    "<all_urls>"
]
"permissions": [
    "tabs",
    "webNavigation",
    "storage",
    "activeTab",
    "sidePanel",
    "tabCapture"
],

搞定。

Chrome 扩展开发中,使用 `<all_urls>` 权限时出现错误 `Permission '<all_urls>' is unknown or URL pattern is malformed`,通常是由于清单文件(manifest.json)的语法或格式问题导致。以下是对该问题的详细分析与解决方案: ### 1. 检查权限字段的写法 Chrome 扩展要求权限字段中的 URL 模式必须是有效的字符串形式。如果 `<all_urls>` 被错误地转义为 `<all_urls>`,则会导致解析失败并提示 URL 模式不合法[^2]。 正确的写法应为: ```json "permissions": [ "tabs", "webNavigation", "storage", "activeTab", "sidePanel", "<all_urls>" ] ``` 错误的写法示例(会导致问题): ```json "permissions": [ "<all_urls>" ] ``` ### 2. 确保使用正确的 Manifest 版本 Manifest V3 对权限和脚本注入机制进行了调整,推荐使用 `chrome.scripting.executeScript` 替代旧版的 `chrome.tabs.executeScript`。同时,确保 `"manifest_version"` 设置为 `3`,否则可能导致某些功能无法正常运行或报错[^1]。 一个标准的 `manifest.json` 配置如下: ```json { "name": "My Extension", "version": "1.0", "description": "Access all websites content", "permissions": ["<all_urls>", "activeTab", "scripting"], "background": { "service_worker": "background.js" }, "manifest_version": 3 } ``` ### 3. 使用后台服务工作线程 在 Manifest V3 中,`background_page` 已被弃用,取而代之的是 `service_worker`。如果仍然使用 `background_page`,会提示 `'background_page' requires manifest version of 1 or lower` 错误[^1]。 正确配置方式如下: ```json "background": { "service_worker": "background.js" } ``` ### 4. 避免非法的 URL 模式 除了 `<all_urls>`,还可以指定特定域名的访问权限,例如: ```json "permissions": [ "https://*.example.com/*" ] ``` 确保 URL 模式符合 Chrome 的规范,避免拼写错误或格式不正确的情况。 ### 5. 检查扩展程序安装方式 如果通过开发者模式加载扩展程序时出现问题,可以尝试清除浏览器缓存或使用干净的用户配置文件重新加载扩展。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值