UUID Link Completer

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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();
    }
})();