generated from KyuubiRan/EzXHepler-template
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Local.kt
170 lines (155 loc) · 5.84 KB
/
Local.kt
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
package org.matrix.chromext.script
import android.content.Context
import android.net.Uri
import java.io.File
import java.io.FileReader
import kotlin.random.Random
import org.json.JSONArray
import org.json.JSONObject
import org.matrix.chromext.Chrome
import org.matrix.chromext.Resource
import org.matrix.chromext.utils.Log
import org.matrix.chromext.utils.randomString
object GM {
private val localScript: Map<String, String>
init {
val ctx = Chrome.getContext()
Resource.enrich(ctx)
localScript =
ctx.assets
.open("GM.js")
.bufferedReader()
.use { it.readText() }
.split("// Kotlin separator\n\n")
.associateBy(
{
val decalre = it.lines()[0]
val sep = if (decalre.startsWith("function")) "(" else " ="
decalre.split(sep)[0].split(" ").last()
},
{ it })
}
fun bootstrap(
script: Script,
codes: MutableList<String> = mutableListOf<String>()
): MutableList<String> {
var code = script.code
var grants = ""
if (!script.meta.startsWith("// ==UserScript==")) {
code = script.meta + code
}
script.grant.forEach {
when (it) {
"none" -> return@forEach
"GM_info" -> return@forEach
"GM.ChromeXt" -> return@forEach
"window.close" -> return@forEach
else ->
if (localScript.containsKey(it)) {
grants += localScript.get(it)
} else if (it.startsWith("GM_")) {
grants +=
"function ${it}(){ console.error('${it} is not implemented in ChromeXt yet, called with', arguments) }\n"
} else if (it.startsWith("GM.")) {
val func = it.substring(3)
val name =
"GM_" +
if (func == "xmlHttpRequest") {
"xmlhttpRequest"
} else if (func == "getResourceUrl") {
"getResourceURL"
} else {
func
}
if (localScript.containsKey(name) && !script.grant.contains(name))
grants += localScript.get(name)
grants += "${it}={sync: ${name}};\n"
}
}
}
grants += localScript.get("GM.bootstrap")!!
val GM_info = JSONObject(mapOf("scriptMetaStr" to script.meta))
GM_info.put("script", JSONObject().put("id", script.id))
if (script.storage != null) GM_info.put("storage", script.storage)
code = "\ndelete window.__loading__;\n${code};"
code = localScript.get("globalThis")!! + script.lib.joinToString("\n") + code
codes.add(
"(()=>{ const GM = {key:${Local.key}, name:'${Local.name}'}; const GM_info = ${GM_info}; GM_info.script.code = (key=null) => {${code}};\n${grants}GM.bootstrap();})();\n//# sourceURL=local://ChromeXt/${Uri.encode(script.id)}")
return codes
}
}
object Local {
val promptInstallUserScript: String
val customizeDevTool: String
val eruda: String
val encoding: String
val initChromeXt: String
val openEruda: String
val cspRule: String
val cosmeticFilter: String
val key = Random.nextDouble()
val name = randomString(25)
var eruda_version: String?
val anchorInChromeXt: Int
// lineNumber of the anchor in GM.js, used to verify ChromeXt.dispatch
init {
val ctx = Chrome.getContext()
Resource.enrich(ctx)
var css =
JSONArray(
ctx.assets.open("editor.css").bufferedReader().use { it.readText() }.split("\n\n"))
promptInstallUserScript =
"const _editor_style = ${css}[0];\n" +
ctx.assets.open("editor.js").bufferedReader().use { it.readText() }
customizeDevTool = ctx.assets.open("devtools.js").bufferedReader().use { it.readText() }
css =
JSONArray(ctx.assets.open("eruda.css").bufferedReader().use { it.readText() }.split("\n\n"))
eruda =
"eruda._styles = ${css};\n" +
ctx.assets
.open("eruda.js")
.bufferedReader()
.use { it.readText() }
.replaceFirst("Symbol.ChromeXt", "Symbol." + name)
.replaceFirst("ChromeXtUnlockKeyForEruda", key.toString())
encoding = ctx.assets.open("encoding.js").bufferedReader().use { it.readText() }
eruda_version = getErudaVersion()
val localScript =
ctx.assets
.open("scripts.js")
.bufferedReader()
.use { it.readText() }
.split("// Kotlin separator\n\n")
val seed = Random.nextDouble()
// Use empty lines to randomize anchorInChromeXt
val parts =
localScript[0]
.replaceFirst("Symbol.ChromeXt", "Symbol." + name)
.replaceFirst("ChromeXtUnlockKeyForInit", key.toString())
.split("\n")
.filter { if (it.length != 0) true else Random.nextDouble() > seed }
anchorInChromeXt = parts.indexOfFirst { it.endsWith("// Kotlin anchor") } + 2
initChromeXt = parts.joinToString("\n")
openEruda =
localScript[1]
.replaceFirst("Symbol.ChromeXt", "Symbol." + name)
.replaceFirst("ChromeXtUnlockKeyForEruda", key.toString())
cspRule = localScript[2]
cosmeticFilter = localScript[3]
}
fun getErudaVersion(ctx: Context = Chrome.getContext(), versionText: String? = null): String? {
val eruda = File(ctx.filesDir, "Eruda.js")
if (eruda.exists() || versionText != null) {
val verisonReg = Regex(" eruda v(?<version>[\\d\\.]+) https://")
val firstLine = (versionText ?: FileReader(eruda).use { it.readText() }).lines()[0]
val vMatchGroup = verisonReg.find(firstLine)?.groups
if (vMatchGroup != null) {
return vMatchGroup[1]?.value as String
} else if (eruda.exists()) {
eruda.delete()
Log.toast(ctx, "Eruda.js is corrupted")
}
}
return null
}
}