From 8866bf90c76e90c2f6411a0848e75076a38d0c7c Mon Sep 17 00:00:00 2001 From: gdlbo <41114720+gdlbo@users.noreply.github.com> Date: Wed, 14 Feb 2024 22:10:33 +0300 Subject: [PATCH] refactored webview injector speedup by runtime injection evaluateJavascript --- .../lite/utils/WebViewColoringUtils.java | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/ru/vtosters/lite/utils/WebViewColoringUtils.java b/app/src/main/java/ru/vtosters/lite/utils/WebViewColoringUtils.java index 6052d0cda..101bfdb99 100644 --- a/app/src/main/java/ru/vtosters/lite/utils/WebViewColoringUtils.java +++ b/app/src/main/java/ru/vtosters/lite/utils/WebViewColoringUtils.java @@ -3,6 +3,7 @@ package ru.vtosters.lite.utils; import android.webkit.WebView; import java.io.IOException; +import java.io.InputStream; import static ru.vtosters.hooks.other.ThemesUtils.*; import static ru.vtosters.lite.utils.AndroidUtils.getGlobalContext; @@ -13,25 +14,42 @@ public class WebViewColoringUtils { public static String loadedCSSAmoled = ""; private static String readCSS(String str) { - try (var is = getGlobalContext().getAssets().open(str)) { - return IOUtils.readAllLines(is) - .replace("%header_background%", hex(getHeaderBackground())) - .replace("%background_content%", hex(getBackgroundContent())) - .replace("%header_text%", hex(getHeaderText())) - .replace("%accent%", hex(getAccentColor())) - .replace("%background_page%", hex(getBackgroundPage())) - .replace("%content_tint_background%", hex(getColorFromAttr(com.vtosters.lite.R.attr.content_tint_background))) - .replace("%button_primary_background%", hex(getColorFromAttr(com.vtosters.lite.R.attr.button_primary_background))) - .replace("%button_outline_foreground%", hex(getColorFromAttr(com.vtosters.lite.R.attr.button_outline_foreground))) - .replace("%button_outline_border%", hex(getColorFromAttr(com.vtosters.lite.R.attr.button_outline_border))) - .replace("%field_background%", hex(getColorFromAttr(com.vtosters.lite.R.attr.field_background))) - .replace("%field_border%", hex(getColorFromAttr(com.vtosters.lite.R.attr.field_border))) - .replace("%text_primary%", hex(getTextAttr())); + try (InputStream is = getGlobalContext().getAssets().open(str)) { + return replaceWebAttrs(IOUtils.readAllLines(is)); } catch (IOException e) { return ""; } } + private static String replaceWebAttrs(String css) { + int headerBackground = getHeaderBackground(); + int backgroundContent = getBackgroundContent(); + int headerText = getHeaderText(); + int accentColor = getAccentColor(); + int backgroundPage = getBackgroundPage(); + int contentTintBackground = getColorFromAttr(com.vtosters.lite.R.attr.content_tint_background); + int buttonPrimaryBackground = getColorFromAttr(com.vtosters.lite.R.attr.button_primary_background); + int buttonOutlineForeground = getColorFromAttr(com.vtosters.lite.R.attr.button_outline_foreground); + int buttonOutlineBorder = getColorFromAttr(com.vtosters.lite.R.attr.button_outline_border); + int fieldBackground = getColorFromAttr(com.vtosters.lite.R.attr.field_background); + int fieldBorder = getColorFromAttr(com.vtosters.lite.R.attr.field_border); + int textPrimary = getTextAttr(); + + return css + .replace("%header_background%", hex(headerBackground)) + .replace("%background_content%", hex(backgroundContent)) + .replace("%header_text%", hex(headerText)) + .replace("%accent%", hex(accentColor)) + .replace("%background_page%", hex(backgroundPage)) + .replace("%content_tint_background%", hex(contentTintBackground)) + .replace("%button_primary_background%", hex(buttonPrimaryBackground)) + .replace("%button_outline_foreground%", hex(buttonOutlineForeground)) + .replace("%button_outline_border%", hex(buttonOutlineBorder)) + .replace("%field_background%", hex(fieldBackground)) + .replace("%field_border%", hex(fieldBorder)) + .replace("%text_primary%", hex(textPrimary)); + } + public static void load() { loadedCSS = readCSS("vt_ui_accent.css"); loadedCSSAmoled = readCSS("vt_ui_oled.css"); @@ -39,6 +57,8 @@ public class WebViewColoringUtils { } public static void inject(WebView webView, String str) { - webView.loadUrl("javascript:(function() {let parent = document.head || document.documentElement; let style = document.getElementById('custom-style'); if (!style) {style = parent.appendChild(document.createElement('style')); style.id = 'custom-style';} style.innerText = window.atob('" + str + "');})()"); + String injectOwnStyles = "javascript:(function() {let parent = document.head || document.documentElement; let style = document.getElementById('custom-style'); if (!style) {style = parent.appendChild(document.createElement('style')); style.id = 'custom-style';} style.innerText = window.atob('%s');})()"; + String jsStyleInjector = String.format(injectOwnStyles, str); + webView.evaluateJavascript(jsStyleInjector, null); } -} +} \ No newline at end of file