为何我的Declarative Net Request在谷歌搜索页面无法生效?
问题
尝试将所有指向谷歌的请求重定向至其他网站。当前配置对google.com和google.com/test能正常生效(分别跳转至example.com和example.com/test),但对谷歌搜索页面(google.com/search?...)无效。补充说明:使用Opera GX浏览器,且已开启扩展的“Allow access to the search page results”复选框。
当前配置
manifest.json
{ "manifest_version": 3, "name": "Redirect Google", "version": "1.0", "declarative_net_request": { "rule_resources": [{ "id": "ruleset_1", "enabled": true, "path": "rules_1.json" }] }, "permissions": [ "declarativeNetRequest" ], "host_permissions": [ "*://*.google.com/*" ] }
rules_1.json
[{ "id": 1, "priority": 1, "action": { "type": "redirect", "redirect": { "transform": { "host": "example.com" } } }, "condition": { "urlFilter": "||google.com", "resourceTypes": ["main_frame"] } }]
问题原因及解决办法
核心原因
谷歌搜索页面的跳转逻辑特殊:当发起google.com/search?xxx请求后,谷歌会立即通过HTTP 3xx重定向跳转到对应地区的域名(比如google.com.hk/search?xxx、google.co.jp/search?xxx等)。你的规则仅匹配google.com,无法覆盖这些带地区后缀的谷歌域名,导致重定向失效。
解决步骤
- 修改URL过滤规则
将urlFilter改为||google.,这样可以匹配所有以google.开头的域名(包括所有地区化谷歌域名)。修改后的rules_1.json如下:[{ "id": 1, "priority": 1, "action": { "type": "redirect", "redirect": { "transform": { "host": "example.com" } } }, "condition": { "urlFilter": "||google.", "resourceTypes": ["main_frame"] } }] - 验证现有配置项
保持priority为1即可(当前无冲突规则);resourceTypes设为["main_frame"]是正确的,仅需重定向页面主框架请求。 - 确认浏览器权限
你已开启Opera GX的“Allow access to the search page results”,该权限配置无需调整。
内容的提问来源于stack exchange,提问作者Dnomyar96
相关产品推荐
相关产品推荐

