This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
majsoul-plus-korean.user.js
121 lines (110 loc) · 4.7 KB
/
majsoul-plus-korean.user.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
// ==UserScript==
// @name Majsoul Korean (EN Server)
// @namespace majsoul-plus-korean
// @version 0.1.7
// @description Apply majsoul-plus-korean using UserScript!
// @author YF-Dev
// @license MIT
// @icon https://file.update.sh/mpk/korean/preview.png
// @supportURL https://github.com/yf-dev/majsoul-korean-resources/issues
// @homepageURL https://github.com/yf-dev/majsoul-korean-resources/
// @downloadURL https://raw.githubusercontent.com/yf-dev/majsoul-korean-resources/main/majsoul-plus-korean.user.js
// @updateURL https://raw.githubusercontent.com/yf-dev/majsoul-korean-resources/main/majsoul-plus-korean.user.js
// @include https://mahjongsoul.game.yo-star.com/*
// @grant unsafeWindow
// @grant GM_getResourceText
// @run-at document-start
// @resource resourcepack https://raw.githubusercontent.com/yf-dev/majsoul-korean-resources/main/resources/korean/resourcepack.json
// ==/UserScript==
(function () {
'use strict';
const GAME_BASE_URL = 'https://mahjongsoul.game.yo-star.com/';
const MAJSOUL_PLUS_BASE_URL = 'https://localhost:8887/';
const RES_BASE_URL = 'https://raw.githubusercontent.com/yf-dev/majsoul-korean-resources/main/resources/korean/';
const RESOURCEPACK_URL = 'https://raw.githubusercontent.com/yf-dev/majsoul-korean-resources/main/resources/korean/resourcepack.json';
const version_re = /v\d+\.\d+\.\d+\.w\//i;
let resourcepack = null;
// replaceCodeScript();
// if ('function' === typeof GM_getResourceText) {
// resourcepack = JSON.parse(GM_getResourceText('resourcepack'));
// } else {
// var resourcepack_xhr = new XMLHttpRequest();
// resourcepack_xhr.open("GET", RESOURCEPACK_URL, false);
// resourcepack_xhr.send();
// resourcepack = JSON.parse(resourcepack_xhr.response);
// }
// replaceXhrOpen();
function replaceCodeScript() {
console.log(window.Laya);
if (window.Laya !== undefined && window.Laya.Loader !== undefined && window.Laya.Loader.prototype !== undefined) {
replaceLayaLoadImage();
replaceLayaLoadSound();
replaceLayaLoadTtf();
return;
}
let observer = null;
observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
const scripts = document.getElementsByTagName('script');
for (let i = 0; i < scripts.length; i++) {
const script = scripts[i];
if (script.src && script.src.indexOf('code.js') !== -1) {
script.onload = function () {
replaceLayaLoadImage();
replaceLayaLoadSound();
replaceLayaLoadTtf();
};
observer.disconnect();
}
}
});
});
const config = {
childList: true,
subtree: true
};
observer.observe(document, config);
}
function updateUrl(url) {
const original_url = url;
// console.log('original url: ' + url);
if (url.startsWith(GAME_BASE_URL)) {
url = url.substring(GAME_BASE_URL.length);
}
if (url.startsWith(MAJSOUL_PLUS_BASE_URL)) {
url = url.substring(MAJSOUL_PLUS_BASE_URL.length);
}
url = url.replace(version_re, '');
if (resourcepack.replace.includes(url)) {
url = RES_BASE_URL + 'assets/' + url;
console.log(' updated url: ' + url);
return url;
} else {
return original_url;
}
}
function replaceXhrOpen() {
const original_function = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
return original_function.call(this, method, updateUrl(url), async, user, password);
};
}
function replaceLayaLoadImage() {
const original_function = Laya.Loader.prototype._loadImage;
Laya.Loader.prototype._loadImage = function (url) {
return original_function.call(this, updateUrl(url));
}
}
function replaceLayaLoadSound() {
const original_function = Laya.Loader.prototype._loadSound;
Laya.Loader.prototype._loadSound = function (url) {
return original_function.call(this, updateUrl(url));
}
}
function replaceLayaLoadTtf() {
const original_function = Laya.Loader.prototype._loadTTF;
Laya.Loader.prototype._loadTTF = function (url) {
return original_function.call(this, updateUrl(url));
}
}
})();