Word & Text Replace

replaces text with other text.

目前为 2019-09-03 提交的版本。查看 最新版本

// ==UserScript==
// @name         Word & Text Replace
// @namespace    http://tampermonkey.net/
// @version      1.1
// @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 = .33; // 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 = [];

    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.");
        // #### title element ####
        let numTerms = replaceArry.length;
        let tText = jQuery("title").text();
        if (tText) {
            console.log("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);
                    //console.log(tText);
                    //console.log(replaceArry[J][0]);
                }
            }
        }
        // #### text elements ####
        let txtWalker = document.createTreeWalker(
            document.body,
            NodeFilter.SHOW_TEXT,
            {
                acceptNode: function (node) {
                    if (node.nodeValue.trim()) {
                        return NodeFilter.FILTER_ACCEPT;
                    }
                    return NodeFilter.FILTER_SKIP;
                }
            },
            false
        );
        let txtNode = txtWalker.nextNode();
        while (txtNode) {
            var oldTxt = txtNode.nodeValue;
            for (let index = 0; index < numTerms; index++) {
                oldTxt = oldTxt.replace(replaceArry[index][0], replaceArry[index][1]);
            }
            txtNode.nodeValue = oldTxt;
            txtNode = txtWalker.nextNode();
        }
        // #### link elements ####
        jQuery("a").each(function () {
            let aText = jQuery(this).text();
            if (aText) {
                //console.log("at: "+ aText);
                for (let index = 0; index < numTerms; index++) {
                    if (replaceArry[index][0].test(aText)) {
                        aText = aText.replace(replaceArry[index][0], replaceArry[index][1]);
                        jQuery(this).text(aText);
                        //console.log(aText);
                        //console.log(replaceArry[J][0]);
                    }
                }
            }
        });
        //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("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.");
})();