diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt | 52 |
1 files changed, 9 insertions, 43 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt index 4ab48bdb..556a5555 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt @@ -5,44 +5,21 @@ import android.webkit.WebView /** * Created by Allan Wang on 2017-05-31. */ -enum class JsActions(val function: String) { - HIDE("style.display='none'"), - REMOVE("remove()") -} - -class VariableGenerator { +enum class JsActions(body: String) { + /** + * Redirects to login activity if create account is found + * see [com.pitchedapps.frost.web.FrostJSI.loadLogin] + */ + LOGIN_CHECK("document.getElementById('signup-button')&&Android.loadLogin();"); - var count = 0 + val function = "!function(){$body}();" - val next: String - get() { - var key = count - count++ - if (key == 0) return "a" - val name = StringBuilder() - while (key > 0) { - name.append(((key % 26) + 'a'.toInt()).toChar()) - key /= 26 - } - return name.toString() - } - - fun reset() { - count = 0 - } + fun inject(webView: WebView, callback: ((String) -> Unit)? = null) = JsInjector(function).inject(webView, callback) } class JsBuilder { - - private val map: MutableMap<String, MutableSet<JsActions>> = mutableMapOf() - private val v = VariableGenerator() private val css: StringBuilder by lazy { StringBuilder() } - fun append(action: JsActions, vararg ids: String): JsBuilder { - ids.forEach { id -> map[id]?.add(action) ?: map.put(id, mutableSetOf(action)) } - return this - } - fun css(css: String): JsBuilder { this.css.append(css.trim()) return this @@ -51,21 +28,10 @@ class JsBuilder { fun build() = JsInjector(toString()) override fun toString(): String { - v.reset() val builder = StringBuilder().append("!function(){") - map.forEach { id, actions -> - if (actions.size == 1) { - builder.append("document.getElementById('$id').${actions.first().function};") - } else { - val name = v.next - builder.append("var $name=document.getElementById('$id');") - actions.forEach { a -> builder.append("$name.${a.function};") } - } - } if (css.isNotBlank()) { - val name = v.next val cssMin = css.replace(Regex("\\s+"), "") - builder.append("var $name=document.createElement('style');$name.innerHTML='$cssMin';document.head.appendChild($name);") + builder.append("var a=document.createElement('style');a.innerHTML='$cssMin';document.head.appendChild(a);") } return builder.append("}()").toString() } |