UUID Link Completer

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
    }
})();