[英]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.