generated from KyuubiRan/EzXHepler-template
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathWebView.kt
63 lines (53 loc) · 2.34 KB
/
WebView.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
package org.matrix.chromext.hook
import android.os.Handler
import android.webkit.ConsoleMessage
import android.webkit.WebView
import org.matrix.chromext.Chrome
import org.matrix.chromext.Listener
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 WebViewHook : BaseHook() {
var ViewClient: Class<*>? = null
var ChromeClient: Class<*>? = null
fun evaluateJavascript(code: String?) {
val webView = Chrome.getTab() as WebView?
if (code != null && code.length > 0 && webView != null && webView.settings.javaScriptEnabled) {
Handler(Chrome.getContext().mainLooper).post { webView.evaluateJavascript(code, null) }
}
}
override fun init() {
WebView.setWebContentsDebuggingEnabled(true)
findMethod(ChromeClient!!, true) {
name == "onConsoleMessage" &&
parameterTypes contentDeepEquals arrayOf(ConsoleMessage::class.java)
}
// public boolean onConsoleMessage (ConsoleMessage consoleMessage)
.hookAfter {
// This should be the way to communicate with the front-end of ChromeXt
val consoleMessage = it.args[0] as ConsoleMessage
if (consoleMessage.messageLevel() == ConsoleMessage.MessageLevel.TIP &&
consoleMessage.sourceId() == "local://ChromeXt/init" &&
consoleMessage.lineNumber() == Local.anchorInChromeXt) {
Listener.startAction(consoleMessage.message())
} else {
Log.d(
consoleMessage.messageLevel().toString() +
": [${consoleMessage.sourceId()}@${consoleMessage.lineNumber()}] ${consoleMessage.message()}")
}
}
fun onUpdateUrl(url: String, view: WebView) {
if (url.startsWith("javascript")) return
Chrome.updateTab(view)
ScriptDbManager.invokeScript(url)
}
findMethod(ViewClient!!, true) { name == "onPageStarted" }
// public void onPageStarted (WebView view, String url, Bitmap favicon)
.hookAfter { onUpdateUrl(it.args[1] as String, it.args[0] as WebView) }
findMethod(Chrome.load("android.app.Activity")) { name == "onStop" }
.hookBefore { ScriptDbManager.updateScriptStorage() }
}
}