使用 declarativeNetRequest 进行动态重定向

tob*_*b88 5 google-chrome google-chrome-extension chrome-extension-manifest-v3

我想使用 declarativeNetRequest 将所有 URL 从域重定向到以发起者 URL 作为编码参数的 URL,例如

http://www.google.com --> http://www.newsite.com?url=http%3A%2F%2Fwww.google.com
http://www.google.com?param1=true --> http://www.newsite.com?url=http%3A%2F%2Fwww.google.com%3Fparam1%3Dtrue
Run Code Online (Sandbox Code Playgroud)

就像是...

  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "url": `http://www.newsite.com?url=${encodeURIComponent(url)}`
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

这可以吗?如果没有,还有其他方法可以实现这一目标吗?

tob*_*b88 2

[
  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "regexSubstitution": "http://www.newsite.com?url=\\0"
      }
    },
    "condition": {
      "regexFilter": "^https://www.google.com(.*)",
      "resourceTypes": ["main_frame"]
    }
  }
]
Run Code Online (Sandbox Code Playgroud)

它不会对其进行 URL 编码,但我发现只要参数位于末尾,这并不是真正的问题。还要小心地将其限制在正则表达式中的特定站点,否则您可能会陷入无限循环。