Convert Any links to Clickable Links

Convert plain text URLs and domain names (even missing "h" for http/https) into clickable links

目前為 2025-02-23 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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.1
// @description  Convert plain text URLs and domain names (even missing "h" for http/https) into clickable links 
// @author       Kdroidwin
// @match        *://*/* 
// @grant        none
// @license MIT 
// ==/UserScript==

(function() { 'use strict';

function convertTextToLinks() {
    // 正規表現パターンを更新: "h" がない場合や "http(s)://" の場合も対象にする
    // この例では先頭が "h?ttps?://" を探すことで、"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 => {
            // この部分も正規表現を更新(元のコードとほぼ同じですが、patternを使用)
            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();
        // 改良:正規表現のキャプチャを利用して分割(splitはキャプチャも含むのでOK)
        const parts = node.nodeValue.split(pattern);
        
        parts.forEach(part => {
            // "ttps://" で始まる場合には先頭に"h"を補給
            if (/^ttps:\/\//i.test(part)) {
                const link = document.createElement('a');
                link.href = 'h' + part;
                link.textContent = part;
                link.target = '_blank';
                fragment.appendChild(link);
            // "http://" または "https://" で始まる場合
            } else if (/^https?:\/\//i.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 });
})();