From e31d2c9934e714f7b5718eb7b6921c5c1c8d0e46 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 15 Dec 2023 14:25:00 +0100 Subject: [PATCH 1/2] feat(android)!: Use addDocumentStartJavaScript for injecting javascript --- .../src/main/java/com/getcapacitor/Bridge.java | 11 ++++++++++- .../java/com/getcapacitor/WebViewLocalServer.java | 10 +++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index f46f684db..80bafdfc4 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -26,6 +26,9 @@ import androidx.core.app.ActivityCompat; import androidx.core.content.pm.PackageInfoCompat; import androidx.fragment.app.Fragment; +import androidx.webkit.WebViewCompat; +import androidx.webkit.WebViewFeature; + import com.getcapacitor.android.R; import com.getcapacitor.annotation.CapacitorPlugin; import com.getcapacitor.annotation.Permission; @@ -40,6 +43,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -246,7 +250,12 @@ private void loadWebView() { final boolean html5mode = this.config.isHTML5Mode(); // Start the local web server - localServer = new WebViewLocalServer(context, this, getJSInjector(), authorities, html5mode); + JSInjector injector = getJSInjector(); + if (WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) { + WebViewCompat.addDocumentStartJavaScript(webView, injector.getScriptString(), Collections.singleton(appUrl)); + injector = null; + } + localServer = new WebViewLocalServer(context, this, injector, authorities, html5mode); localServer.hostAssets(DEFAULT_WEB_ASSET_DIR); Logger.debug("Loading app at " + appUrl); diff --git a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java index 6ef123500..4b073e20b 100755 --- a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +++ b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java @@ -277,7 +277,9 @@ private WebResourceResponse handleLocalRequest(WebResourceRequest request, PathH return null; } - responseStream = jsInjector.getInjectedStream(responseStream); + if (jsInjector != null) { + responseStream = jsInjector.getInjectedStream(responseStream); + } int statusCode = getStatusCode(responseStream, handler.getStatusCode()); return new WebResourceResponse( @@ -305,7 +307,7 @@ private WebResourceResponse handleLocalRequest(WebResourceRequest request, PathH InputStream responseStream = new LollipopLazyInputStream(handler, request); // TODO: Conjure up a bit more subtlety than this - if (ext.equals(".html")) { + if (ext.equals(".html") && jsInjector != null) { responseStream = jsInjector.getInjectedStream(responseStream); } @@ -369,7 +371,9 @@ private WebResourceResponse handleProxyRequest(WebResourceRequest request, PathH } } InputStream responseStream = conn.getInputStream(); - responseStream = jsInjector.getInjectedStream(responseStream); + if (jsInjector != null) { + responseStream = jsInjector.getInjectedStream(responseStream); + } return new WebResourceResponse( "text/html", handler.getEncoding(), From ef87443613a481bb7586884851998a046001e163 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 15 Dec 2023 14:32:09 +0100 Subject: [PATCH 2/2] fmt --- android/capacitor/src/main/java/com/getcapacitor/Bridge.java | 1 - 1 file changed, 1 deletion(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 80bafdfc4..a1d79e2c5 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -28,7 +28,6 @@ import androidx.fragment.app.Fragment; import androidx.webkit.WebViewCompat; import androidx.webkit.WebViewFeature; - import com.getcapacitor.android.R; import com.getcapacitor.annotation.CapacitorPlugin; import com.getcapacitor.annotation.Permission;