Word & Text Replace

replaces text with other text.

Versão de: 12/09/2019. Veja: a última versão.

// ==UserScript==
// @name         Word & Text Replace
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  replaces text with other text.
// @author       listfilterErick
// @grant        none

// @match        *://*/*

// @require      http://code.jquery.com/jquery-1.12.4.min.js
// @require      https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012

// @licence      CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/
// @licence      GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// ==/UserScript==
/*jshint esversion: 6 */

(function () {

    let wrShowReplaceButton = 0; // set to 1 to show a button to manually run this script.
    let wrButtonTransparency = .2; // set to a value between 0 and 1, 1 is no transparency, .5 is 50% transparency.
    let wrDynamicChecking = 1; // set to 1 to run the script automatically when new image elements are detected.
    let wrShowConsoleMsg = 0; // set to 1 to show console messages.
    let wrPrintRuntime = 0; // set to 1 to display runtime.

    function consolelog(text) {
        if (wrShowConsoleMsg) {
            console.log(text);
        }
    }

    if (wrPrintRuntime) {
        var wrStartTime = performance.now();
    }

    //word filters
    let replaceArry = [];

    if (1) {
        replaceArry.push(
            // basic examples:
            //[/(.\W?)*/i, 'words'], //replace all text instances with "words".
            //[/\w/gi, 'a'], //replace all characters with an "a" character.
            //[/match/gi, 'a'], //matches "match" in "ABmarchCD" and "red match".
            //[/\bmatch\b/gi, 'a'], //does not match "ABmatchesCD" but does match "this match is red".
            //[/scripts/gi, 'dictionaries'],
            //[/script/gi, 'dictionary'],
            //[/(web)?site/gi, 'webzone'],
        );
        // separated just for an example of an option of grouping/sorting.
        replaceArry.push(
            //[/user/gi, 'individual'],
            //[/\buse\b/gi, 'utilize'],
        );
    }

    var titleChecked = 0;

    function replaceText() {
        //console.log("start replaceText.");
        let numTerms = replaceArry.length;
        let isChanged = 0;

        // ==== title element =====================================================================|
        let tText = jQuery("title").text();
        if (tText && !titleChecked) {
            //consolelog("tt: " + tText);
            for (let index = 0; index < numTerms; index++) {
                if (replaceArry[index][0].test(tText)) {
                    tText = tText.replace(replaceArry[index][0], replaceArry[index][1]);
                    jQuery("title").text(tText);
                }
            }
            titleChecked = 1;
        }

        // #### text elements ####
        let txtWalker = document.createTreeWalker(
            document.body,
            NodeFilter.SHOW_TEXT,
            {
                acceptNode: function (node) {
                    if (node.nodeValue && node.parentNode.nodeName !== 'SCRIPT') {
                        return NodeFilter.FILTER_ACCEPT;
                    }
                    return NodeFilter.FILTER_SKIP;
                }
            },
            false
        );
        let txtNode = txtWalker.nextNode();
        while (txtNode) {
            let oldTxt = txtNode.nodeValue.trim();
            for (let index = 0; index < numTerms; index++) {
                if (replaceArry[index][0].test(oldTxt)) {
                    isChanged = 1;
                    oldTxt = oldTxt.replace(replaceArry[index][0], replaceArry[index][1]);
                }
            }
            if (isChanged) {
                //consolelog("new (text node) text: "+ oldTxt);
                isChanged = 0;
            }
            txtNode.nodeValue = oldTxt;
            txtNode = txtWalker.nextNode();
        }

        // ==== link elements =====================================================================|
        jQuery("a").each(function () {
            if (jQuery(this).children().length == 0) {
                let aText = this.innerHTML;
                if (aText && !/^\S*$/.test(aText) && !jQuery(this).hasClass("wr-checked")) {
                    //consolelog("at: "+ aText);
                    for (let index = 0; index < numTerms; index++) {
                        if (replaceArry[index][0].test(aText)) {
                            isChanged = 1;
                            aText = aText.replace(replaceArry[index][0], replaceArry[index][1]);
                            jQuery(this).text(aText);
                        }
                    }
                    if (isChanged) {
                        //consolelog("new (a) text: " + aText.trim());
                        isChanged = 0;
                    }
                    jQuery(this).addClass("wr-checked");
                }
            }
        });
        //console.log("end replaceText.");
    } //end function function replaceText()

    if (wrDynamicChecking) {
        waitForKeyElements("img", replaceText);
    }else {
        replaceText();
    }

    if (wrShowReplaceButton) {
        // adds button to run replace manually.

        if (!jQuery("#wt-buttons").length) {
            consolelog("(WR) created #wt-buttons.");
            jQuery("body").prepend("<div id='wt-buttons'><div id='wr-reset'>WR</div><div id='wt-close'>&times;</div></div>");
            jQuery("#wt-close").click(function () { jQuery("#wt-buttons").remove(); });

            const webToolsCss =
                `<style type="text/css">
    #wt-buttons {
        width: 50px;
        display: block;
        opacity: `+ wrButtonTransparency + `;
        position: fixed;
        top: 0px;
        right: 0px;
        z-index: 999;
    }
    #wt-buttons:hover {
        opacity: 1;
    }
    #wt-buttons div {
        display: block !important;
        padding: 5px;
        border-radius: 5px;
        margin-top: 2px;
        font-size: initial !important;
        font-weight: bold;
        color: white;
        cursor: pointer;
    }
    #wt-close {
        background: #777777;
        text-align: center;
    }
</style>`;

            jQuery(document.body).append(webToolsCss);
        } else {
            jQuery("#wt-buttons").prepend("<div id='wr-reset'>WR</div>");
        }
        jQuery("#wr-reset").click(replaceText);

        const wordReplaceCss =
            `<style type="text/css">
    #wr-reset {
        background: #ffb51b;
    }
</style>`;

        jQuery(document.body).append(wordReplaceCss);
    }

    if (wrPrintRuntime) {
        var wrEndTime = performance.now();
        console.log('(WR) finished after ' + ((wrEndTime - wrStartTime) / 1000).toFixed(2) + ' seconds.');
    }

    consolelog("word replace script is active.");
})();