blob: 14e0606b1b93a638bb99f99167892adc43efe31e (
plain)
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
|
package com.pitchedapps.frost.injectors
import android.webkit.WebView
class JsBuilder {
private val css = StringBuilder()
private val js = StringBuilder()
fun css(css: String): JsBuilder {
this.css.append(css)
return this
}
fun js(content: String): JsBuilder {
this.js.append(content)
return this
}
fun build() = JsInjector(toString())
override fun toString(): String {
val builder = StringBuilder().append("!function(){")
if (css.isNotBlank()) {
val cssMin = css.replace(Regex("\\s+"), "")
builder.append("var a=document.createElement('style');a.innerHTML='$cssMin';document.head.appendChild(a);")
}
if (js.isNotBlank())
builder.append(js)
return builder.append("}()").toString()
}
}
class JsInjector(val function: String) {
fun inject(webView: WebView, callback: ((String) -> Unit)? = null) {
webView.evaluateJavascript(function, { value -> callback?.invoke(value) })
}
}
|