aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt
blob: 21d660b8b7a25ffefc22ef1350912d69efa2b1eb (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
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, HEADER_HIDER, TEXTAREA_LISTENER, NOTIF_MSG,
    DOCUMENT_WATCHER
    ;

    var file = "${name.toLowerCase(Locale.CANADA)}.js"
    var injector = lazyContext {
        try {
            val content = it.assets.open("js/$file").bufferedReader().use { it.readText() }
            JsBuilder().js(content).single(name).build()
        } catch (e: FileNotFoundException) {
            L.e(e) { "JsAssets file not found" }
            JsInjector(JsActions.EMPTY.function)
        }
    }

    override fun inject(webView: WebView, callback: (() -> Unit)?) {
        injector(webView.context).inject(webView, callback)
    }

}