Skip to content

feat: add eval_with_callback to Webview and WebviewWindow#14925

Open
lanyeeee wants to merge 1 commit intotauri-apps:devfrom
lanyeeee:feature/eval_with_callback
Open

feat: add eval_with_callback to Webview and WebviewWindow#14925
lanyeeee wants to merge 1 commit intotauri-apps:devfrom
lanyeeee:feature/eval_with_callback

Conversation

@lanyeeee
Copy link

@lanyeeee lanyeeee commented Feb 10, 2026

close #5441

This PR adds a new method eval_with_callback to tauri::webview::Webview and tauri::webview::WebviewWindow, providing a way to evaluate JavaScript in the webview and receive the evaluation result (serialized as a JSON string) via a callback function.

The feature addresses the long-standing request in #5441

The change is non-breaking, fully backward-compatible, and ready for integration.

Implementation Details

  • Added eval_script_with_callback to the tauri_runtime::WebviewDispatch trait.
  • Implemented the dispatcher method in tauri-runtime-wry by:
    • Introducing a new WebviewMessage::EvaluateScriptWithCallback variant (with conditional fields for tracing support, mirroring the pattern used for WebviewMessage::EvaluateScript).
    • Sending the message via the existing user message channel.
    • Handling the message in handle_user_message by forwarding to wry::WebView::evaluate_script_with_callback.
  • Updated the mock runtime to record the evaluated script (callback is not invoked, as it's a mock).
  • Exposed a convenient public API eval_with_callback on both tauri::webview::Webview and tauri::webview::WebviewWindow.
  • Added documentation comments explaining the behavior, including JSON serialization and platform-specific notes.

The implementation closely follows the existing pattern for eval_script to ensure consistency, minimal code duplication, and proper tracing span propagation when the tracing feature is enabled.

Platform Support (Android)

The underlying wry::WebView::evaluate_script_with_callback method is documented as "Android: Not implemented yet."

In my own real-world testing on Android devices, the method actually works correctly — the callback is invoked with the expected JSON-serialized result, and no errors occur.

However, to respect the wry documentation and avoid promising unsupported behavior, eval_with_callback documentation includes the same warning: "Android: Not implemented yet." Users should be aware that while it may work in practice (as verified in testing), it is not officially guaranteed by wry.

Tested Example

The following command was tested successfully on both Windows and Android, correctly returning the evaluated result on both platforms:

use std::sync::mpsc::channel;

use tauri::{AppHandle, Manager};

#[tauri::command(async)]
fn get_document_html(app: AppHandle) -> Result<String, String> {
    let window = app.get_webview_window("main").unwrap();

    let (tx, rx) = channel::<String>();

    let _ = window.eval_with_callback("document.documentElement.innerHTML", move |res| {
        tx.send(res).unwrap();
    });

    let result = rx.recv().unwrap();

    Ok(result)
}

@github-actions
Copy link
Contributor

Package Changes Through e6b376b

There are 3 changes which include tauri-bundler with patch, tauri-cli with patch, @tauri-apps/cli with patch

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
tauri-bundler 2.8.0 2.8.1
@tauri-apps/cli 2.10.0 2.10.1
tauri-cli 2.10.0 2.10.1

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@lanyeeee
Copy link
Author

lanyeeee commented Feb 11, 2026

There are two test failures in this run, but they appear to be flaky. I pushed the exact same commit to my fork and all tests passed there without issues.

Could you please re-run the GitHub Actions? @FabianLars

@FabianLars
Copy link
Member

don't worry about it. the android builds regularly fail cause the cache breaks for some reason. meant to delete the caches for another PR but the mobile gh app is horrible x) will do now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 📬Proposal

Development

Successfully merging this pull request may close these issues.

[feat] Call javascript function from Rust (expose wry's eval_with_callback in tauri)

2 participants