Discord SetName Replacer

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

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