Replacer

replace words

As of 20.12.2024. See апошняя версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

Аўтар
Shuraken007
Рэйтынг
0 0 0
Версія
2.1
Створаны
13.12.2024
Абноўлены
20.12.2024
Памер
52.6 КБ
Ліцэнзія
MIT
Ужываецца на
Усе сайты

Install

Just copy replacement.js to Tampermonkey / Userscripts.
Change url to your config.

Idea

Here is small js script for word replacements.

  • replacements stored at json file, which loaded by url
  • support regular expressions
  • flexible json design - create categories / subcategories
  • flexible url matching - you can specify web_book chapters for specific replacements as example
  • split json on multiple json files and use include, you can add configs from other ppl

Script Config

it's small config at the beginning of the scirpt

const  config  = {
    "json_url":  "https://api.npoint.io/5ef4bedc09296d71cf5a",
    "traditional_to_simple_chinese":  true,
    "delay_scroll_on_reload":  -1,
    "default_priority_level":  1,
};

json_url

url, with your config

  • traditional_to_simple_chinese: true/false

Where you can store json?

  • [https://www.npoint.io/]
  • you can create gist on [https://github.com/], or create repo with configs and make links to raw file

traditional_to_simple_chinese

true, false
I readed chinese book with translation dubbing. I saw as original, as translated text.
So I added option to convert hard readable hieroglyphs to there simple version

delay_scroll_on_reload

Delay for restoring page position in ms after reloading. If < 0 - not working.
By default browsers restore position. Try it, if smth gone wrong
3000 - 3 sec

default_priority_level

optional, useful if you're experimenting with the order of substitutions
read about __level in json

Json Config

config looks this way:

{
   "__urls": [
      "https://some_novel_site/my_novel/chapter_*"
   ],
   "smthA": {
      "a": "b",
      "smthB": {
         "smthC": {
            "x": "y",
         }
      }
   },
   "smthD": {
      "/val(.+)/": "test $1"
   }
}

You can create so much nested nodes with any names, as you want.
Script recursively parse json and collect pairs

arr = [
    ["a", "b"],
    ["x", "y"],
    ["/val(.+)/", "test $1"],
]

Replacements

Replacement have 2 options.

  • basic form: string: string
  • random form: string: [str1, str2, ...str_n]

Only this things would be collected.

Random replacements

"x": ["a", "b", "c"]
"老三", ["Third Bro", "Younger Bro"],
Random value would be select for replacement.
"AAx" -> "AAb"
"AAx" -> "AAc"

Regex support

Example:
"/(\\d)o/": "$10" - change o after digit to 0
2o -> 20, 9o -> 90

  • start and end with /
  • escape with \\
  • all flags supported i, u, e.t.c.
  • flag global g auto inserted
  • multiline would work only if all text lays in one html element - paragraph as example, depends on page

google javascript regex if want more examples

Space problem

  • you have string ab
  • you created replacements "a": "Alice", "b": "Bob" basicaly result would be AliceBob
    but script converts it to Alice Bob
    usefull for hieroglyphs without spaces

rules to convertion:

  • replacement like Alice - start from letter, not hieroglyph
  • it search - where to insert Alice "XXX<--aXXX"
  • if there is letter too - add space

Key Words

There are following key words:

  • __urls - ignore node if not passed
  • __include - add other nodes from higher level, or url to load another json
  • __level - set priority level, probably some replacements should work first, another later

__urls

example:

{
    "common_replacements": {
        ...
        "__urls": ["*"],
    }
    "MyNovel1": {
        ...
        "arc 5": {
            "specific per concrete chapters": {
                "x": "y",
                 // chapters 1159-1212
                 "__urls": [
                    "1159",
                    "/11[6-9][0-9]/",
                    "/120[0-9]/",
                    "/121[0-2]/",
                 ],             
            }
        }
        "__urls": [
            "https://some_novel_site/my_novel/chapter_*",
            "https://another_source_with_novel/chapt*"
        ]
    }
}

how script work with "__url", nested behaviour

Script recursively look in each node {}.
If it see __urls and they passed - it started to collect replacements in that node.

You can add __urls in any subnode too.
If they not passed - that subnode would be ignored.
Usefull for specific chapters of book/ pages, check example.

url format

easy way *

use *, it solves 99% cases
https://tw.wa01.com/novel/pagea/lunhuileyuan-nayizhiwenzi_*
if you really have * in your url (some extreme cases) - then escape it \\*
https://tw.wa01.com/novel/\\*/lunhuileyuan-nayizhiwenzi_*

hard way full regex

  • easy way - use unique part from url, no mess with /
    /lunhuileyuan-nayizhiwenzi_.*/
  • full way - escape []^&$.()?/\+{}|* with \\ https:\\/\\/tw\\.wa01\\.com\\/novel\\/pagea\\/lunhuileyuan-nayizhiwenzi_
  • add bounds /url/ - to point that this is regular expression
    /https:\\/\\/tw\\.wa01\\.com\\/novel\\/pagea\\/lunhuileyuan-nayizhiwenzi_/
  • add your regex part
    /https:\\/\\/tw\\.wa01\\.com\\/novel\\/pagea\\/lunhuileyuan-nayizhiwenzi_.+/
  • example for specific chapters
    [1][0-9] - chapters 10 - 19
    /https:\\/\\/tw\\.wa01\\.com\\/novel\\/pagea\\/lunhuileyuan-nayizhiwenzi_[1][0-9]/

__include

example:

{
    "commonA": {...},
    "commonB": {...},
    "Book1": {
        ...
        "__include": {
            "smth_name": "commonA",
            "blockB": "commonB",
            "engeneer terms": "https://some_server/engeneer_terms.json"
        }
    },
    "news_portal": {
        ...
        "__include": {"commonA": "commonA"}
    }
}

As you see - it allows to include outer nodes or even load other jsons.

how it works

Firstly script recursively check all nodes and collect all names on all depth like "name": {...}
Later script just insert in node with __include all data by aliases

Example:
json by url1

{"x": "y", "z": {"a": "b"}} // url json
{
    "commonA": {"nodeA": {"n": "m"}},
    "ExampleNode": {
        ...
        "__include": {
            "alias1": "nodeA",
            "engeneer_terms": "url1",
        }       
    }
}

ExampleNode converted to

{
    ...
    "ExampleNode": {
        ...
        "alias1": {"n": "m"},
        "engeneer_terms": {"x": "y", "z": {"a": "b"}}, 
        "__include": {...},
    }   
}

__level & order

about order of replacements

As earlier was mentioned - script build simple array from json

arr = [
    ["a1", "b1"],
    ["a2", "b2"],
    ["a3", "b3"],
]

and apply replacements in same order

automatic order

Example:

text = "xxAAxx"
json = {"A": "B", "AA": "CC"}
replacements = [
    ["A", "B"],
    ["AA", "CC"],
]

we expect xxCCxx
basically result would be xxBBxx

But this problem is solved.

  1. if adding key contains one of the earlier added - it would be moved before that point.
    json [ ..., -> ["A", "B"], ..., ]
  2. if exactly same key exists, new won't be added

Problem But regular expressions can't be auto checked.

{
    "100": "hundred",
    "/o\d/": "0$1",
}

we expected "1o0" -> "hundred"
but if replacements collected in wrong order (which json not guaranteed)

replacements = [
    ["100", "hundred"],
    ["/o\d/", "o$1"],   
]

here __level comes

{
    "100": "hundred",
    fixes: {
        "/o\d/": "0$1",
        "__level": 0,
    }
}

__level

Example:

{
    "first_order_fixes": {
        ...,
        "__level": 0,
    },
    ...
    "source_which_override_mine_replacements": {
        "__include": {
            "fiction terms": "some_url",
        }
        "__level": 2,
    }
}

__level allow you to solve problems with order
Default level set in javascript config:

let config = {
    ...
    "default_priority_level":  1,
}

All replacements collected independently by this levels.

let arr = [
    "-10": [['a1', 'a2'], ['a3', 'a4'], ...]
    "0": [['b1', 'b2'], ['b3', 'b4'], ...]
    "1": [['c1', 'c2'], ['c3', 'c4'], ...]
    "5": [['d', 'd2'], ['d3', 'd4'], ...]   
]

Autoorder, checking same key - works only on the same lvl
Finally they squashed in one array, ordered by levels

[
    'a1','a2',
    ...,
    'b1','b2',
    ...,
    'd1','d2',
    ...
]

Inheriting Level is inherited by child nodes.
Here is example.

{
    "a1": "a2", // default level 1
    "a": {
        "b1": "b2" // level 1
        "b": {
            "c1": "c2", // level -100
            "c": {
                "d1": "d2", // level -100
                "d": {
                    "e1": "e2", // level 5
                    "f": {
                        "f1": f2, // level 5
                    }
                    "__level": 5
                }
            }
            "__level": -100
        }
    },
    "x": {
        "x1": "x2", // level 3
        "__level": 3
    }
}