文本识别为超链接

Convert URLs in text nodes to hyperlinks using regular expressions

当前为 2024-10-05 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                Convert Text to Hyperlink
// @name:zh-CN          文本识别为超链接
// @namespace           https://github.com/KPI0/tampermonkey
// @version             1.4
// @description         Convert URLs in text nodes to hyperlinks using regular expressions
// @description:zh-cn   通过正则表达式将文本中的链接转换为超链接
// @author              KPI0
// @match               *://*/*
// @icon                data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyB0PSIxNzI4MTE3NTk1Mjc5IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjE2MjAiIHhtbG5zOnhsaW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gMCAwIDAgMzY4IDQ3OC44OGwtMS40NCAxLjI4YTQ5LjQ0IDQ5LjQ0IDAgMCAwLTQgMy44NGwtMTAxLjI4IDEwMS4yOGExMjQuMTYgMTI0LjE2IDAgMCAwIDAgMTc2bDEuOTIgMS45MmExMjQuMTYgMTI0LjE2IDAgMCAwIDE3NiAwbDUxLjY4LTUxLjY4YTQ5LjQ0IDQ5LjQ0IDAgMCAwIDMuODQtNGwyMC0yNC45NiA1MC4wOCA0MC0yMC44IDI1LjEyYTEwOC4xNiAxMDguMTYgMCAwIDEtOC42NCA5LjI4bC01MS42OCA1MS42OEExODcuMzYgMTg3LjM2IDAgMCAxIDM1MC43MiA4NjR6IiBmaWxsPSIjMzMzMzMzIiBwLWlkPSIxNjIyIj48L3BhdGg+PC9zdmc+
// @grant               none
// @license             MIT
// ==/UserScript==

(function () {
    'use strict';

    // Regular expression to match URLs starting with http or https
    const urlRegex = /(http:\/\/[^\s]+|https:\/\/[^\s]+)/g;

    // Get the recognized count from local storage
    function getRecognitionCount() {
        return parseInt(localStorage.getItem('recognition_count') || '0', 10);
    }

    // Set the recognized count in local storage
    function setRecognitionCount(count) {
        localStorage.setItem('recognition_count', count);
    }

    // Increment the recognized count and save it to local storage
    function incrementRecognitionCount() {
        const count = getRecognitionCount() + 1;
        setRecognitionCount(count);
    }

    function convertTextLinksToHyperlinks(node) {
        if (node.nodeType === Node.TEXT_NODE) {
            const text = node.nodeValue;
            const matches = text.match(urlRegex);

            if (matches) {
                const span = document.createElement('span');
                let lastIndex = 0;

                matches.forEach((match) => {
                    const matchIndex = text.indexOf(match, lastIndex);

                    // Append normal text
                    if (matchIndex > lastIndex) {
                        span.appendChild(document.createTextNode(text.substring(lastIndex, matchIndex)));
                    }

                    // Create hyperlink element
                    const link = document.createElement('a');
                    link.href = match;
                    link.target = '_blank'; // Open in a new tab
                    link.textContent = match;
                    span.appendChild(link);

                    lastIndex = matchIndex + match.length;
                });

                // Append remaining normal text
                if (lastIndex < text.length) {
                    span.appendChild(document.createTextNode(text.substring(lastIndex)));
                }

                node.parentNode.replaceChild(span, node);

                // Increment recognition count once a hyperlink is created
                incrementRecognitionCount();
            }
        } else if (node.nodeType === Node.ELEMENT_NODE) {
            // Skip certain tags that should not contain links or be processed
            const skipTags = ['A', 'SCRIPT', 'STYLE', 'IMG', 'VIDEO', 'AUDIO', 'PICTURE', 'IFRAME', 'BUTTON', 'CANVAS', 'NAV', 'HEADER', 'FOOTER'];
            if (!skipTags.includes(node.tagName)) {
                // Recursively process child nodes
                for (let child of Array.from(node.childNodes)) {
                    convertTextLinksToHyperlinks(child);
                }
            }
        }
    }

    // Register a menu command to show the recognition count
    function registerMenuCommand() {
        GM_registerMenuCommand('👀 Recognized: ' + getRecognitionCount() + ' times', () => {
            setRecognitionCount(0);
            alert('Recognition count has been reset.');
            // Update the menu item after reset
            registerMenuCommand();
        });
    }

    // Execute after the page has fully loaded
    window.addEventListener('load', function () {
        const mainContentSelectors = ['#main', '.content', '.article', '#content', '.post', '.entry']; // Common main content containers
        mainContentSelectors.forEach(selector => {
            const container = document.querySelector(selector);
            if (container) {
                convertTextLinksToHyperlinks(container);
            }
        });
        registerMenuCommand();
    });

})();