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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το 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         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
    });
})();