-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebViewExtension.kt
79 lines (72 loc) · 2.49 KB
/
WebViewExtension.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
fun WebView.injectJs(s: String, callback: ((String) -> Unit)? = null) {
onUiThread {
if (callback == null) {
this.evaluateJavascript("(function() {$s})();", null)
} else {
this.evaluateJavascript("(function() {$s})();") {
if (it.startsWith("\"") && it.endsWith("\"")) {
callback(it.replace("\\n", "\n").dropLast(1).drop(1))
} else {
callback(it.replace("\\n", "\n"))
}
}
}
}
}
fun onUiThread(callback: () -> Unit) {
GlobalScope.launch(Dispatchers.Main) {
callback()
}
}
fun WebView.playPauseVideo() {
onUiThread {
this.evaluateJavascript(
"""document.getElementsByClassName('player-control-play-pause-icon')[0].click()""",
null
)
}
}
fun WebView.previousVideo() {
onUiThread {
this.evaluateJavascript(
"""document.getElementsByClassName('player-controls-middle center')[0].children[0].click()""",
null
)
}
}
fun WebView.nextVideo() {
onUiThread {
this.evaluateJavascript(
"""document.getElementsByClassName('player-controls-middle center')[0].children[4].click()""",
null
)
}
}
fun WebView.isNextDisable(listener: ValueCallback<Boolean>) {
onUiThread {
this.evaluateJavascript(
"""(function() { var element = document.getElementsByClassName('player-controls-middle center')[0].children[0]; return element.className.includes('icon-disable'); })();""",
){ isDisable ->
listener.onReceiveValue(isDisable != "null" && isDisable == "true")
}
}
}
fun WebView.isPreviousDisable(listener: ValueCallback<Boolean>) {
onUiThread {
this.evaluateJavascript(
"""(function() { var element = document.getElementsByClassName('player-controls-middle center')[0].children[4]; return element.className.includes('icon-disable'); })();""",
){ isDisable ->
listener.onReceiveValue(isDisable != "null" && isDisable == "true")
}
}
}
fun WebView.isHome(listener: ValueCallback<Boolean>) {
//getElementsByClassName better than querySelector(sometime not work)
onUiThread {
this.evaluateJavascript(
"""(function() {var element = document.getElementsByClassName('pivot-bar-item-tab').length; return element > 0})();"""
) { isHome ->
listener.onReceiveValue(isHome.toBoolean())
}
}
}