jiacai2050 / gooreplacer

⚡️⚡️A browser extension to modify HTTP requests :-)
https://jiacai2050.github.io/gooreplacer/
1.23k stars 175 forks source link

正则规则 重定向目标有误 #111

Closed Lehmaning closed 2 years ago

Lehmaning commented 2 years ago

重定向规则:

{
      "src": "^https:\/\/links\\.jianshu\\.com\/go\\?to=http(s)?%3A%2F%2F(.*)(?:%2F)?",
      "kind": "regexp",
      "dst": "http$1://$2",
      "enable": true
}

测试 URL:https://www.jianshu.com/go-wild?ac=2&url=http%3A%2F%2Fsyncthing.net%2F 在沙盒中测试会显示 URL 被错误地重定向到 http$1://$2,而非 https://syncthing.net,结果浏览器并没有跳转。

目前看来问题应该出在 ? 这个匹配字符上,如果把规则改成 https:\/\/www\.jianshu\.com\/go-wild\?ac=\d\&url=https%3A%2F%2F(.*)(:?%2F)?,把重定向目标改为 https://$1,测试结果则是正常的 https://syncthing.net,浏览器也会跳转到相应网址。

jiacai2050 commented 2 years ago

在浏览器 console 里测试了下,

re = new RegExp('^https:\/\/www\\.jianshu\\.com\/go-wild\\?to=http(s)?%3A%2F%2F(.*)(?:%2F)?')
re.exec('https://www.jianshu.com/go-wild?to=http%3A%2F%2Fsyncthing.net%2F')

返回了

0: "https://www.jianshu.com/go-wild?to=http%3A%2F%2Fsyncthing.net%2F"
1: undefined
2: "syncthing.net%2F"

貌似 http(s)? 就是不会进 group,正则现在没有做什么处理,就是用的 JS 原生的功能,你自己再测试下吧。

Lehmaning commented 2 years ago

@jiacai2050 这样,我突然想到解决办法了,把问号放到括号里面就行了,返回的结果是正常的😂😂