Skip to content

fix: scripts injection#11

Merged
friofry merged 2 commits into
masterfrom
ab/issue-20602-sync-onstart-clean
May 20, 2026
Merged

fix: scripts injection#11
friofry merged 2 commits into
masterfrom
ab/issue-20602-sync-onstart-clean

Conversation

@friofry
Copy link
Copy Markdown
Collaborator

@friofry friofry commented May 20, 2026

fix(android): inject provider on SPA redirects and same-URL reload

  • Expand allowedOrigins on cross-origin navigation
  • Register document-start scripts with https://* / http://* rules
  • Force re-injection on same-URL reload (clear inject markers)

friofry added 2 commits May 21, 2026 01:39
- Expand allowedOrigins on cross-origin navigation
- Register document-start scripts with https://* / http://* rules
- Force re-injection on same-URL reload (clear inject markers)
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));
}
@friofry friofry merged commit 53d32e0 into master May 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants