oneNorth7 / LinkHelper

link-help-userscript
112 stars 11 forks source link

在url中含有&这样的html转义字符时不能正常识别 #6

Closed neoblackxt closed 1 year ago

neoblackxt commented 2 years ago

我遇到了一个这样的链接 https://www.example.com/adredir.php?id=2&url=https%3A%2F%2Fwww.example.com%2Fforums.php%3Faction%3Dviewforum%26amp%3Bforumid%3D8

在执行到链接助手.user.js#L2072 时 result = ['https://www.example.com/adredir.php?id=2&url=https://www.example.com/forums.php?action=viewforum&amp', 'https://www.example.com/adredir.php?id=2&url=', 'https://www.example.com/forums.php?action=viewforum&amp']

实际上真实的url是 https://www.example.com/forums.php?action=viewforum&forumid=8

我这样修改了一下,暂时解决了问题 result = reg.exec(decodeURIComponent(a.href).replace(/&/g, "&"));

得到 result = ['https://www.example.com/adredir.php?id=2&url=https://www.example.com/forums.php?action=viewforum&forumid=8', 'https://www.example.com/adredir.php?id=2&url=', 'https://www.example.com/forums.php?action=viewforum&forumid=8']

oneNorth7 commented 2 years ago

链接中包含HTML实体字符的情况我这暂时没遇到,针对所有实体字符的话可以采用以下代码:

result = reg.exec($("<span>" + decodeURIComponent(a.href) + "</span>").text());

找不到直接转换的方法,只能借助于html元素对象后取文本值。 可能没必要针对所有字符,毕竟两边的合法字符集不太一样,只是想到了就写出来参考参考。