You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

declarativeNetRequest重定向扩展内部页面失效问题排查

问题描述

配置Chrome扩展Manifest V3清单及declarative-net-request规则后,试图将特定域名重定向至扩展内部页面,但访问目标域名时无任何反应,也未出现报错信息,怀疑condition配置存在遗漏。

清单文件(manifest.json)

{
    "manifest_version": 3,
    "default_locale": "en",

    "name": "__MSG_extension_name__",
    "description": "__MSG_extension_description__",
    "version": "1.0",

    "icons": {
        "48": "images/icon-48.png",
        "96": "images/icon-96.png",
        "128": "images/icon-128.png",
        "256": "images/icon-256.png",
        "512": "images/icon-512.png"
    },

    "background": {
        "service_worker": "background.js"
    },

    "content_scripts": [{
        "js": [ "content.js" ],
        "matches": [ "*://example.com/*" ]
    }],

    "action": {
        "default_popup": "popup.html",
        "default_icon": {
            "16": "images/toolbar-icon-16.png"
        }
    },
    
    "web_accessible_resources": [
        {
          "resources": ["locked.html"],
          "matches": ["<all_urls>"]
        }
    ],
    "declarative_net_request" : {
        "rule_resources" : [{
          "id": "ruleset_1",
          "enabled": true,
          "path": "declarative-net-request-rules.json"
        }]
      },

    "permissions": ["tabs", "webNavigation", "declarativeNetRequest", "declarativeNetRequestWithHostAccess"]
}

规则文件(declarative-net-request-rules.json)

[
    {
   "id": 1,
   "action": {
      "type": "redirect",
      "redirect": {
          "extensionPath": "/locked.html?type=1"
      }
   },
   "condition": {
      "requestDomains": [
         "test-redir.app",
         "reddit.com",
         "example.net"
      ],
      "resourceTypes": [
         "main_frame",
         "sub_frame"
      ]
   }
}
]

问题分析与修复方案

你的condition配置存在关键遗漏,同时还有权限和匹配逻辑的问题,具体修复点如下:

1. 替换requestDomains为urlFilter

requestDomains仅匹配发起请求的页面域名,而非用户直接访问的目标域名。要拦截用户访问指定域名的请求,必须用urlFilter明确匹配目标URL:

"condition": {
  "urlFilter": "*://test-redir.app/*|*://reddit.com/*|*://example.net/*",
  "resourceTypes": ["main_frame", "sub_frame"]
}

|符号用于分隔多个匹配规则,实现多域名拦截。

2. 补充host_permissions权限

使用declarativeNetRequestWithHostAccess权限时,必须在manifest.json中声明要拦截的域名权限,否则规则无法生效。添加:

"host_permissions": [
    "*://test-redir.app/*",
    "*://reddit.com/*",
    "*://example.net/*"
]

3. 验证扩展资源路径

确认locked.html确实位于扩展根目录下,extensionPath路径无误。如果文件在子目录(如pages/locked.html),需同步修改路径。

4. 检查规则加载状态

通过Chrome扩展开发者工具验证规则是否正常加载:

  • 进入扩展管理页面,点击目标扩展的「详情」
  • 打开「Service Worker」的开发者工具
  • 切换到「Declarative Net Request」标签,查看规则是否启用、匹配条件是否正确

修复后的完整规则文件

[
    {
        "id": 1,
        "action": {
            "type": "redirect",
            "redirect": {
                "extensionPath": "/locked.html?type=1"
            }
        },
        "condition": {
            "urlFilter": "*://test-redir.app/*|*://reddit.com/*|*://example.net/*",
            "resourceTypes": ["main_frame", "sub_frame"]
        }
    }
]

内容的提问来源于stack exchange,提问作者Cornwell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 23:07:39