aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/injectors
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-01 00:21:04 -0700
committerAllan Wang <me@allanwang.ca>2017-06-01 00:21:04 -0700
commit4cbcabb122e4bdf5d8e3eb67213ec7270d7aa9f0 (patch)
tree93ed7ff19412da306c6e7abf7f38fe035ab88e10 /app/src/main/kotlin/com/pitchedapps/frost/injectors
parent8618670b82641d5fbaec9c333f1290bab429ce27 (diff)
downloadfrost-4cbcabb122e4bdf5d8e3eb67213ec7270d7aa9f0.tar.gz
frost-4cbcabb122e4bdf5d8e3eb67213ec7270d7aa9f0.tar.bz2
frost-4cbcabb122e4bdf5d8e3eb67213ec7270d7aa9f0.zip
working injectors and redid tabs db
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/injectors')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt25
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/injectors/JsBuilder.kt36
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt71
3 files changed, 93 insertions, 39 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt
new file mode 100644
index 00000000..74a55888
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt
@@ -0,0 +1,25 @@
+package com.pitchedapps.frost.injectors
+
+import android.webkit.WebView
+import com.pitchedapps.frost.utils.L
+
+/**
+ * Created by Allan Wang on 2017-05-31.
+ */
+enum class CssAssets(val file: String) {
+ BASE("facebook.min.css");
+
+ var content: String? = null
+ var injector: JsInjector? = null
+
+ fun inject(webView: WebView, callback: ((String) -> Unit)?) {
+ if (injector == null) {
+ if (content == null)
+ content = webView.context.assets.open(file).bufferedReader().use { it.readText() }
+ injector = JsBuilder().css(content!!).build()
+ }
+ injector!!.inject(webView, callback)
+ L.d("CSS ${injector!!.function}")
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsBuilder.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsBuilder.kt
deleted file mode 100644
index 9844e5d5..00000000
--- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsBuilder.kt
+++ /dev/null
@@ -1,36 +0,0 @@
-package com.pitchedapps.frost.injectors
-
-import android.webkit.WebView
-
-/**
- * Created by Allan Wang on 2017-05-31.
- */
-class JsBuilder {
- private val builder = StringBuilder()
-
- init {
- builder.append("javascript:(function(){")
- }
-
- private fun getElementById(id: String) = "document.getElementById(\"$id\")"
-
- private fun hideElementById(id: String) {
- builder.append(getElementById(id)).append(".style.display=\"none\";")
- }
-
- fun hideElementById(vararg ids: String) {
- ids.forEach { hideElementById(it) }
- }
-
- fun build() = builder.toString() + "})()"
-
- fun inject(webView: WebView) {
- webView.loadUrl(build())
- }
-
- fun removeAllStyles() {
-
- }
-
- override fun toString() = build()
-} \ No newline at end of file
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 2d1659cc..c7b4eaf8 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt
@@ -5,8 +5,73 @@ import android.webkit.WebView
/**
* Created by Allan Wang on 2017-05-31.
*/
-object JsInjector {
- fun inject(webView: WebView) {
+enum class JsActions(val function: String) {
+ HIDE("style.display='none'"),
+ REMOVE("remove()")
+}
+class VariableGenerator {
+
+ var count = 0
+
+ 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
+ }
+}
+
+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
+ }
+
+ 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
+ builder.append("var $name=document.createElement('style');$name.innerHTML='$css';document.head.appendChild($name);")
+ }
+ return builder.append("}()").toString()
+ }
+}
+
+class JsInjector(val function: String) {
+ fun inject(webView: WebView, callback: ((String) -> Unit)? = null) {
+ webView.evaluateJavascript(function, { value -> callback?.invoke(value) })
}
-} \ No newline at end of file
+}