μ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 για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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