aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-06 19:29:25 -0700
committerAllan Wang <me@allanwang.ca>2017-06-06 19:29:25 -0700
commit067ea15188f20fa268255153e35c2df732fdffee (patch)
tree87a0fcd84f37ad569ec2743ffc702a69cd59e252 /app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt
parentc4e22f5512570d05178711ba90c28eb6dc288253 (diff)
downloadfrost-067ea15188f20fa268255153e35c2df732fdffee.tar.gz
frost-067ea15188f20fa268255153e35c2df732fdffee.tar.bz2
frost-067ea15188f20fa268255153e35c2df732fdffee.zip
Clean up injectors and events
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.kt52
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()
}