在chrome扩展manifest v3中,如何在重定向url时只更改参数键名而仍然使用原始值?

vsaztqbk  于 2022-12-06  发布在  Go
关注(0)|答案(2)|浏览(367)

我在rules.json中使用了addOrReplaceParams,但它似乎只能添加或替换键-值对,但我只想修改键名,有什么方法可以做到吗?例如,我想将参数名postID修改为po. https://www.blogger.com/comment/frame/5235590154125226279?postID=9028051665011570908&hl=zh-CN&blogspotRpcToken=942236&parentID=7864247129676677521#。这是我的rules.json,它重定向得很好,但我不知道如何获得原始url postID的值9028051665011570908

[{
  "id": 1,
  "priority": 1,
  "action": {
    "type": "redirect",
    "redirect": {
      "transform": {
        "queryTransform": {
          "removeParams": ["postID"],
          "addOrReplaceParams": [
            {
              "key": "po",
              "value": how to gain postID's value
            }
          ]
        }
      }
    }
  },
  "condition": {
    "urlFilter": "blogger.com/comment/frame",
    "resourceTypes": ["main_frame"]
  }
}]

我已经阅读了下面的文档,但我找不到任何解决方案3-72-0-dot-chrome-apps-doc.appspot.com developer.chrome.com

ibrsph3r

ibrsph3r1#

没有这样的操作,所以建议在https://crbug.com上实现它。
解决方法是使用regexFilter + regexSubstitution。
它通常比较慢,但对于像这样的简单表达式来说就不会慢很多,特别是当我们指定requestDomains来限制规则时。

[{
  "id": 1,
  "action": {
    "type": "redirect",
    "redirect": {
      "regexSubstitution": "\\1po="
    }
  },
  "condition": {
    "requestDomains": ["blogger.com"],
    "regexFilter": "(/comment/frame[^#]*?[?&])postID=",
    "resourceTypes": ["main_frame"]
  }
}]

要测试表达式,可以使用devtools控制台:

"https://www.blogger.com/comment/frame?postID=123"
  .replace(RegExp("(/comment/frame[^#]*?[?&])postID="),
     "\\1po=".replace(/\\(?=\d)/g, '$'))
fafcakar

fafcakar2#

这是我的rules.json

[{
"id": 1,
"priority": 1,
"action": {
"type": "redirect",
"redirect": {
"transform": {
"queryTransform": {
"removeParams": ["postID"],
"addOrReplaceParams": [
{
"key": "po",
"value":  how to gain postID's value 9028051665011570908
}
]
}
}
}
},
"condition": {
"urlFilter": "blogger.com/comment/frame",
"resourceTypes": ["main_frame"]
}
}]

相关问题