简体   繁体   English

使用Tampermonkey取消链接电话:链接

[英]Use Tampermonkey to delinkify tel: links

I'd like to use Tampermonkey to strip the <a href="tel: from telephone numbers on a webpage on our intranet. 我想使用Tampermonkey从我们的Intranet网页上的电话号码中删除<a href="tel: :。

I've installed Tampermonkey and bodged the following script 我已经安装了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];
}

but I'm way out of my depth! 但是我走不通了! I've tried Googling for "delinkify" and "strip links" but even if I could find someone suitable, I'm specifically looking to strip tel: links not just all links. 我已经尝试使用Google搜索来进行“取消链接”和“条形链接”,但是即使我可以找到合适的人,我还是特别希望剥离tel:链接而不仅仅是所有链接。

Thanks! 谢谢!

EDIT: 编辑:

// ==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");

The tag name you're looking for is a as in <a ... > . 您正在寻找的标记名称是a<a ... > So use: 因此使用:

const links = document.querySelectorAll("a");

But that gives you ALL the links. 但这为您提供了所有链接。 So you will need to only filter for those that have tel: in the beginning. 因此,您一开始只需要过滤那些具有tel:的地址。 There's an utility method .filter , but it is only available for arrays, not HTMLElementCollection that is returned by document queries. 有一个实用程序方法.filter ,但它仅适用于数组,而不适用于文档查询返回的HTMLElementCollection You can unroll the collection in array like this: 您可以像这样展开数组中的集合:

const linksArray = [...links];

Now the filter: 现在过滤器:

const linksTel = linksArray.filter((link)=>link.href.startsWith("tel:"));

Now you have all the links that start with tel . 现在,您拥有所有以tel开头的链接。 To replace with just plain text node, you could do: 要用纯文本节点替换,您可以执行以下操作:

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);
}

That should be it. 应该是这样。

Also mind this! 还请注意这一点! You use @run-at document-start . 您使用@ run-at document-start That's not what you want. 那不是你想要的。 You need to use @run-at document-end . 您需要使用@run-at document-end At document-start , the HTML was not loaded yet, so you cannot manipulate it. document-start ,尚未加载HTML,因此您无法对其进行操作。

See it all together below: 一起查看以下内容:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM