替换用户脚本中的故障:在结果前面添加当前 URL

问题描述 投票:0回答:1

我正在尝试使用用户脚本删除网站上传出链接中不需要的部分,方法是将其替换为任何内容。

jQuery(document).on("mouseenter", "a", function(event) {
    jQuery(this).prop("href", jQuery(this).prop("href").replace(/https;\/\/steamcommunity\.com\/linkfilter\/\?u=/, ""));
});

所述部分 (

https;//steamcommunity.com/linkfilter/?u=
) 被删除,但随后没有替换为任何内容,而是替换为当前 URL。

示例:
我在这个网址:

https;//store.steampowered.com/app/1091500/Cyberpunk_2077/

有一个链接(“访问网站”):
https;//steamcommunity.com/linkfilter/?u=https;//www.cyberpunk.net

脚本应将其更改为:
https;//www.cyberpunk.net

脚本确实将其更改为:
https;//store.steampowered.com/app/1091500/Cyberpunk_2077/https;//www.cyberpunk.net

如果我只是在浏览器控制台中运行该函数,它就会按预期工作。
输入:

"https;//steamcommunity.com/linkfilter/?u=https;//www.cyberpunk.net".replace(/https;\/\/steamcommunity\.com\/linkfilter\/\?u=/, "");

输出:
"https;//www.cyberpunk.net"

也不适用于其他替代品。

jQuery(document).on("mouseenter", "a", function(event) {
    jQuery(this).prop("href", jQuery(this).prop("href").replace(/https;\/\/steamcommunity\.com\/linkfilter\/\?u=/, "TEST"));
});

应该是:

TESThttps;//www.cyberpunk.net

是:
https;//store.steampowered.com/app/1091500/Cyberpunk_2077/TESThttps;//www.cyberpunk.net

必须将 https: 替换为 https;因为问题被标记为“垃圾邮件”...

javascript replace userscripts
1个回答
0
投票

这不起作用,因为生成的字符串不是以有效字符串开头(即

http://
https://
),因此浏览器假设链接是相对的并在当前 url 前面添加。看来该方案的冒号和斜杠实际上是编码字符,也需要替换。请参阅处理解码的小提琴,生成的字符串应该通过方案测试,并且一切都应该按预期工作。

jsfiddle.net/fw4tjvL7

© www.soinside.com 2019 - 2024. All rights reserved.