generated from KyuubiRan/EzXHepler-template
-
Notifications
You must be signed in to change notification settings - Fork 29
/
UserScript.kt
131 lines (117 loc) · 4.87 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
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.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
}
}
}
}
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
}
}