[英]Use Tampermonkey to delinkify tel: links
我想使用Tampermonkey從我們的Intranet網頁上的電話號碼中刪除<a href="tel:
:。
我已經安裝了Tampermonkey並結合了以下腳本
// ==UserScript==
// @name Localhost Tel: link remover
// @author Dan
// @match *://localhost/*
// @version 2019.08.15
// @grant none
// @run-at document-start
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
console.log('Started Localhost Tel: killer...');
// ==/UserScript==
var anchors = document.getElementsByTagName('tel');
for (var i = 0; i < anchors.length; i++) {
anchors[i].href = anchors[i].href.split("?")[0];
}
但是我走不通了! 我已經嘗試使用Google搜索來進行“取消鏈接”和“條形鏈接”,但是即使我可以找到合適的人,我還是特別希望剝離tel:鏈接而不僅僅是所有鏈接。
謝謝!
編輯:
// ==UserScript==
// @name tel: remover
// @match *redacted*
// @version 2019.08.15
// @run-at document-end
// ==/UserScript==
const linksTel = [...document.querySelectorAll("a")].filter((link)=>link.href.startsWith("tel:"));
for(let linkTel of linksTel) {
const parent = linkTel.parentNode;
// Trim will remove extra spaces at the beginning and end
const newTextNode = new Text(linkTel.innerText.trim());
parent.replaceChild(newTextNode, linkTel);
}
alert("check");
您正在尋找的標記名稱是a
在<a ... >
因此使用:
const links = document.querySelectorAll("a");
但這為您提供了所有鏈接。 因此,您一開始只需要過濾那些具有tel:
的地址。 有一個實用程序方法.filter
,但它僅適用於數組,而不適用於文檔查詢返回的HTMLElementCollection
。 您可以像這樣展開數組中的集合:
const linksArray = [...links];
現在過濾器:
const linksTel = linksArray.filter((link)=>link.href.startsWith("tel:"));
現在,您擁有所有以tel
開頭的鏈接。 要用純文本節點替換,您可以執行以下操作:
for(let linkTel of linksTel) {
const parent = linkTel.parentNode;
// Trim will remove extra spaces at the beginning and end
const newTextNode = new Text(linkTel.innerText.trim());
parent.replaceChild(newTextNode, linkTel);
}
應該是這樣。
還請注意這一點! 您使用@ run-at document-start 。 那不是你想要的。 您需要使用@run-at document-end
。 在document-start
,尚未加載HTML,因此您無法對其進行操作。
一起查看以下內容:
const linksTel = [...document.querySelectorAll("a")].filter((link)=>link.href.startsWith("tel:")); for(let linkTel of linksTel) { const parent = linkTel.parentNode; // Trim will remove extra spaces at the beginning and end const newTextNode = new Text(linkTel.innerText.trim()); parent.replaceChild(newTextNode, linkTel); }
<a href="tel:123456489">123456489</a><br /> <a href="/test">Tet page</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.