forked from JingMatrix/ChromeXt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserScript.kt
82 lines (71 loc) · 2.93 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
package org.matrix.chromext.hook
import android.content.Context
import android.net.http.HttpResponseCache
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.findMethod
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) }
findMethod(proxy.tabWebContentsDelegateAndroidImpl) { name == "onUpdateUrl" }
// public void onUpdateUrl(GURL url)
.hookAfter {
val tab = proxy.getTab(it.thisObject)!!
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 == "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()
}
}
}