簡體   English   中英

Greasemonkey 從沒有正則表達式的 url 中刪除字符串

[英]Greasemonkey remove string from url without regex

如果我有一個網頁,每個 URL 上都有推薦字符串,我想用油脂猴刪除它,我該怎么做?

例如:

<a href="https://website.refer?url=www.google.com"> The link </a>

鏈接

變成:

<a href="www.google.com"> The link </a>

鏈接

如何刪除 URL 的“/website.refer?url=”部分?

我試過這樣的事情:

document.getElementsByTagName("a").replace('/website.refer?url=','');

然而,我嘗試的任何東西似乎都不起作用參見小提琴:這里

請不要使用正則表達式。 謝謝

遍歷每個<a> ,通過 URLSearchParameters 獲取url參數,並將其分配給<a>href

 document.querySelectorAll('a').forEach((a) => { const params = new URLSearchParams(a.href.split('?')[1]); a.href = '//' + params.get('url'); });
 <a href="https://website.refer.com?url=www.google.com"> The link </a>

document.getElementsByTagName將返回實時HTMLCollection並用strings替換作品。

相反,您可以獲取每個a標簽的href屬性並對其使用替換。

 document.querySelectorAll('a').forEach(a => { const newHref = a.getAttribute('href').replace('//website.refer?url=', ''); a.href = newHref; });
 <a href="https://website.refer?url=www.google.com"> Link 1 </a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM