μSlop

Replaces all occurences of "Microsoft" in text nodes and page titles with "Microslop".

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         μSlop
// @match        *://*/*
// @run-at       document-end
// @grant        none
// @description  Replaces all occurences of "Microsoft" in text nodes and page titles with "Microslop".
// @license      Public Domain
// @version      0.2
// @namespace https://greasyfork.org/users/1593905
// ==/UserScript==

(function() {
    'use strict';

    const TARGET = "microsoft";
    const REPLACEMENT_BASE = "microslop";
    const MAX_EXEC_TIME_MS = 50;
    const IGNORE_TAGS = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE'];

    function applyCasing(match, replacement) {
        if (!match || !replacement) return match;

        const firstChar = match[0];
        const isUpperFirst = firstChar === firstChar.toUpperCase() && firstChar !== firstChar.toLowerCase();

        const isAllUpper = match === match.toUpperCase();

        let result = "";

        if (isAllUpper) {
            result = replacement.toUpperCase();
        } else if (isUpperFirst) {
            result = replacement.charAt(0).toUpperCase() + replacement.slice(1).toLowerCase();
        } else {
            // All lowercase (or mixed that isn't title/upper)
            result = replacement.toLowerCase();
        }

        return result;
    }

    function processNode(node) {
        if (node.nodeType === Node.TEXT_NODE) {
            const text = node.nodeValue;
            if (!text.toLowerCase().includes(TARGET.toLowerCase())) return;

            const regex = new RegExp(
                TARGET.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
                'gi'
            );

            node.nodeValue = text.replace(regex, (match) => {
                return applyCasing(match, REPLACEMENT_BASE);
            });
        }
    }

    function fixTitle() {
        if (document.title && document.title.toLowerCase().includes(TARGET.toLowerCase())) {
            const regex = new RegExp(
                TARGET.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
                'gi'
            );

            document.title = document.title.replace(regex, (match) => {
                return applyCasing(match, REPLACEMENT_BASE);
            });
        }
    }

    function processQueue(queue) {
        const startTime = performance.now();

        while (queue.length > 0) {
            const node = queue.shift();
            processNode(node);

            if (node.childNodes) {
                for (let j = 0; j < node.childNodes.length; j++) {
                    const child = node.childNodes[j];
                    if (!IGNORE_TAGS.includes(child.tagName)) {
                        queue.push(child);
                    }
                }
            }

            if (performance.now() - startTime > MAX_EXEC_TIME_MS) {
                setTimeout(() => processQueue(queue), 0);
                return;
            }
        }
    }

    fixTitle();
    setInterval(() => {fixTitle();}, 2000);


    const initialQueue = [document.body];
    processQueue(initialQueue);


    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            mutation.addedNodes.forEach(node => {
                if (!IGNORE_TAGS.includes(node.tagName)) {
                    const queue = [node];
                    processQueue(queue);
                }
            });
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();