Chrome扩展处理路由器HTTPS重定向连接拒绝报错求助
问题分析与修复方案
错误原因
你遇到的Frame with ID 0 is showing error page错误,是因为当触发net::ERR_CONNECTION_REFUSED时,标签页加载的是Chrome内置的错误提示页面——这类页面不属于你的host_permissions范围,也不允许注入脚本,所以executeScript调用会失败。
修复方案
方案1:提前拦截重定向(无需等待错误发生)
直接在路由器发起HTTP转HTTPS的重定向阶段拦截,避免进入错误页面。修改background.js如下:
chrome.webNavigation.onBeforeRedirect.addListener((details) => { // 匹配从HTTP路由器地址重定向到HTTPS的请求 if (details.url.startsWith('http://192.168.0.1') && details.redirectUrl.startsWith('https://192.168.0.1')) { // 强制标签页停留在HTTP页面,阻断重定向 chrome.tabs.update(details.tabId, { url: details.url }); } }, { url: [{ hostEquals: '192.168.0.1' }] });
同时可以删除原有的onErrorOccurred监听代码,以及manifest.json中的content_scripts配置(不再需要回退操作)。
方案2:用Declarative Net Request拦截(Manifest V3推荐)
这是Manifest V3官方推荐的高效网络拦截方式,无需后台脚本监听,直接通过规则处理:
- 更新
manifest.json,添加权限和规则配置:
{ "manifest_version": 3, "name": "Zyxel Blocker", "description": "Blocks your Zyxel Router from redirecting you to HTTPS", "version": "1.0", "permissions": ["declarativeNetRequest"], "host_permissions": ["*://192.168.0.1/*"], "declarative_net_request": { "rule_resources": [{ "id": "ruleset_1", "enabled": true, "path": "rules.json" }] } }
- 在扩展根目录创建
rules.json,添加协议转换规则:
[ { "id": 1, "priority": 1, "action": { "type": "redirect", "redirect": { "transform": { "scheme": "http" } } }, "condition": { "urlFilter": "*://192.168.0.1/*", "requestDomains": ["192.168.0.1"], "resourceTypes": ["main_frame"] } } ]
该规则会自动将所有针对192.168.0.1的HTTPS请求重定向为HTTP请求,从根源上避免重定向和连接错误。
内容的提问来源于stack exchange,提问作者JxxIT
相关产品推荐
相关产品推荐

