-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTentiles.js
232 lines (197 loc) · 9.64 KB
/
Tentiles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// ==UserScript==
// @name TenTiles
// @version 0.2
// @description Makes Tenhou.net UI easier on Western players
// @author Liam Kelly
// @updateURL https://raw.githubusercontent.com/liammkellly/tenhou-tiles/main/Tentiles.js
// @match *://tenhou.net/3*
// @noframes
// @grant GM.setValue
// @grant GM.getValue
// ==/UserScript==
(async () => {
function addOptionsButton() {
let settingsScripts = document.createElement("script");
settingsScripts.innerText = 'function saveSettings(){' +
'let transLanguage = document.getElementById("transLanguage").value;' +
'let replaceTilesStatus = document.getElementById("replaceTilesBox").checked;' +
'let updateTransStatus = document.getElementById("updateTrans").checked;' +
'let newURL = "https://tenhou.net/3/#updateTiles=" + (replaceTilesStatus == true ? "1" : "0")' +
'+ "updateTrans=" + (updateTransStatus == true ? "1" : "0")' +
'+ "transLanguage=" + transLanguage;' +
//'alert(newURL);' +
'location.replace(newURL);' +
'location.reload(true);' +
'};';
document.head.appendChild(settingsScripts);
let optionsWindow = document.createElement("div");
optionsWindow.id = "optionsWindow";
optionsWindow.style.zIndex = 1000;
optionsWindow.style.display = "none";
optionsWindow.style.position = "absolute";
optionsWindow.style.fontSize = "1em";
optionsWindow.style.position = "absolute";
optionsWindow.style.left = "30%";
optionsWindow.style.width = "40%";
optionsWindow.style.top = "40%";
optionsWindow.style.textAlign = "center";
optionsWindow.style.backgroundColor = "grey";
optionsWindow.innerHTML = `TenTiles userscript options.<br />
Saving changes will refresh the page.<br /><br />
<table>
<tr><td>Replace tiles</td><td><input type="checkbox" id="replaceTilesBox" ` + (tenSettings.replaceTiles == true ? "checked" : "") + `></td></tr>
<tr><td>Translate UI</td><td><input type="checkbox" id="updateTrans" ` + (tenSettings.translateUI == true ? "checked" : "") + `></td></tr>
<tr><td>Target language:</td><td><select id="transLanguage">
<option value="DEFAULT" ` + (tenSettings.translationLN == "DEFAULT" ? "selected" : "") + `>Default</option>
<option value="ENG" ` + (tenSettings.translationLN == "ENG" ? "selected" : "") + `>English</option>
<option value="FRA_TRAD" ` + (tenSettings.translationLN == "FRA_TRAD" ? "selected" : "") + `>French - complete</option>
<option value="FRA_DEFT" ` + (tenSettings.translationLN == "FRA_DEFT" ? "selected" : "") + `>French - default</option>
<option value="VIE" ` + (tenSettings.translationLN == "VIE" ? "selected" : "") + `>Vietnamese</option>
<option value="ES" ` + (tenSettings.translationLN == "ES" ? "selected" : "") + `>Spanish</option>
<option value="POL" ` + (tenSettings.translationLN == "POL" ? "selected" : "") + `>Polish</option>
<option value="RUS_DEFT" ` + (tenSettings.translationLN == "RUS_DEFT" ? "selected" : "") + `>Russian</option></select></td></tr>
</table>
<button onclick="saveSettings()" ontouchstart="saveSettings()" value="Save settings">Save settings</button>`;
document.body.appendChild(optionsWindow);
let optionsButtonDiv = document.createElement("div");
optionsButtonDiv.style.position = "fixed";
optionsButtonDiv.style.zIndex = 1000;
let optionsButton = document.createElement("button");
optionsButton.innerText = "TenTiles options";
optionsButton.style.fontSize = "0.5em"
optionsButton.ontouchstart = function () {
let optionsWindow = document.getElementById("optionsWindow");
if (optionsWindow.style.display == "block") {
optionsWindow.style.display = "none";
} else {
optionsWindow.style.display = "block";
};
};
optionsButton.onclick = function () {
let optionsWindow = document.getElementById("optionsWindow");
if (optionsWindow.style.display == "block") {
optionsWindow.style.display = "none";
} else {
optionsWindow.style.display = "block";
};
};
optionsButton.id = "optionsButton";
optionsButtonDiv.appendChild(optionsButton);
document.body.appendChild(optionsButtonDiv);
}
function injectPatch() {
fetch("https://tenhou.net/3/latest.js").then(
t => t.text()).then(
t => varToReplace = t[t.search("vieww") + 18]).then(
() => fetch("https://tenhou.net/3/")).then(
t => t.text()
).then(t => {
if (tenSettings.replaceTiles) {
document.write(
t.replace("e.src=src;",
'fetch(src).then((responseSrc) => responseSrc.text()).then((dataSrc) => e.text=dataSrc.replace("cdn.tenhou.net/5/img/","\\" + (' + varToReplace + ' == 0 ? \\"' + 'tentiles.work.gd/serveimg.php?img=' + '\\" : \\"cdn.tenhou.net/5/img/\\") + \\""));')
);
}
if (tenSettings.translateUI) {
startUpdatingUI();
}
addOptionsButton();
}).then();
}
let tenSettings = await GM.getValue('tenSettings');
if (tenSettings) {
tenSettings = JSON.parse(tenSettings);
}
if (!tenSettings) {
alert("The TenTiles userscript is being run for the first time. It will set default values, you can change them later using the TenTiles button on the splashpage.");
tenSettings = {
"ressourcesURL": "tentiles.work.gd/serveimg.php?img=",
"replaceTiles": true,
"translationsURL": "https://tentiles.work.gd/translations.json",
"translationLN": "ENG",
"translateUI": true
};
await GM.setValue('tenSettings', JSON.stringify(tenSettings));
}
const URL = window.location.href;
let regex = /updateTiles=([0-1])/g;
let matches = URL.matchAll(regex);
matches = Array.from(matches)
if (matches[0]) {
if (matches[0][1] == 1) {
tenSettings.replaceTiles = true;
} else if (matches[0][1] == 0) {
tenSettings.replaceTiles = false;
}
}
regex = /updateTrans=([0-1])/g;
matches = URL.matchAll(regex);
matches = Array.from(matches)
if (matches[0]) {
if (matches[0][1] == 1) {
tenSettings.translateUI = true;
} else if (matches[0][1] == 0) {
tenSettings.translateUI = false;
}
}
regex = /transLanguage=([A-Z_]*)/g;
matches = URL.matchAll(regex);
matches = Array.from(matches)
if (matches[0]) {
tenSettings.translationLN = matches[0][1];
}
// alert("updateTiles: " + tenSettings.replaceTiles + "\ntranslateUI: " + tenSettings.translateUI + "\nlanguage: " + tenSettings.translationLN);
await GM.setValue('tenSettings', JSON.stringify(tenSettings));
let resourcesURL = tenSettings.ressourcesURL;
let checkConnectivity;
let Httpreq = new XMLHttpRequest();
Httpreq.onreadystatechange = function () {
if (Httpreq.readyState === 4) {
if (Httpreq.status === 200) {
checkConnectivity = true;
} else {
checkConnectivity = false;
addOptionsButton();
}
}
}
Httpreq.open("GET", tenSettings.translationsURL, false);
Httpreq.send(null);
if (checkConnectivity) {
let allTranslations = JSON.parse(Httpreq.responseText);
let exactTranslation = allTranslations['exactTranslation'];
let partialTranslation = allTranslations['partialTranslation'];
function nodeCeption(node) {
if (node.nodeType == 3) {
for (oneTranslation in exactTranslation) {
if (exactTranslation[oneTranslation][tenSettings.translationLN] ) {
node.nodeValue = node.nodeValue.replace(oneTranslation, exactTranslation[oneTranslation][tenSettings.translationLN]);
} else {
node.nodeValue = node.nodeValue.replace(oneTranslation, exactTranslation[oneTranslation]['DEFAULT']);
}
}
for (oneTranslation in partialTranslation) {
if (partialTranslation[oneTranslation][tenSettings.translationLN] ) {
node.nodeValue = node.nodeValue.replace(oneTranslation, partialTranslation[oneTranslation][tenSettings.translationLN]);
} else {
node.nodeValue = node.nodeValue.replace(oneTranslation, partialTranslation[oneTranslation]['DEFAULT']);
}
}
} else {
node.childNodes.forEach(nodeCeption);
}
}
function startUpdatingUI() {
const callback = (mutationList, observer) => {
observer.disconnect();
for (individualMutation in mutationList) {
nodeCeption(mutationList[individualMutation].target);
}
observer.observe(document.body, { childList: true, subtree: true });
};
const observer = new MutationObserver(callback);
observer.observe(document.body, { childList: true, subtree: true });
}
injectPatch();
}
})();