UUID Link Completer

识别并补全网页中的UUID字符串为完整可点击链接

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         UUID Link Completer
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  识别并补全网页中的UUID字符串为完整可点击链接
// @author       You
// @match        https://tieba.baidu.com/f?kw=grok
// @match        https://tieba.baidu.com/p/*
// @match        https://tieba.baidu.com/f?*kw=grok*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const UUID_PATTERN = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/gi;

    function method0(text) {
        return text.replace(UUID_PATTERN, (uuid) => {
            const link = document.createElement('a');
            link.href = `https://grok.com/imagine/post/${uuid}`;
            link.target = '_blank';
            link.rel = 'noopener noreferrer';
            link.textContent = '点我跳转';
            return link.outerHTML;
        });
    }

    function method1(element) {
        let hasSmileyElements = false;
        const nodesToRemove = [];
        
        element.childNodes.forEach(node => {
            if (node.nodeType === Node.ELEMENT_NODE && node.tagName === 'IMG') {
                const className = node.getAttribute('class');
                if (className === 'BDE_Smiley') {
                    nodesToRemove.push(node);
                    hasSmileyElements = true;
                }
            }
        });
        
        nodesToRemove.forEach(node => {
            node.remove();
        });
        
        if (hasSmileyElements) {
            let prevTextNode = null;
            const nodesToRemove = [];
            
            element.childNodes.forEach(node => {
                if (node.nodeType === Node.TEXT_NODE) {
                    if (prevTextNode) {
                        prevTextNode.textContent += node.textContent;
                        nodesToRemove.push(node);
                    } else {
                        prevTextNode = node;
                    }
                } else {
                    prevTextNode = null;
                }
            });
            
            nodesToRemove.forEach(node => {
                node.remove();
            });
        }
    }

    function method2(text) {
        return text.replace(/[删哈里]/g, '');
    }

    function processTextNode(node) {
        if (node.nodeType === Node.TEXT_NODE) {
            let text = node.textContent;
            
            if (text.includes('-') && (text.match(/-/g) || []).length >= 4) {
                text = method2(text);
                node.textContent = text;
                
                if (UUID_PATTERN.test(text)) {
                    UUID_PATTERN.lastIndex = 0;
                    
                    const fragments = [];
                    let lastIndex = 0;
                    let match;
                    
                    while ((match = UUID_PATTERN.exec(text)) !== null) {
                        if (match.index > lastIndex) {
                            fragments.push(document.createTextNode(text.slice(lastIndex, match.index)));
                        }
                        
                        const link = document.createElement('a');
                        link.href = `https://grok.com/imagine/post/${match[0]}`;
                        link.target = '_blank';
                        link.rel = 'noopener noreferrer';
                        link.textContent = '点我跳转';
                        fragments.push(link);
                        
                        lastIndex = UUID_PATTERN.lastIndex;
                    }
                    
                    if (lastIndex < text.length) {
                        fragments.push(document.createTextNode(text.slice(lastIndex)));
                    }
                    
                    const wrapper = document.createElement('span');
                    fragments.forEach(f => wrapper.appendChild(f));
                    node.parentNode.replaceChild(wrapper, node);
                }
            }
        }
    }

    function processElement(element) {
        if (element.tagName === 'SCRIPT' || element.tagName === 'STYLE' || element.tagName === 'NOSCRIPT' || element.tagName === 'A') {
            return;
        }

        method1(element);
        
        const childNodes = Array.from(element.childNodes);
        childNodes.forEach(node => {
            if (node.nodeType === Node.TEXT_NODE) {
                processTextNode(node);
            } else if (node.nodeType === Node.ELEMENT_NODE) {
                processElement(node);
            }
        });
    }

    function init() {
        processElement(document.body);

        const observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                mutation.addedNodes.forEach((node) => {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        processElement(node);
                    } else if (node.nodeType === Node.TEXT_NODE) {
                        processTextNode(node);
                    }
                });
            });
        });

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

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();