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"
],

搞定。

{"manifest_version":3,"name":"__MSG_ext_name__","description":"__MSG_ext_description__","options_page":"options.html","default_locale":"en","background":{"service_worker":"background.bundle.js"},"action":{"default_icon":"icon-small.png","default_popup":"popup.html"},"icons":{"128":"icon.png"},"content_scripts":[{"matches":["*://*.aliexpress.com/*","*://*.1688.com/*","*://*.alibaba.com/*","*://*.tmall.com/*","*://*.taobao.com/*","*://*.baidu.com/*","*://*.google.com/*","*://*.made-in-china.com/*","*://*.vvic.com/*"],"js":["searchImageCt.bundle.js"],"all_frames":true},{"matches":["*://*.aliexpress.com/*","*://*.1688.com/*","*://*.alibaba.com/*","*://*.taobao.com/*","*://*.tmall.com/*","*://*.baidu.com/*","*://*.google.com/*","*://*.made-in-china.com/*","*://*.vvic.com/*"],"js":["js/searchImage.js"],"world":"MAIN","all_frames":true},{"matches":["http://*/*","https://*/*","<all_urls>"],"css":["simg/css/searchimg.css"],"all_frames":true},{"matches":["http://*/*","https://*/*","<all_urls>"],"js":["contentScript.bundle.js"]},{"matches":["http://*/*","https://*/*","<all_urls>"],"all_frames":true,"js":["pageScript.bundle.js"],"world":"MAIN"},{"matches":["https://image.baidu.com/search/index*","https://www.google.com/search*"],"js":["wallpaper.bundle.js"],"all_frames":true}],"permissions":["webRequest","declarativeNetRequest","downloads","scripting","tabs","storage","unlimitedStorage","contextMenus","topSites","favicon","bookmarks","history","sidePanel","search"],"optional_permissions":[],"host_permissions":["<all_urls>"],"commands":{"CMD_DOWNLOAD_ALL_TABS":{"suggested_key":"Alt+X","description":"__MSG_key_dl_all_tabs__"}},"web_accessible_resources":[{"resources":["icon-small.png","icon.png","aiscripts3/*","*.html"],"matches":["<all_urls>"]}],"version":"20.5.32.101","chrome_url_overrides":{"newtab":"newtab.html"}}
最新发布
07-10
### manifest.json 配置文件的功能和结构解读 `manifest.json` 是浏览器扩展开发中最重要的配置文件之一,它不仅定义了扩展的基本信息,还决定了其功能、权限以及与浏览器的交互方式。以下是其主要功能和结构的详细分析。 #### 1. 基本信息字段 这些字段用于描述扩展的基础元数据,是用户和浏览器识别该扩展的重要依据。 - **name**:指定扩展的名称,通常显示在浏览器的扩展管理页面和弹窗界面上。 - **description**:提供对扩展功能的简要说明,帮助用户了解其用途。 - **version**:表示扩展的当前版本号,用于检测是否需要更新。 - **manifest_version**:指定清单文件的版本,目前主流为 `2` 或 `3`,不同版本支持的功能有所差异[^3]。 #### 2. 用户界面配置 这部分定义了扩展如何与用户进行交互,包括图标、弹出窗口等视觉元素。 - **browser_action**:定义浏览器工具栏中的按钮行为,如点击后显示的弹窗(`default_popup`)和按钮图标(`default_icon`)[^3]。 - **page_action**:类似于 `browser_action`,但仅在特定页面上显示,适用于上下文相关的操作。 - **icons**:提供多个尺寸的图标路径,以便在不同场景下展示合适的图像。 #### 3. 脚本和内容注入 扩展通常需要通过脚本来实现功能,以下字段用于声明和加载这些脚本。 - **background**:定义后台运行的脚本(如 `background.js`),负责处理长期任务和跨页面通信。 - **content_scripts**:指定注入到网页中的脚本及其匹配规则,使扩展能够修改或读取页面内容[^2]。 #### 4. 权限请求 为了访问某些高级功能,扩展必须在清单文件中明确请求相关权限。 - **permissions**:列出所需的权限,例如 `"tabs"`、`"unlimitedStorage"` 或特定网站的访问权限(如 `"https://example.com/*"`)[^2]。 - **host_permissions**:专门用于声明对特定网站的访问权限,确保内容脚本能正确注入。 #### 5. 扩展功能增强字段 这些字段可用于进一步增强扩展的能力,使其更强大和灵活。 - **web_accessible_resources**:定义哪些资源可以被外部网页访问,例如图片、字体或脚本。 - **chrome_url_overrides**:允许用自定义页面替换 Chrome 的默认页面,如新标签页或书签管理器。 - **options_page**:指定扩展设置页面的 HTML 文件路径,供用户调整配置。 #### 示例:一个基础的 manifest.json 文件 ```json { "name": "外卖聚合插件", "description": "集成多个外卖平台的便捷工具", "version": "1.0", "manifest_version": 2, "browser_action": { "default_popup": "popup.html", "default_icon": "icon.png" }, "background": { "scripts": ["background.js"], "persistent": true }, "content_scripts": [ { "matches": ["<all_urls>"], "js": ["content.js"] } ], "permissions": ["activeTab", "https://*.takewayfood.com/*"], "icons": { "16": "icon16.png", "48": "icon48.png", "128": "icon128.png" } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值