blob: 0e46225c81b15d94697b87478cd2789f29d4114a (
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
38
39
40
41
|
package com.pitchedapps.frost.injectors
import android.webkit.WebView
import ca.allanwang.kau.kotlin.lazyContext
import com.pitchedapps.frost.utils.L
import java.io.FileNotFoundException
import java.util.*
/**
* Created by Allan Wang on 2017-05-31.
* Mapping of the available assets
* The enum name must match the css file name
*/
enum class JsAssets : InjectorContract {
MENU, MENU_DEBUG, CLICK_A, CONTEXT_A, MEDIA, HEADER_BADGES, TEXTAREA_LISTENER, NOTIF_MSG
;
var file = "${name.toLowerCase(Locale.CANADA)}.js"
var injector = lazyContext {
try {
val content = it.assets.open("js/$file").bufferedReader().use { it.readText() }
JsBuilder().js(singleInjector(content)).build()
} catch (e: FileNotFoundException) {
L.e(e, "JsAssets file not found")
JsInjector(JsActions.EMPTY.function)
}
}
override fun inject(webView: WebView, callback: ((String) -> Unit)?) {
injector(webView.context).inject(webView, callback)
}
private fun singleInjector(content: String) = StringBuilder().apply {
val name = "_frost_$name"
append("if (!window.hasOwnProperty(\"$name\")) {")
append("console.log(\"Registering $name\");")
append("window.$name = true;")
append(content)
append("}")
}.toString()
}
|