aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt60
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt35
2 files changed, 52 insertions, 43 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 3155b11a..d9542cba 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt
@@ -2,6 +2,7 @@ package com.pitchedapps.frost.injectors
import android.graphics.Color
import android.webkit.WebView
+import ca.allanwang.kau.kotlin.lazyContext
import ca.allanwang.kau.utils.*
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
@@ -17,42 +18,41 @@ enum class CssAssets(val folder: String = "themes") : InjectorContract {
MATERIAL_LIGHT, MATERIAL_DARK, MATERIAL_AMOLED, MATERIAL_GLASS, CUSTOM, ROUND_ICONS("components")
;
- var file = "${name.toLowerCase(Locale.CANADA)}.compact.css"
- var injector: JsInjector? = null
-
- override fun inject(webView: WebView, callback: ((String) -> Unit)?) {
- if (injector == null) {
- try {
- var content = webView.context.assets.open("css/$folder/$file").bufferedReader().use { it.readText() }
- if (this == CUSTOM) {
- val bt: String
- if (Color.alpha(Prefs.bgColor) == 255) {
- bt = Prefs.bgColor.toRgbaString()
- } else {
- bt = "transparent"
- }
- content = content
- .replace("\$T\$", Prefs.textColor.toRgbaString())
- .replace("\$TT\$", Prefs.textColor.colorToBackground(0.05f).toRgbaString())
- .replace("\$A\$", Prefs.accentColor.toRgbaString())
- .replace("\$B\$", Prefs.bgColor.toRgbaString())
- .replace("\$BT\$", bt)
- .replace("\$BBT\$", Prefs.bgColor.withAlpha(51).colorToForeground(0.35f).toRgbaString())
- .replace("\$O\$", Prefs.bgColor.withAlpha(255).toRgbaString())
- .replace("\$OO\$", Prefs.bgColor.withAlpha(255).colorToForeground(0.35f).toRgbaString())
- .replace("\$D\$", Prefs.textColor.adjustAlpha(0.3f).toRgbaString())
+ var file = "${name.toLowerCase(Locale.CANADA)}.css"
+ var injector = lazyContext {
+ try {
+ var content = it.assets.open("css/$folder/$file").bufferedReader().use { it.readText() }
+ if (this == CUSTOM) {
+ val bt: String
+ if (Color.alpha(Prefs.bgColor) == 255) {
+ bt = Prefs.bgColor.toRgbaString()
+ } else {
+ bt = "transparent"
}
- injector = JsBuilder().css(content).build()
- } catch (e: FileNotFoundException) {
- L.e(e, "CssAssets file not found")
- injector = JsInjector(JsActions.EMPTY.function)
+ content = content
+ .replace("\$T\$", Prefs.textColor.toRgbaString())
+ .replace("\$TT\$", Prefs.textColor.colorToBackground(0.05f).toRgbaString())
+ .replace("\$A\$", Prefs.accentColor.toRgbaString())
+ .replace("\$B\$", Prefs.bgColor.toRgbaString())
+ .replace("\$BT\$", bt)
+ .replace("\$BBT\$", Prefs.bgColor.withAlpha(51).colorToForeground(0.35f).toRgbaString())
+ .replace("\$O\$", Prefs.bgColor.withAlpha(255).toRgbaString())
+ .replace("\$OO\$", Prefs.bgColor.withAlpha(255).colorToForeground(0.35f).toRgbaString())
+ .replace("\$D\$", Prefs.textColor.adjustAlpha(0.3f).toRgbaString())
}
+ JsBuilder().css(content).build()
+ } catch (e: FileNotFoundException) {
+ L.e(e, "CssAssets file not found")
+ JsInjector(JsActions.EMPTY.function)
}
- injector!!.inject(webView, callback)
+ }
+
+ override fun inject(webView: WebView, callback: ((String) -> Unit)?) {
+ injector(webView.context).inject(webView, callback)
}
fun reset() {
- injector = null
+ injector.invalidate()
}
}
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 8e30346b..0e46225c 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt
@@ -1,6 +1,7 @@
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.*
@@ -11,22 +12,30 @@ 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, SEARCH, TEXTAREA_LISTENER, NOTIF_MSG
+ MENU, MENU_DEBUG, CLICK_A, CONTEXT_A, MEDIA, HEADER_BADGES, TEXTAREA_LISTENER, NOTIF_MSG
;
- var file = "${name.toLowerCase(Locale.CANADA)}.min.js"
- var injector: JsInjector? = null
+ var file = "${name.toLowerCase(Locale.CANADA)}.js"
+ var injector = lazyContext {
+ try {
+ val content = it.assets.open("js/$file").bufferedReader().use { it.readText() }
+ JsBuilder().js(singleInjector(content)).build()
+ } catch (e: FileNotFoundException) {
+ L.e(e, "JsAssets file not found")
+ JsInjector(JsActions.EMPTY.function)
+ }
+ }
override fun inject(webView: WebView, callback: ((String) -> Unit)?) {
- if (injector == null) {
- try {
- val content = webView.context.assets.open("js/$file").bufferedReader().use { it.readText() }
- injector = JsBuilder().js(content).build()
- } catch (e: FileNotFoundException) {
- L.e(e, "JsAssets file not found")
- injector = JsInjector(JsActions.EMPTY.function)
- }
- }
- injector!!.inject(webView, callback)
+ injector(webView.context).inject(webView, callback)
}
+
+ private fun singleInjector(content: String) = StringBuilder().apply {
+ val name = "_frost_$name"
+ append("if (!window.hasOwnProperty(\"$name\")) {")
+ append("console.log(\"Registering $name\");")
+ append("window.$name = true;")
+ append(content)
+ append("}")
+ }.toString()
}