generated from KyuubiRan/EzXHepler-template
-
Notifications
You must be signed in to change notification settings - Fork 39
/
UserScript.kt
164 lines (147 loc) · 6.74 KB
/
UserScript.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
package org.matrix.chromext.hook
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.net.http.HttpResponseCache
import org.matrix.chromext.BuildConfig
import org.matrix.chromext.Chrome
import org.matrix.chromext.Listener
import org.matrix.chromext.proxy.UserScriptProxy
import org.matrix.chromext.script.Local
import org.matrix.chromext.script.ScriptDbManager
import org.matrix.chromext.utils.Log
import org.matrix.chromext.utils.findField
import org.matrix.chromext.utils.findFieldOrNull
import org.matrix.chromext.utils.findMethod
import org.matrix.chromext.utils.findMethodOrNull
import org.matrix.chromext.utils.hookAfter
import org.matrix.chromext.utils.hookBefore
object UserScriptHook : BaseHook() {
override fun init() {
val proxy = UserScriptProxy
// proxy.tabModelJniBridge.declaredConstructors[0].hookAfter {
// Chrome.addTabModel(it.thisObject)
// }
// findMethod(proxy.tabModelJniBridge) { name == "destroy" }
// .hookBefore { Chrome.dropTabModel(it.thisObject) }
if (Chrome.isSamsung) {
findMethodOrNull(proxy.tabWebContentsDelegateAndroidImpl) { name == "onDidFinishNavigation" }
.let {
if (it == null)
findMethod(proxy.tabWebContentsDelegateAndroidImpl) {
name == "updateBrowserControlsState"
}
else it
}
.hookAfter { Chrome.updateTab(it.thisObject) }
runCatching {
// Avoid exceptions thrown due to signature conficts while binding services
val ConnectionManager =
Chrome.load("com.samsung.android.sdk.scs.base.connection.ConnectionManager")
val mServiceConnection =
findField(ConnectionManager) { name == "mServiceConnection" }
.also { it.isAccessible = true }
findMethod(ConnectionManager) { name == "connectToService" }
// (Landroid/content/Context;Landroid/content/Intent;)Z
.hookBefore {
val hook = it
val ctx = hook.args[0] as Context
val intent = hook.args[1] as Intent
val connection = mServiceConnection.get(hook.thisObject) as ServiceConnection
runCatching {
if (BuildConfig.DEBUG) Log.d("Binding service ${intent} with ${ctx}")
val bound = ctx.bindService(intent, connection, Context.BIND_AUTO_CREATE)
hook.result = bound
}
.onFailure {
if (BuildConfig.DEBUG) Log.ex(it)
hook.result = false
}
}
}
.onFailure { if (BuildConfig.DEBUG) Log.ex(it) }
runCatching {
// Avoid version codes mismatch when isolated child services are connected
val childProcessConnection =
Chrome.load("org.chromium.base.process_launcher.ChildProcessConnection")
val packageUtils = Chrome.load("org.chromium.base.PackageUtils")
val buildInfo = Chrome.load("org.chromium.base.BuildInfo")
val buildConifg = Chrome.load("org.chromium.build.BuildConfig")
val mServiceName = findField(childProcessConnection) { name == "mServiceName" }
val getApplicationPackageInfo =
findMethod(packageUtils) { name == "getApplicationPackageInfo" }
val packageVersionCode = findMethod(buildInfo) { name == "packageVersionCode" }
val version_code = findFieldOrNull(buildConifg) { name == "VERSION_CODE" }
if (version_code != null) {
findMethod(childProcessConnection) { name == "onServiceConnectedOnLauncherThread" }
// (Landroid/os/IBinder;)V
.hookBefore {
val latestPackage = getApplicationPackageInfo.invoke(null, 0)
val latestVersionCode = packageVersionCode.invoke(null, latestPackage)
val loadedVersionCode = version_code.get(null)
if (latestVersionCode != loadedVersionCode) {
Log.d(
"Version codes mismatched for child services ${mServiceName.get(it.thisObject)}")
version_code.set(null, latestVersionCode)
}
}
}
}
.onFailure { if (BuildConfig.DEBUG) Log.ex(it) }
}
findMethod(if (Chrome.isSamsung) proxy.tabImpl else proxy.tabWebContentsDelegateAndroidImpl) {
name == "onUpdateUrl"
}
// public void onUpdateUrl(GURL url)
.hookAfter {
val tab = proxy.getTab(it.thisObject)!!
if (!Chrome.isSamsung) Chrome.updateTab(tab)
val url = proxy.parseUrl(it.args[0])!!
val isLoading = proxy.mIsLoading.get(tab) as Boolean
if (!url.startsWith("chrome") && isLoading) {
ScriptDbManager.invokeScript(url)
}
}
findMethod(proxy.tabWebContentsDelegateAndroidImpl) {
name == if (Chrome.isSamsung) "onAddMessageToConsole" else "addMessageToConsole"
}
// public boolean addMessageToConsole(int level, String message, int lineNumber,
// String sourceId)
.hookAfter {
// This should be the way to communicate with the front-end of ChromeXt
val lineNumber = it.args[2] as Int
val sourceId = it.args[3] as String
if (it.args[0] as Int == 0 &&
sourceId == "local://ChromeXt/init" &&
lineNumber == Local.anchorInChromeXt) {
Listener.startAction(it.args[1] as String, proxy.getTab(it.thisObject))
} else {
Log.d(
when (it.args[0] as Int) {
0 -> "D"
2 -> "W"
3 -> "E"
else -> "V"
} + ": [${sourceId}@${lineNumber}] ${it.args[1]}")
}
}
findMethod(proxy.navigationControllerImpl) {
name == "loadUrl" || parameterTypes contentDeepEquals arrayOf(proxy.loadUrlParams)
}
// public void loadUrl(LoadUrlParams params)
.hookBefore {
val url = proxy.parseUrl(it.args[0])!!
proxy.userAgentHook(url, it.args[0])
}
findMethod(proxy.chromeTabbedActivity, true) { name == "onResume" }
.hookBefore { Chrome.init(it.thisObject as Context) }
findMethod(proxy.chromeTabbedActivity) { name == "onStop" }
.hookBefore {
ScriptDbManager.updateScriptStorage()
val cache = HttpResponseCache.getInstalled()
Log.d("HttpResponseCache: Hit ${cache.hitCount} / NetWork ${cache.networkCount}")
cache.flush()
}
isInit = true
}
}