replace words
目前為
Just copy replacement.js to Tampermonkey / Userscripts.
Change url to your config.
Here is small js script for word replacements.
include, you can add configs from other pplit's small config at the beginning of the scirpt
const config = {
"json_url": "https://api.npoint.io/adca32df1622919ca5bd",
"traditional_to_simple_chinese": true,
"default_priority_level": 1,
"reload_config_event": {
"max_clicks": 10,
"max_time_ms": 3000
}
};
url, with your config
traditional_to_simple_chinese: true/falseWhere you can store json?
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
"reload_config_event": {
"max_clicks": 10,
"max_time_ms": 3000
}
Here are settings for reloading json config.
As example - you reading text, made replacement in config - and want to check result, without reloading page.
You should click max_clicks times in max_time_ms time at any place of page.
10 times in 3 sec by default.
Script would reload config, swap text back to original state and apply new replacements.
optional, useful if you're experimenting with the order of substitutions
read about __level in json
config looks this way:
{
"__urls": [
"https://some_novel_site/my_novel/chapter_*"
],
"smthA": {
"a": "b",
"smthB": {
"smthC": {
"x": "y",
}
}
},
"smthD": {
"/val(.+)/": "test $1"
}
}
Check here mine current config:
[https://www.npoint.io/docs/adca32df1622919ca5bd]
[https://www.npoint.io/docs/97ef86b1e99dfc26c72d]
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"],
]
Replacement have 2 options.
string: stringstring: [str1, str2, ...str_n]Only this things would be collected.
"x": ["a", "b", "c"]
"老三", ["Third Bro", "Younger Bro"],
Random value would be select for replacement.
"AAx" -> "AAb"
"AAx" -> "AAc"
Example:
"/(\\d)o/": "$10" - change o after digit to 0
2o -> 20, 9o -> 90
/\\i, u, e.t.c.g auto insertedgoogle javascript regex if want more examples
ab"a": "Alice", "b": "Bob"
basicaly result would be AliceBobAlice Bobrules to convertion:
Alice - start from letter, not hieroglyphAlice "XXX<--aXXX"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 laterexample:
{
"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*"
]
}
}
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.
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_*
//lunhuileyuan-nayizhiwenzi_.*/[]^&$.()?/\+{}|* with \\
https:\\/\\/tw\\.wa01\\.com\\/novel\\/pagea\\/lunhuileyuan-nayizhiwenzi_/https:\\/\\/tw\\.wa01\\.com\\/novel\\/pagea\\/lunhuileyuan-nayizhiwenzi_//https:\\/\\/tw\\.wa01\\.com\\/novel\\/pagea\\/lunhuileyuan-nayizhiwenzi_.+/[1][0-9] - chapters 10 - 19/https:\\/\\/tw\\.wa01\\.com\\/novel\\/pagea\\/lunhuileyuan-nayizhiwenzi_[1][0-9]/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.
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": {...},
}
}
As earlier was mentioned - script build simple array from json
arr = [
["a1", "b1"],
["a2", "b2"],
["a3", "b3"],
]
and apply replacements in same 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.
json
[
...,
->
["A", "B"],
...,
]
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,
}
}
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
}
}