Convert Any links to Clickable Links

URLやドメイン名(先頭の "h" が抜けている場合も含む)をクリック可能なリンクに変換する。ただし、入力中のフォーム領域は除外する。

目前為 2025-03-11 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Convert Any links to Clickable Links
// @namespace    kdroidwin.hatenablog.com
// @version      1.5
// @description  URLやドメイン名(先頭の "h" が抜けている場合も含む)をクリック可能なリンクに変換する。ただし、入力中のフォーム領域は除外する。
// @author       K
// @match        *://*/*
// @exclude      *://github.com/*
// @exclude      *://chat.openai.com/*
// @exclude      *://blog.hatena.ne.jp/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function isEditable(node) {
        if (!node) return false;
        if (node.nodeName === 'INPUT' || node.nodeName === 'TEXTAREA') return true;
        if (node.hasAttribute && node.hasAttribute('contenteditable') && node.getAttribute('contenteditable') !== 'false') {
            return true;
        }
        return false;
    }

    function convertTextToLinks(root = document.body) {
        const pattern = /(h?ttps?:\/\/[^\s]+|(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})/g;
        const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
            acceptNode: node => {
                let current = node.parentNode;
                while (current) {
                    if (isEditable(current)) return NodeFilter.FILTER_REJECT;
                    current = current.parentNode;
                }
                if (node.parentNode && node.parentNode.tagName !== 'A' && pattern.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(pattern);
            parts.forEach(part => {
                const text = part.trim();
                if (/^ttps:\/\//i.test(text)) {
                    const link = document.createElement('a');
                    link.href = 'h' + text;
                    link.textContent = text;
                    link.target = '_blank';
                    fragment.appendChild(link);
                } else if (/^https?:\/\//i.test(text)) {
                    const link = document.createElement('a');
                    link.href = text;
                    link.textContent = text;
                    link.target = '_blank';
                    fragment.appendChild(link);
                } else if (/(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}/.test(text)) {
                    const link = document.createElement('a');
                    link.href = 'https://' + text;
                    link.textContent = text;
                    link.target = '_blank';
                    fragment.appendChild(link);
                } else {
                    fragment.appendChild(document.createTextNode(part));
                }
            });
            node.parentNode.replaceChild(fragment, node);
        });
    }

    function debounce(func, wait) {
        let timeout;
        return function(...args) {
            clearTimeout(timeout);
            timeout = setTimeout(() => func.apply(this, args), wait);
        };
    }

    // 初回実行
    convertTextToLinks();

    // DOM の変更を監視(変更があったら 300ms 後に実行)
    const observer = new MutationObserver(debounce(mutations => {
        for (const mutation of mutations) {
            for (const node of mutation.addedNodes) {
                if (node.nodeType === 1) {
                    convertTextToLinks(node);
                }
            }
        }
    }, 300));

    observer.observe(document.body, { childList: true, subtree: true });

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址