Convert Any links to Clickable Links

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

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Convert Any links to Clickable Links
// @namespace    kdroidwin.hatenablog.com
// @version      1.6
// @description  URLやドメイン名(先頭の "h" が抜けている場合も含む)をクリック可能なリンクに変換する。ただし、入力中のフォーム領域は除外する。
// @author       Kdroidwin
// @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,}(?:\/[^\s]*)?)/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 matches = node.nodeValue.match(pattern);
            if (!matches) return;

            let lastIndex = 0;
            matches.forEach(match => {
                const matchIndex = node.nodeValue.indexOf(match, lastIndex);
                if (matchIndex > lastIndex) {
                    fragment.appendChild(document.createTextNode(node.nodeValue.substring(lastIndex, matchIndex)));
                }

                const link = document.createElement('a');
                if (/^ttps:\/\//i.test(match)) {
                    link.href = 'h' + match;
                } else if (/^https?:\/\//i.test(match)) {
                    link.href = match;
                } else {
                    link.href = 'https://' + match;
                }
                link.textContent = match;
                link.target = '_blank';
                fragment.appendChild(link);

                lastIndex = matchIndex + match.length;
            });

            if (lastIndex < node.nodeValue.length) {
                fragment.appendChild(document.createTextNode(node.nodeValue.substring(lastIndex)));
            }

            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 の変更を監視(変更があったら 500ms 後に実行)
    const observer = new MutationObserver(debounce(mutations => {
        for (const mutation of mutations) {
            for (const node of mutation.addedNodes) {
                if (node.nodeType === 1) {
                    convertTextToLinks(node);
                }
            }
        }
    }, 500));

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

})();