Word & Text Replace

replaces text with other text.

目前為 2019-05-02 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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.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 = 1; // 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.");
})();