Discord SetName Replacer

Replace words in Discord messages (e.g., "ah" -> "ASC")

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         Discord SetName Replacer
// @namespace    http://tampermonkey.net/
// @version      0.3.1
// @description  Replace words in Discord messages (e.g., "ah" -> "ASC")
// @match        https://discord.com/*
// @match        https://*.discord.com/*
// @grant        none
// @license      MIT
// ==/UserScript==
(function() {
    'use strict';

    // Configuration - in must be lowercase
    const translations = [
        {in: ["cr"], out: "CRI"},
        {in: ["po"], out: "POR"},
        {in: ["ah"], out: "ASC"},
        {in: ["pf", "phf"], out: "PFL"},
        {in: ["wf"], out: "WHT"},
        {in: ["bbl"], out: "BLK"},
        {in: ["dr"], out: "DRI"},
        {in: ["jt"], out: "JTG"},
        {in: ["ss"], out: "SSP"},
        {in: ["sc"], out: "SCR"},
        {in: ["sf"], out: "SFA"},
        {in: ["tm"], out: "TWM"},
        {in: ["tf"], out: "TEF"},
        {in: ["pf"], out: "PAF"},
        {in: ["pr"], out: "PAR"},
        {in: ["obf"], out: "OBF"},
        {in: ["pe"], out: "PAL"},
        {in: ["bb"], out: "BoosterBox"},
        {in: ["bbs"], out: "BoosterBoxes"},
        {in: ["prissy", "pris", "prizzy", "prizz", "priz"], out: "PRE"}
    ];

    // Process text with word boundary checks
    function replaceWords(text) {
        let result = text;
        translations.forEach(({in: inputs, out: output}) => {
            inputs.forEach(word => {
                // Escape special regex characters
                const escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
                // Create case-insensitive regex with word boundaries
                const regex = new RegExp(`\\b${escapedWord}\\b`, 'gi');
                result = result.replace(regex, output);
            });
        });
        return result;
    }

    // Replace text in text nodes only
    function replaceInElement(el) {
        el.childNodes.forEach(node => {
            if (node.nodeType === Node.TEXT_NODE) {
                // Only replace in text nodes
                const originalText = node.textContent;
                const newText = replaceWords(originalText);
                if (originalText !== newText) {
                    node.textContent = newText;
                }
            } else if (node.nodeType === Node.ELEMENT_NODE && !['CODE', 'PRE'].includes(node.tagName)) {
                // Recursively process child elements (but skip code/pre)
                replaceInElement(node);
            }
        });
    }

    // Mutation observer to watch for new messages
    const observer = new MutationObserver(() => {
        // Find message elements and replace text
        document.querySelectorAll('[id^=message-content-]').forEach(el => {
            // Skip if already processed
            if (el.dataset.replaced) return;

            // Replace text while preserving HTML elements
            replaceInElement(el);

            // Mark as processed
            el.dataset.replaced = 'true';
        });
    });


    observer.observe(document.body, { childList: true, subtree: true });
    console.log('Ultimate Discord SetName Replacer');
})();