Word & Text Replace

replaces text with other text.

目前為 2019-09-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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;
            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.");
})();