aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/injectors
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-01-18 23:23:56 -0500
committerAllan Wang <me@allanwang.ca>2018-01-20 16:42:37 -0500
commitd766100c297bc094491de150f24c04719ffa8f4e (patch)
treeb69863dace2fcb45d5fd25c276ca450e07305c44 /app/src/main/kotlin/com/pitchedapps/frost/injectors
parent78b3cc41e4c9f8d141ad46ee75e476fa2d177f19 (diff)
downloadfrost-d766100c297bc094491de150f24c04719ffa8f4e.tar.gz
frost-d766100c297bc094491de150f24c04719ffa8f4e.tar.bz2
frost-d766100c297bc094491de150f24c04719ffa8f4e.zip
Enhancement/speed up (#650)
* Revert back to m.facebook * Add initial speedup * Update theme * Fix link press for event status * Move web states to fb const * Fix images and email * Fix up flyweight for requests * Ensure frost request is synchronous * Prepare diff utils * Improve speed and fix blank overlay * Update comments * Add debugger and fix searchview * Theme discover pages. Resolves #654 * Fix duplicate reload * Fix image loading * Update changelog * Update tests * Rename test Update dependencies Update gitignore
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/injectors')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/injectors/CssHider.kt11
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/injectors/JsActions.kt3
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt5
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt70
5 files changed, 56 insertions, 35 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
index 015b5e2d..033e482f 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt
@@ -51,7 +51,7 @@ enum class CssAssets(val folder: String = "themes") : InjectorContract {
}
}
- override fun inject(webView: WebView, callback: ((String) -> Unit)?) {
+ override fun inject(webView: WebView, callback: (() -> Unit)?) {
injector(webView.context).inject(webView, callback)
}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssHider.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssHider.kt
index de268360..4a390d9a 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssHider.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssHider.kt
@@ -9,11 +9,10 @@ import android.webkit.WebView
*/
enum class CssHider(vararg val items: String) : InjectorContract {
CORE("[data-sigil=m_login_upsell]", "role=progressbar"),
- HEADER("#header[data-sigil=MTopBlueBarHeader]", "#header-notices", "[data-sigil*=m-promo-jewel-header]"),
- ADS(
- "article[data-xt*=sponsor]",
- "article[data-store*=sponsor]"
- ),
+ HEADER("#header", "[data-sigil=MTopBlueBarHeader]",
+ "#header-notices", "[data-sigil*=m-promo-jewel-header]"),
+ ADS("article[data-xt*=sponsor]",
+ "article[data-store*=sponsor]"),
PEOPLE_YOU_MAY_KNOW("article._d2r"),
SUGGESTED_GROUPS("article[data-ft*=\"ei\":]"),
COMPOSER("#MComposer"),
@@ -26,7 +25,7 @@ enum class CssHider(vararg val items: String) : InjectorContract {
.single(name).build()
}
- override fun inject(webView: WebView, callback: ((String) -> Unit)?) {
+ override fun inject(webView: WebView, callback: (() -> Unit)?) {
injector.inject(webView, callback)
}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsActions.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsActions.kt
index 3fa03bcc..53eb9ede 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsActions.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsActions.kt
@@ -23,5 +23,6 @@ enum class JsActions(body: String) : InjectorContract {
val function = "!function(){$body}();"
- override fun inject(webView: WebView, callback: ((String) -> Unit)?) = JsInjector(function).inject(webView, callback)
+ override fun inject(webView: WebView, callback: (() -> Unit)?) =
+ JsInjector(function).inject(webView, callback)
} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt
index 74ab7d37..9ff129d4 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt
@@ -12,7 +12,8 @@ import java.util.*
* 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
+ MENU, MENU_DEBUG, CLICK_A, CONTEXT_A, MEDIA, HEADER_BADGES, TEXTAREA_LISTENER, NOTIF_MSG,
+ DOCUMENT_WATCHER
;
var file = "${name.toLowerCase(Locale.CANADA)}.js"
@@ -26,7 +27,7 @@ enum class JsAssets : InjectorContract {
}
}
- override fun inject(webView: WebView, callback: ((String) -> Unit)?) {
+ override fun inject(webView: WebView, callback: (() -> Unit)?) {
injector(webView.context).inject(webView, callback)
}
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 dd73209f..2d067e44 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt
@@ -3,7 +3,7 @@ package com.pitchedapps.frost.injectors
import android.webkit.WebView
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.web.FrostWebViewClient
-import io.reactivex.Observable
+import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.subjects.SingleSubject
import org.apache.commons.text.StringEscapeUtils
@@ -26,30 +26,35 @@ class JsBuilder {
}
fun single(tag: String): JsBuilder {
- this.tag = tag
+ this.tag = "_frost_${tag.toLowerCase(Locale.CANADA)}"
return this
}
fun build() = JsInjector(toString())
override fun toString(): String {
- val builder = StringBuilder().append("!function(){")
- if (css.isNotBlank()) {
- val cssMin = css.replace(Regex("\\s*\n\\s*"), "")
- builder.append("var a=document.createElement('style');a.innerHTML='$cssMin';document.head.appendChild(a);")
+ val tag = this.tag
+ val builder = StringBuilder().apply {
+ append("!function(){")
+ if (css.isNotBlank()) {
+ val cssMin = css.replace(Regex("\\s*\n\\s*"), "")
+ append("var a=document.createElement('style');")
+ append("a.innerHTML='$cssMin';")
+ if (tag != null) append("a.id='$tag';")
+ append("document.head.appendChild(a);")
+ }
+ if (js.isNotBlank())
+ append(js)
}
- if (js.isNotBlank())
- builder.append(js)
var content = builder.append("}()").toString()
- if (tag != null) content = singleInjector(tag!!, content)
+ if (tag != null) content = singleInjector(tag, content)
return content
}
private fun singleInjector(tag: String, content: String) = StringBuilder().apply {
- val name = "_frost_${tag.toLowerCase(Locale.CANADA)}"
- append("if (!window.hasOwnProperty(\"$name\")) {")
- append("console.log(\"Registering $name\");")
- append("window.$name = true;")
+ append("if (!window.hasOwnProperty(\"$tag\")) {")
+ append("console.log(\"Registering $tag\");")
+ append("window.$tag = true;")
append(content)
append("}")
}.toString()
@@ -60,7 +65,7 @@ class JsBuilder {
*/
interface InjectorContract {
fun inject(webView: WebView) = inject(webView, null)
- fun inject(webView: WebView, callback: ((String) -> Unit)?)
+ fun inject(webView: WebView, callback: (() -> Unit)?)
/**
* Toggle the injector (usually through Prefs
* If false, will fallback to an empty action
@@ -71,24 +76,39 @@ interface InjectorContract {
/**
* Helper method to inject multiple functions simultaneously with a single callback
*/
-fun WebView.jsInject(vararg injectors: InjectorContract, callback: ((Array<String>) -> Unit) = {}) {
+fun WebView.jsInject(vararg injectors: InjectorContract, callback: ((Int) -> Unit)? = null) {
val validInjectors = injectors.filter { it != JsActions.EMPTY }
- if (validInjectors.isEmpty()) return callback(emptyArray())
- val observables = Array(validInjectors.size, { SingleSubject.create<String>() })
- L.d { "Injecting ${observables.size} items" }
- Observable.zip<String, Array<String>>(observables.map(SingleSubject<String>::toObservable),
- { it.map(Any::toString).toTypedArray() })
- .subscribeOn(AndroidSchedulers.mainThread()).subscribe({ callback(it) })
- (0 until validInjectors.size).forEach { i -> validInjectors[i].inject(this, { observables[i].onSuccess(it) }) }
+ if (validInjectors.isEmpty()) {
+ callback?.invoke(0)
+ return
+ }
+ L.d { "Injecting ${validInjectors.size} items" }
+ if (callback == null) {
+ validInjectors.forEach { it.inject(this) }
+ return
+ }
+ val observables = Array(validInjectors.size, { SingleSubject.create<Unit>() })
+ Single.zip<Unit, Int>(observables.asList(), { it.size })
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe { res, _ ->
+ callback(res)
+ }
+ (0 until validInjectors.size).forEach { i ->
+ validInjectors[i].inject(this, {
+ observables[i].onSuccess(Unit)
+ })
+ }
}
-fun FrostWebViewClient.jsInject(vararg injectors: InjectorContract, callback: ((Array<String>) -> Unit) = {}) = web.jsInject(*injectors, callback = callback)
+fun FrostWebViewClient.jsInject(vararg injectors: InjectorContract,
+ callback: ((Int) -> Unit)? = null)
+ = web.jsInject(*injectors, callback = callback)
/**
* Wrapper class to convert a function into an injector
*/
class JsInjector(val function: String) : InjectorContract {
- override fun inject(webView: WebView, callback: ((String) -> Unit)?) {
- webView.evaluateJavascript(function, { value -> callback?.invoke(value) })
+ override fun inject(webView: WebView, callback: (() -> Unit)?) {
+ webView.evaluateJavascript(function, { callback?.invoke() })
}
} \ No newline at end of file