feat(WebViewNavigator): add evaluateJavaScript (#20)#28
Open
jim-daf wants to merge 1 commit into
Open
Conversation
Adds an evaluateJavaScript(script, callback) method to WebViewNavigator, porting the API from compose-webview-multiplatform so callers can run JS from outside the composable without having to fork the library. The call is dispatched as a NavigationEvent and executed on the UI thread via WebView.evaluateJavascript. The optional Kotlin callback is wrapped in a ValueCallback<String> so consumers stay on a Kotlin-only API surface. Refs KevinnZou#20
There was a problem hiding this comment.
Pull request overview
Adds the missing WebViewNavigator.evaluateJavaScript(script, callback) API to the Android artifact, aligning the navigator surface with what the README/issue #20 expects by routing JS evaluation through the existing navigation-event pipeline.
Changes:
- Introduced
NavigationEvent.EvaluateJavaScript(script, callback)to represent JS evaluation requests. - Handled the new event in
WebView.handleNavigationEvents()onDispatchers.Main. - Exposed
WebViewNavigator.evaluateJavaScript(...)as a public API with optional Kotlin callback.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * Evaluates the given JavaScript in the context of the currently displayed page. | ||
| * | ||
| * @param script The JavaScript to evaluate. | ||
| * @param callback Optional callback invoked with the evaluation result on the UI thread. |
Comment on lines
+68
to
+71
| data class EvaluateJavaScript( | ||
| val script: String, | ||
| val callback: ((String) -> Unit)? | ||
| ) : NavigationEvent |
| is NavigationEvent.EvaluateJavaScript -> { | ||
| val cb = event.callback | ||
| val valueCallback: ValueCallback<String>? = | ||
| if (cb == null) null else ValueCallback { value -> cb(value ?: "") } |
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.
What
Adds
evaluateJavaScript(script, callback)toWebViewNavigatorso callers can run JavaScript in the underlyingWebViewfrom outside the composable.Why
Issue #20 reports that the README advertises this capability, but the function is missing from the published Android-only artifact (only the
compose-webview-multiplatformversion exposes it). Without it, integrators have to either fork the library or wire up their ownAndroidViewfactory just to invoke a JS snippet.How
NavigationEvent.EvaluateJavaScriptdata class alongside the existingLoadUrl/PostUrlevents.handleNavigationEventsso the call is dispatched onDispatchers.Mainlike every other navigation action.WebViewNavigator.evaluateJavaScript(script, callback). The optional Kotlin lambda is wrapped in aValueCallback<String>internally so consumers stay on a Kotlin-only API surface.The signature mirrors the multiplatform version so the two libraries stay source-compatible.
Usage
Refs #20