fix(JsBridgeImpl): fire OnJsBridgeReady only once per JS context (#35)#42
Open
jim-daf wants to merge 1 commit intopengwei1024:masterfrom
Open
fix(JsBridgeImpl): fire OnJsBridgeReady only once per JS context (#35)#42jim-daf wants to merge 1 commit intopengwei1024:masterfrom
jim-daf wants to merge 1 commit intopengwei1024:masterfrom
Conversation
…gwei1024#35) The injected bridge script ended with an unconditional "<protocol>.OnJsBridgeReady();" call. Re-running injectJs on the same JS context (for example WebViewClient.onPageFinished firing multiple times for the same page or sub frames triggering an extra inject) re-fired the ready callback, so any JS handler registered on it ran once per injection. This change wraps the injection in if (!window.<protocol>) { ... } which makes a genuine new page load (fresh JS context) still set the bridge up exactly once, and turns repeat injection into a no-op so OnJsBridgeReady fires only on the actual ready transition.
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #35 where OnJsBridgeReady can fire multiple times for a single navigation due to repeated JS injection calls (e.g., multiple onPageFinished/subframe events), by making the injection idempotent per JS context.
Changes:
- Wrap the injected bridge installation script in a
if (!window.<protocol>) { ... }guard so the bridge is created andOnJsBridgeReadyis fired only once per JS context.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+223
to
225
| builder.append("if(!window.").append(newProtocol).append("){"); | ||
| builder.append("var ").append(className).append("=function(){"); | ||
| // 注入通用方法 |
There was a problem hiding this comment.
PR description mentions clean() sets window.<protocol> back to undefined, but the implementation uses <protocol>=undefined; (no window.). To avoid confusion and keep the contract clear, update either the description or the code so both refer to the same form (window.<protocol> vs global identifier).
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.
OnJsBridgeReady firing multiple times (refs #35)
Resolves #35
Why it fires more than once
JsBridgeImpl.getInjectJsStringends with an unconditional ready call:The script is pushed in by
onInjectJs, which the host app typically calls fromWebViewClient.onPageFinishedorWebChromeClient.onProgressChanged. Both can fire more than once for a single navigation (sub frame finish, redirect, late content). Every extra invocation re-runs the whole script, which re-creates the bridge object and re-firesOnJsBridgeReady, so any JS handler registered on it executes once per injection. That is exactly the symptom in the issue.The fix
Wrap the whole injection in a JS guard keyed on the configured protocol:
Behavior:
window.<protocol>is undefined, the guard passes, the bridge is installed,OnJsBridgeReadyfires once. Same as today.window.<protocol>already references the bridge instance, the guard short-circuits, the script does nothing.OnJsBridgeReadydoes not fire again.clean()already nulls the protocol (it evaluates a JS assignment that setswindow.<protocol>back toundefined) so a deliberate re-init still works.Files changed
jsbridge/src/main/java/com/apkfuns/jsbridge/JsBridgeImpl.java