Pattern Mapping Replacer - Multi-Site

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         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
    });
})();