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

如何在Manifest V3中实现Chrome扩展指定页面访问重定向

适配Manifest V3的URL重定向方案

在Manifest V3中,webRequest的blocking模式已被废弃,替代方案是Declarative Net Request API,该API性能更优且无需后台脚本持续运行。以下是两种适配方案:

方案一:静态规则(推荐)

静态规则直接在配置文件中定义,无需后台脚本,是最轻量化的实现方式。

1. 编写manifest.json

{
  "manifest_version": 3,
  "name": "URL重定向工具",
  "version": "1.0",
  "declarative_net_request": {
    "rule_resources": [
      {
        "id": "redirect_rules",
        "enabled": true,
        "path": "rules.json"
      }
    ]
  },
  "permissions": ["declarativeNetRequest"],
  "host_permissions": [
    "*://foo.com/*",
    "*://www.foo.com/*",
    "*://example.com/*"
  ]
}

2. 编写规则文件rules.json

将原逻辑转换为Declarative Net Request规则,匹配指定URL并替换域名:

[
  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "regexSubstitution": "http://example.com\\1"
      }
    },
    "condition": {
      "regexFilter": "^https?://foo\\.com/demo(.*)$",
      "resourceTypes": ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
    }
  },
  {
    "id": 2,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "regexSubstitution": "http://example.com/test/\\1"
      }
    },
    "condition": {
      "regexFilter": "^https?://www\\.foo\\.com/test/(.*)$",
      "resourceTypes": ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
    }
  }
]

方案二:动态规则(适合需修改规则的场景)

如果需要动态调整重定向目标或匹配规则,可通过Service Worker实现:

1. 编写manifest.json

{
  "manifest_version": 3,
  "name": "动态URL重定向工具",
  "version": "1.0",
  "permissions": ["declarativeNetRequestWithHostAccess"],
  "host_permissions": [
    "*://foo.com/*",
    "*://www.foo.com/*",
    "*://example.com/*"
  ],
  "background": {
    "service_worker": "background.js"
  }
}

2. 编写Service Worker脚本background.js

const targetHost = "http://example.com";

// 扩展安装/更新时注册规则
chrome.runtime.onInstalled.addListener(async () => {
  // 先移除旧规则避免冲突
  await chrome.declarativeNetRequest.updateDynamicRules({
    removeRuleIds: [1, 2],
    addRules: [
      {
        id: 1,
        priority: 1,
        action: {
          type: "redirect",
          redirect: {
            regexSubstitution: `${targetHost}\\1`
          }
        },
        condition: {
          regexFilter: "^https?://foo\\.com/demo(.*)$",
          resourceTypes: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
        }
      },
      {
        id: 2,
        priority: 1,
        action: {
          type: "redirect",
          redirect: {
            regexSubstitution: `${targetHost}/test/\\1`
          }
        },
        condition: {
          regexFilter: "^https?://www\\.foo\\.com/test/(.*)$",
          resourceTypes: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
        }
      }
    ]
  });
});

关键说明

  • Manifest V3必须声明manifest_version: 3
  • host_permissions需包含所有涉及的域名(原URL域名和目标URL域名)
  • resourceTypes对应原代码中的types参数,指定需要重定向的请求类型
  • 正则表达式中的反斜杠需双重转义(JSON文件中)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 07:54:20