Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android)!: Use addDocumentStartJavaScript for injecting javascript #7135

Merged
merged 7 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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;
Expand All @@ -40,6 +42,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;
Expand Down Expand Up @@ -246,7 +249,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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(),
Expand Down