μSlop

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 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 });
})();