UUID Link Completer

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==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();
    }
})();