fix(JBCallbackImpl): use evaluateJavascript so delayed callback.apply works (#34)#41
Open
jim-daf wants to merge 1 commit intopengwei1024:masterfrom
Open
Conversation
… works (pengwei1024#34) A synchronous callback.apply works because it runs while the JS prompt is still pending and the WebView treats the loadUrl javascript URL as part of the active call. A delayed apply (the classic case is after a network request finishes) shows nothing because loadUrl("javascript:...") is unreliable on modern WebView when the call did not originate from a JS event. evaluateJavascript was added in API 19 for exactly this scenario. This change prefers it for android.webkit.WebView and keeps loadUrl as a fallback for pre-19 builds and the IWebView wrapper.
There was a problem hiding this comment.
Pull request overview
This PR fixes lost asynchronous JBCallback.apply() deliveries by switching from WebView.loadUrl("javascript:...") to WebView.evaluateJavascript(...) on API 19+ (keeping loadUrl as a fallback for older Android versions and for IWebView).
Changes:
- Use
WebView.evaluateJavascriptfor callback execution when running on API 19+. - Strip the
"javascript:"prefix before callingevaluateJavascript(since it expects raw script). - Keep the existing
loadUrlbehavior for pre-KitKat devices andIWebViewimplementations.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Async callback.apply lost because loadUrl is unreliable (refs #34)
Resolves #34
Reproducer from the issue
The OP calls a native method, kicks off a 3 second delayed handler, and tries to deliver the result through the same JBCallback:
Root cause
JBCallbackImpl.applybuilds a JS snippet and pushes it to the WebView withloadUrl("javascript:..."):WebView.loadUrl("javascript:...")is the legacy API. It is reliable when called from inside anonJsPrompt(orshouldOverrideUrlLoading) handler because the WebView still has an active dispatch frame. Once the call is asynchronous (apostDelayed, a network callback, anything off the bridge entry frame) modern WebView builds drop or coalesce the URL load with no visible effect. That is exactly what the OP is hitting.WebView.evaluateJavascriptwas added in API 19 to provide a reliable async-safe way to run JS in the active document. Using it here makes the delayedcallback.applyarrive on the JS side every time.The fix
Notes:
evaluateJavascriptwants raw script, not ajavascript:URL, so the leading"javascript:"prefix that the existing builder emits is stripped before the call.IWebViewbranch is left onloadUrlbecause that abstraction is implementer defined and we cannot assume anevaluateJavascriptequivalent.Files changed
jsbridge/src/main/java/com/apkfuns/jsbridge/JBCallbackImpl.java