UUID Link Completer

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

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

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