Greasy Fork is available in English.

Emoji Replacer

Replace specified emojis with '卐' using a mutation observer

// ==UserScript==
// @name         Emoji Replacer
// @namespace    Violentmonkey Scripts
// @version      1.2
// @description  Replace specified emojis with '卐' using a mutation observer
// @author       A concerned citizen
// @match        *://*/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
'use strict';

const emojisToReplace = ['🍉', '🇵🇸','free palestine'];
const replacementChar = '卐';

function createReplacementSpan() {
    const newSpan = document.createElement('span');
    newSpan.textContent = replacementChar;
    newSpan.style.color = 'black';
    newSpan.style.fontWeight = '700';
    newSpan.style.display = 'inline-flex';
    newSpan.style.marginLeft = '4px';
  newSpan.style.padding = '2px';
    newSpan.style.transform = 'rotate(45deg)';
    return newSpan;
}

function replaceEmoji(node) {
    const regex = new RegExp(emojisToReplace.join('|'), 'gi');

    if (node.nodeType === Node.TEXT_NODE) {
        if (regex.test(node.nodeValue)) {
            console.log('Match found for emoji:', emojisToReplace);
            const newSpan = createReplacementSpan();
            node.parentNode.insertBefore(newSpan, node.nextSibling);
            node.nodeValue = node.nodeValue.replace(regex, '');
        }
    } else if (node.nodeType === Node.ELEMENT_NODE) {
        const emojiElements = node.querySelectorAll('[alt*="1F1F5-1F1F8"i], [src*="1F1F5-1F1F8"i], [alt*="palestin"i][alt*="emoji"i], [src*="palestin"i][alt*="emoji"i], [alt="🇵🇸"i], [alt*="palestin"i][alt*="flag"i]');
        emojiElements.forEach(element => {
            const newSpan = createReplacementSpan();
            element.parentNode.replaceChild(newSpan, element);
        });

        for (const childNode of node.childNodes) {
            replaceEmoji(childNode);
        }
    }
}

const observerConfig = {
    childList: true,
    subtree: true,
};

const observerCallback = function (mutationsList) {
    for (const mutation of mutationsList) {
        if (mutation.type === 'childList') {
            for (const addedNode of mutation.addedNodes) {
                replaceEmoji(addedNode);
            }
        }
    }
};

const observer = new MutationObserver(observerCallback);

observer.observe(document.body, observerConfig);

replaceEmoji(document.body);
})();