Chrome Extension Manifest V3报错:无法访问chrome:// URL 求助
解决Chrome扩展"Cannot access a 'chrome://' URL"错误
问题根源
你的代码存在两个关键问题引发该错误:
chrome.tabs.onUpdated的回调参数顺序错误,导致过滤chrome://页面的逻辑完全失效;host_permissions中混入了无效的权限项,可能干扰权限判断逻辑。
具体修复步骤
1. 修正background.mjs的回调逻辑
chrome.tabs.onUpdated的正确回调签名是(tabId, changeInfo, tab),你把changeInfo和tab的顺序搞反了,导致过滤chrome://页面的判断根本没作用到正确的tab对象上。同时补全代码的闭合结构:
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { // 过滤chrome://、chrome-extension://、about:等Chrome内置受限URL if (tab.url?.startsWith("chrome://") || tab.url?.startsWith("chrome-extension://") || tab.url?.startsWith("about:")) { return; } if (tab.active && changeInfo.status === "complete") { chrome.scripting.executeScript({ target: { tabId: tabId }, files: ['contentScripts.mjs'] }).catch(err => { // 可选:捕获其他脚本注入错误 console.error("脚本注入失败:", err); }); } });
2. 清理manifest.json的无效配置
host_permissions字段仅用于指定可访问的URL模式,"tabs"和"notifications"属于扩展权限,已经在permissions中声明,需要从host_permissions中移除:
{ "version": "0.1.0", "manifest_version": 3, "name": "Super cool name", "description" : "A super cool intro", "author": "you wouldn't know him", "permissions": ["storage", "activeTab", "scripting", "tabs", "notifications"], "host_permissions": ["http://*/*", "https://*/*"], "background": { "service_worker": "background.mjs", "type": "module" }, "action": { "default_icon": {...}, "default_popup": "popup.html" }, "icons": {...}, "content_scripts": [{ "matches": ["http://*/*", "https://*/*"], "js": ["contentScripts.mjs"] }] }
额外说明
- Chrome扩展不允许在
chrome://、about:等内置页面注入脚本或访问其内容,必须严格过滤这些URL; - 你的
content_scripts配置已经正确限制在http/https页面,不会作用于chrome://页面,问题完全出在background脚本的错误逻辑上。
内容的提问来源于stack exchange,提问作者Basile
相关产品推荐
相关产品推荐

