Word & Text Replace

replaces text with other text.

Ajankohdalta 2.5.2019. Katso uusin versio.

// ==UserScript==
// @name         Word & Text Replace
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  replaces text with other text.
// @author       listfilterjay
// @match        *://*/*
// @grant        none
// @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 () {

    const showReplaceButton = 0; // set to 1 to show a button to manually run this script.
    const dynamicChecking = 1; // set to 1 to run the script automatically when new image elements are detected.

    //word filters
    var replaceArry = [];

    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'],
    );

    function replaceText() {
        //console.log("start replaceText.");
        var numTerms = replaceArry.length;
        var txtWalker = document.createTreeWalker(
            document.body,
            NodeFilter.SHOW_TEXT,
            {
                acceptNode: function (node) {
                    if (node.nodeValue.trim()) {
                        return NodeFilter.FILTER_ACCEPT;
                    }
                    return NodeFilter.FILTER_SKIP;
                }
            },
            false
        );
        var txtNode = txtWalker.nextNode();
        while (txtNode) {
            var oldTxt = txtNode.nodeValue;
            for (var J = 0; J < numTerms; J++) {
                oldTxt = oldTxt.replace(replaceArry[J][0], replaceArry[J][1]);
            }
            txtNode.nodeValue = oldTxt;
            txtNode = txtWalker.nextNode();
        }
        jQuery("a").each(function(){
            var aText = jQuery(this).text();
            if (aText && !/\S*/.test(aText)) {
                //console.log("at: "+ aText);
                for (var J = 0; J < numTerms; J++) {
                    if (replaceArry[J][0].test(aText)) {
                        aText = aText.replace(replaceArry[J][0], replaceArry[J][1]);
                        jQuery(this).text(aText);
                        //console.log(aText);
                        //console.log(replaceArry[J][0]);
                    }
                }
            }
        });
        //console.log("end replaceText.");
    }

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

    if (showReplaceButton){
         // adds button to run replace manually.
        jQuery("body").prepend("<div id='wr-reset'>WR</div>");
        jQuery("#wr-reset").click(replaceText);

        const wordReplaceCss =
`<style type="text/css">
 #wr-reset {
      display: block;
      background: #ffb51b;
      padding: 5px;
      border-radius: 5px;
      position: fixed;
      top: 5px;
      right: 40px;
      z-index: 999;
      color: white;
      font-weight: bold;
      cursor: pointer;
  }
</style>`;

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

    console.log("word replace script is active.");
})();