Convert Any links to Clickable Links

URLやドメイン名(先頭の"h"が抜けている場合も含む)をクリック可能なリンクに変換する

当前为 2025-02-23 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Convert Any links to Clickable Links 
// @namespace    kdroidwin.hatenablog.com/
// @version      1.2
// @description  URLやドメイン名(先頭の"h"が抜けている場合も含む)をクリック可能なリンクに変換する
// @author       Kdroidwin
// @match        *://*/* 
// @grant        none
// @license MIT 
// ==/UserScript==

(function() {
    'use strict';

    function convertTextToLinks() {
        // 先頭の "h" が抜けているケースも考慮してマッチ
        const pattern = /(h?ttps?:\/\/[^\s]+|(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})/g; 
        
        const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, {
            acceptNode: node => {
                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)) {
                    // ドメインのみの場合は「https://」を補完
                    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);
        });
    }

    convertTextToLinks();
    new MutationObserver(convertTextToLinks).observe(document.body, { childList: true, subtree: true });
})();