Trello link replacer

Function to replace text matching (c/ID) with a link

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Trello link replacer
// @namespace    http://tampermonkey.net/
// @match        https://phabricator.clockwise.info/*
// @description  Function to replace text matching (c/ID) with a link
// @version 0.0.1.20241216091059
// ==/UserScript==

// Function to replace text matching (c/ID) with a link
setTimeout(() => {
    const replaceTrelloLinks = () => {
        // Regex to match (c/ID) pattern
        const regex = /\(c\/([a-zA-Z0-9]+)\)/g;

        // Recursive function to process text nodes
        const processNode = (node) => {
            if (node.nodeType === Node.TEXT_NODE) {
                const matches = [...node.textContent.matchAll(regex)];

                if (matches.length > 0) {
                    const parent = node.parentNode;
                    const fragments = [];
                    let lastIndex = 0;

                    matches.forEach((match) => {
                        const [fullMatch, id] = match;
                        const startIndex = match.index;

                        // Text before the match
                        if (startIndex > lastIndex) {
                            fragments.push(document.createTextNode(node.textContent.slice(lastIndex, startIndex)));
                        }

                        // Create the link
                        const link = document.createElement("a");
                        link.href = `https://trello.com/c/${id}`;
                        link.textContent = fullMatch;
                        fragments.push(link);

                        lastIndex = startIndex + fullMatch.length;
                    });

                    // Text after the last match
                    if (lastIndex < node.textContent.length) {
                        fragments.push(document.createTextNode(node.textContent.slice(lastIndex)));
                    }

                    // Replace the original text node with the new content
                    fragments.forEach((fragment) => parent.insertBefore(fragment, node));
                    parent.removeChild(node);
                }
            } else if (node.nodeType === Node.ELEMENT_NODE) {
                // Avoid replacing text inside <a> tags
                if (node.tagName.toLowerCase() !== "a") {
                    Array.from(node.childNodes).forEach(processNode);
                }
            }
        };

        // Start processing the body of the document
        Array.from(document.body.childNodes).forEach(processNode);
    };

    replaceTrelloLinks();
}, 500);