Skip to content

feat(WebViewNavigator): add evaluateJavaScript (#20)#28

Open
jim-daf wants to merge 1 commit into
KevinnZou:mainfrom
jim-daf:fix/issue-20-evaluate-javascript
Open

feat(WebViewNavigator): add evaluateJavaScript (#20)#28
jim-daf wants to merge 1 commit into
KevinnZou:mainfrom
jim-daf:fix/issue-20-evaluate-javascript

Conversation

@jim-daf

@jim-daf jim-daf commented May 2, 2026

Copy link
Copy Markdown

What

Adds evaluateJavaScript(script, callback) to WebViewNavigator so callers can run JavaScript in the underlying WebView from 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-multiplatform version exposes it). Without it, integrators have to either fork the library or wire up their own AndroidView factory just to invoke a JS snippet.

How

  • Added a new NavigationEvent.EvaluateJavaScript data class alongside the existing LoadUrl / PostUrl events.
  • Routed it through handleNavigationEvents so the call is dispatched on Dispatchers.Main like every other navigation action.
  • Exposed WebViewNavigator.evaluateJavaScript(script, callback). The optional Kotlin lambda is wrapped in a ValueCallback<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

val navigator = rememberWebViewNavigator()
// ...
navigator.evaluateJavaScript("document.title") { result ->
    Log.d("WebView", "title = $result")
}

Refs #20

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
@jim-daf jim-daf marked this pull request as ready for review May 2, 2026 20:23
Copilot AI review requested due to automatic review settings May 2, 2026 20:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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() on Dispatchers.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 ?: "") }
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