fix: scripts injection#11
Merged
Merged
Conversation
- Expand allowedOrigins on cross-origin navigation - Register document-start scripts with https://* / http://* rules - Force re-injection on same-URL reload (clear inject markers)
There was a problem hiding this comment.
Pull request overview
This PR fixes bridge/script injection reliability issues across SPA redirects, cross-origin navigations, and same-URL reloads—primarily for Android—by expanding/refreshing allowed origins and restructuring Android WebView lifecycle handling and injection.
Changes:
- Refresh/merge allowed origins on navigation changes (including early on Android navigation start) and add tests for cross-/same-origin behaviors.
- Refactor Android WebView integration: split WebViewClient/WebChromeClient and bridge/injection responsibilities into dedicated host interfaces/classes; add forced re-injection on same-URL reload.
- Update CI to run JVM-only Android helper tests via a dedicated script.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| scripts/run_java_android_tests.sh | Adds a reusable script to compile/run JVM-only Android helper tests. |
| .github/workflows/ci.yml | Switches CI to use the new Java test runner script. |
| mobilewebview/tests/tst_mobilewebviewbackend_common.mm | Adds tests covering allowed-origins refresh behavior across navigation scenarios. |
| mobilewebview/src/js/bootstrap_page.js | Makes transport readiness “sticky” for SPAs and adds DOM observers to detect bridge readiness after <html> replacement. |
| mobilewebview/src/common/mobilewebviewbackend.cpp | Updates URL-state handling and changes allowed-origins update behavior to merge/expand. |
| mobilewebview/src/android/mobilewebviewbackend_android.cpp | Passes URL into navigation-start callback and updates origins early in the navigation lifecycle. |
| mobilewebview/android/src/org/mobilewebview/ScriptInjectionPhase.java | Introduces explicit injection phases for navigation lifecycle. |
| mobilewebview/android/src/org/mobilewebview/NavigationHost.java | Adds a host interface for navigation lifecycle callbacks. |
| mobilewebview/android/src/org/mobilewebview/NativeBridgeHost.java | Adds a host interface for the JS-to-native bridge. |
| mobilewebview/android/src/org/mobilewebview/MobileWebViewNativeBridge.java | Extracts the JavascriptInterface bridge into a dedicated class with origin validation. |
| mobilewebview/android/src/org/mobilewebview/MobileWebViewClient.java | Adds a dedicated WebViewClient that forwards lifecycle events to the host. |
| mobilewebview/android/src/org/mobilewebview/MobileWebView.java | Integrates new host interfaces, injector, and reinjection-on-reload behavior. |
| mobilewebview/android/src/org/mobilewebview/MobileWebChromeClient.java | Adds a dedicated WebChromeClient forwarding events to the host. |
| mobilewebview/android/src/org/mobilewebview/CustomSchemeUrlHandler.java | Extracts custom scheme handling into a dedicated helper. |
| mobilewebview/android/src/org/mobilewebview/ChromeHost.java | Adds a host interface for chrome callbacks. |
| mobilewebview/android/src/org/mobilewebview/BridgeState.java | Adds a snapshot container for bridge/injection state. |
| mobilewebview/android/src/org/mobilewebview/BridgeScriptInjector.java | Centralizes bridge script injection logic, including document-start registration and forced reinjection. |
| mobilewebview/android/src/org/mobilewebview/BridgeInjectorHost.java | Adds a host interface used by the script injector. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+165
to
+170
| // Merge so redirect chains (OpenSea -> Coinbase -> back) keep all origins allowed. | ||
| for (const QString &origin : origins) { | ||
| if (!origin.isEmpty() && !m_allowedOrigins.contains(origin)) { | ||
| m_allowedOrigins.append(origin); | ||
| } | ||
| } |
Comment on lines
+15
to
+21
| /** True when document-start or evaluateJavascript already loaded user scripts. */ | ||
| private static final String SCRIPTS_ALREADY_PRESENT_JS = | ||
| "(function(){return !!window.__SQ_USER_SCRIPTS_LOADED__;})();"; | ||
|
|
||
| private static final String MARK_USER_SCRIPTS_LOADED_JS = | ||
| "window.__SQ_USER_SCRIPTS_LOADED__=true;"; | ||
|
|
Comment on lines
+173
to
+175
| if (onComplete != null) { | ||
| onComplete.run(); | ||
| } |
Comment on lines
+131
to
+160
| function onElement(el) { | ||
| if (el === lastEl) { | ||
| return; | ||
| } | ||
| lastEl = el; | ||
| new MutationObserver(function() { | ||
| if (!_transportReady && isBridgeReady()) { | ||
| markTransportReady(); | ||
| } | ||
| }).observe(el, { | ||
| attributes: true, | ||
| attributeFilter: ['data-sq-bridge-ready'] | ||
| }); | ||
| if (!_transportReady && isBridgeReady()) { | ||
| markTransportReady(); | ||
| } | ||
| } | ||
| function attach() { | ||
| var el = getDocumentElement(); | ||
| if (!el) { | ||
| setTimeout(attach, 50); | ||
| return; | ||
| } | ||
| onElement(el); | ||
| new MutationObserver(function() { | ||
| var current = getDocumentElement(); | ||
| if (current && current !== lastEl) { | ||
| onElement(current); | ||
| } | ||
| }).observe(document, { childList: true, subtree: true }); |
Comment on lines
+526
to
+533
| @Override | ||
| public void onNavigationStarted(String url) { | ||
| final String navUrl = url != null ? url : ""; | ||
| final boolean sameUrlReload = navUrl.equals(mActiveNavigationUrl); | ||
| mActiveNavigationUrl = navUrl; | ||
| mBridgeInjector.resetForNavigation(sameUrlReload); | ||
| withNativePtr(ptr -> nativeOnNavigationStarted(ptr, navUrl)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(android): inject provider on SPA redirects and same-URL reload