您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Convert plain text URLs and domain names into clickable links
当前为
- // ==UserScript==
- // @name Convert Any links to Clickable Links
- // @namespace kdroidwin.hatenablog.com/
- // @version 1.0
- // @description Convert plain text URLs and domain names into clickable links
- // @author Kdroidwin
- // @match *://*/*
- // @grant none
- // @license MIT
- // ==/UserScript==
- (function() { 'use strict';
- function convertTextToLinks() {
- const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, {
- acceptNode: node => {
- if (node.parentNode && node.parentNode.tagName !== 'A' && /(https?:\/\/[^\s]+|(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})/.test(node.nodeValue)) {
- return NodeFilter.FILTER_ACCEPT;
- }
- return NodeFilter.FILTER_REJECT;
- }
- });
- let nodes = [];
- while (walker.nextNode()) {
- nodes.push(walker.currentNode);
- }
- nodes.forEach(node => {
- const fragment = document.createDocumentFragment();
- const parts = node.nodeValue.split(/(https?:\/\/[^\s]+|(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})/g);
- parts.forEach(part => {
- if (/https?:\/\//.test(part)) {
- const link = document.createElement('a');
- link.href = part;
- link.textContent = part;
- link.target = '_blank';
- fragment.appendChild(link);
- } else if (/(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}/.test(part)) {
- const link = document.createElement('a');
- link.href = 'https://' + part;
- link.textContent = part;
- link.target = '_blank';
- fragment.appendChild(link);
- } else {
- fragment.appendChild(document.createTextNode(part));
- }
- });
- node.parentNode.replaceChild(fragment, node);
- });
- }
- convertTextToLinks();
- new MutationObserver(convertTextToLinks).observe(document.body, { childList: true, subtree: true });
- })();