refactored webview injector

speedup by runtime injection evaluateJavascript
This commit is contained in:
gdlbo
2024-02-14 22:10:33 +03:00
parent eab5b7074e
commit 8866bf90c7
@@ -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);
}
}
}