簡體   English   中英

用新鏈接替換網頁中的所有鏈接

[英]replace all links in webpage with new link

我需要用新鏈接替換這種風格的所有鏈接。 我正在禁食谷歌驅動鏈接的下載過程。

https://drive.google.com/file/d/1cCIWJMBOX-NvzYVTQajaVo5lNknYA2E8/view?usp=sharing

我的想法是從鏈接中獲取“1cCIWJMBOX-NvzYVTQajaVo5lNknYA2E8”並創建新鏈接,即直接下載鏈接。

為此我寫了正則表達式

new RegExp('https://drive.google.com/file/d/' + "(.*)" + '/view')

它運作良好。

但我不明白如何制作可直接下載的重復鏈接。

 new RegExp('https://drive.google.com/file/d/' + "(.*)" + '/view');
 <a href="https://drive.google.com/file/d/1cCIWJMBOX-NvzYVTQajaVo5lNknYA2E8/view?usp=sharing">https://drive.google.com/file/d/1cCIWJMBOX-NvzYVTQajaVo5lNknYA2E8/view?usp=sharing</a>

結果我需要的是我在下面寫的兩個鏈接。 所以我可以單擊任何所需的鏈接並下載文件。 這些鏈接在我的網站頁面中,因此我必須在文檔中搜索正則表達式匹配並替換鏈接以獲得以下結果

<a href="https://drive.google.com/file/d/1cCIWJMBOX-NvzYVTQajaVo5lNknYA2E8/view?usp=sharing">https://drive.google.com/file/d/1cCIWJMBOX-NvzYVTQajaVo5lNknYA2E8/view?usp=sharing</a>

<a href="https://drive.google.com/uc?id=NvzYVTQajaVo5lNknYA2E8&export=download">direct download</a>

如果我理解正確,你需要這樣的東西嗎?

 const links = document.querySelectorAll("a"); for (const link of links) { if (link.href.indexOf("drive.google.com")>-1){ var href = link.href; var li = href.split("/"); //li[5] = li[5].split("-")[1]; //console.log(li[5]); var createA = document.createElement('a'); var newline = document.createElement('br'); var createAText = document.createTextNode("direct download"); createA.setAttribute('href', "https://drive.google.com/uc?id=" + li[5] + "&export=download"); link.appendChild(newline); createA.appendChild(createAText); link.appendChild(createA); } } const links2 = document.querySelectorAll("a"); for (const link2 of links2) { if (link2.href.indexOf("download") >= 0) { console.log(link2.href); } }
 <div>link 1 <br> <a href="https://drive.google.com/file/d/1cCIWJMBOX-NvzYVTQajaVo5lNknYA2E8/view?usp=sharing">https://drive.google.com/file/d/1cCIWJMBOX-NvzYVTQajaVo5lNknYA2E8/view?usp=sharing</a> <br> </div> <br> <div>My baby yoda pic link 2 test<br> <a href="https://drive.google.com/file/d/1sRXR9-UgiXP-vKyTznrK8L_07zQaNAuq/view?usp=sharing">https://drive.google.com/file/d/1sRXR9-UgiXP-vKyTznrK8L_07zQaNAuq/view?usp=sharing</a> <br> </div>

或者,在更短的版本中,以下內容將使您獲得相同的結果:

 [...document.querySelectorAll('a[href*="drive.google.com"]')].forEach(a=>a.outerHTML+=` <br><a href="https://drive.google.com/uc?id=${a.href.match(/file\/d\/(.*)\/view/)[1]}&export=download">direct download</a>`)
 <div>link 1 <br> <a href="https://drive.google.com/file/d/1cCIWJMBOX-NvzYVTQajaVo5lNknYA2E8/view?usp=sharing">https://drive.google.com/file/d/1cCIWJMBOX-NvzYVTQajaVo5lNknYA2E8/view?usp=sharing</a> <br> </div> <br> <div>My baby yoda pic link 2 test<br> <a href="https://drive.google.com/file/d/1sRXR9-UgiXP-vKyTznrK8L_07zQaNAuq/view?usp=sharing">https://drive.google.com/file/d/1sRXR9-UgiXP-vKyTznrK8L_07zQaNAuq/view?usp=sharing</a> <br> </div>

以上兩個答案很有幫助。 我修改了答案並想出了這個。

 const links = document.querySelectorAll("a"); for (const link of links) { var href = link.href; var li = href.split("/"); var y = href.match(new RegExp('https://drive.google.com/file/d/' + "(.*)" + '/view')); var href2; if(y){ href2='https://drive.google.com/uc?id=' +y[1]+'&export=download'; console.log(href2); var createA = document.createElement('a'); var newline = document.createElement('br'); var createAText = document.createTextNode("direct download"); createA.setAttribute('href', href2); link.appendChild(newline); createA.appendChild(createAText); link.appendChild(createA); } } const links2 = document.querySelectorAll("a"); for (const link2 of links2) { if (link2.href.indexOf("download") >= 0) { //console.log(link2.href); } }

暫無
暫無

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

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