Pattern Mapping Replacer - Multi-Site

Replaces text based on a mapping with exact matching and site-specific filtering

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

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

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

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

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

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Pattern Mapping Replacer - Multi-Site
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Replaces text based on a mapping with exact matching and site-specific filtering
// @author       Kreezxil
// @match        *://*/*
// @grant        none
// @license      MIT; explicity from MIT license, you must link back to this script.
// ==/UserScript==

(function() {
    'use strict';

    // 1. Define your site lists as variables
    const bibleSites = ["sefaria.org", "chabad.org", "biblegateway.com"];
    const allSites = [];

    /**
     * MAPPING CONFIGURATION
     */
    const mapping = {
        // 2. Use the variable name instead of typing the array
        "The Lord": [true, bibleSites, "Yahweh"],
        "the Lord": [false, bibleSites, "Yahweh"],
        "THE LORD": [false, bibleSites, "Yahweh"],
        "thou": [false, allSites, "you"],
        "shalt": [false, allSites, "shall"],
        "hallow": [false, bibleSites, "sanctify"],
        "candlestick": [false, bibleSites, "menorah"],
        "didst": [false, bibleSites, "did"],
        "God": [false, bibleSites, "Elohim"],
        "thereat": [false, bibleSites, "there"]
    };

    const currentHost = window.location.hostname;

    function escapeRegExp(string) {
        return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    }

    function applyReplacements(text) {
        let newText = text;
        for (const [pattern, [exactMatch, allowedSites, replacement]] of Object.entries(mapping)) {

            // Check if current site is in the allowed list or if the list is empty
            const isSiteAllowed = allowedSites.length === 0 || allowedSites.some(site => currentHost.includes(site));

            if (!isSiteAllowed) continue;

            const escapedPattern = escapeRegExp(pattern);
            const flags = exactMatch ? 'g' : 'gi';
            const regex = new RegExp(`\\b${escapedPattern}\\b`, flags);
            newText = newText.replace(regex, replacement);
        }
        return newText;
    }

    function walkNodes(node) {
        if (node.nodeType === Node.TEXT_NODE) {
            const processed = applyReplacements(node.nodeValue);
            if (processed !== node.nodeValue) {
                node.nodeValue = processed;
            }
        } else if (node.nodeType === Node.ELEMENT_NODE &&
                   !['SCRIPT', 'STYLE', 'TEXTAREA', 'INPUT'].includes(node.tagName)) {
            for (let i = 0; i < node.childNodes.length; i++) {
                walkNodes(node.childNodes[i]);
            }
        }
    }

    walkNodes(document.body);

    const observer = new MutationObserver((mutations) => {
        for (const mutation of mutations) {
            for (const node of mutation.addedNodes) {
                walkNodes(node);
            }
        }
    });

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