generated from KyuubiRan/EzXHepler-template
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathLocal.kt
151 lines (136 loc) · 5.09 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
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
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): List<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
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 {
func
}
if (localScript.containsKey(name))
if (!script.grant.contains(name)) grants += localScript.get(name)
grants += "${it}={sync: ${name}};\n"
}
}
}
grants += localScript.get("GM.bootstrap")!!
code = localScript.get("globalThis")!! + "((key) => {${code}})(null);"
val GM_info =
JSONObject(
mapOf("scriptMetaStr" to script.meta, "script" to JSONObject().put("id", script.id)))
val codes =
mutableListOf(
"(() => { const GM = {key:${Local.key}}; const GM_info = ${GM_info}; GM_info.script.code = () => {${code}};\n${grants}GM.bootstrap();})();\n//# sourceURL=local://ChromeXt/${Uri.encode(script.id)}")
if (script.storage != null) {
val storage_info =
JSONObject(mapOf("id" to script.id, "data" to JSONObject().put("init", script.storage!!)))
codes.add("ChromeXt.unlock(${Local.key}).post('scriptStorage', ${storage_info});")
}
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()
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("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")
initChromeXt = localScript[0]
anchorInChromeXt = initChromeXt.split("\n").indexOfFirst { it.endsWith("// Kotlin anchor") } + 2
openEruda = localScript[1].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 as? MatchNamedGroupCollection
if (vMatchGroup != null) {
return vMatchGroup.get("version")?.value as String
}
}
return "latest"
}
}