Skip to content

feat: add eval_with_callback to Webview and WebviewWindow#14925

Merged
Legend-Master merged 3 commits intotauri-apps:devfrom
lanyeeee:feature/eval_with_callback
Apr 1, 2026
Merged

feat: add eval_with_callback to Webview and WebviewWindow#14925
Legend-Master merged 3 commits intotauri-apps:devfrom
lanyeeee:feature/eval_with_callback

Conversation

@lanyeeee
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Contributor

github-actions Bot commented Feb 11, 2026

Package Changes Through ecd94c1

There are 9 changes which include tauri-macos-sign with patch, tauri-build with patch, tauri with minor, tauri-runtime with minor, tauri-runtime-wry with minor, tauri-bundler with minor, tauri-cli with minor, @tauri-apps/cli with minor, tauri-utils with minor

Planned Package Versions

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

package current next
tauri-utils 2.8.3 2.9.0
tauri-macos-sign 2.3.3 2.3.4
tauri-bundler 2.8.1 2.9.0
tauri-runtime 2.10.1 2.11.0
tauri-runtime-wry 2.10.1 2.11.0
tauri-codegen 2.5.5 2.5.6
tauri-macros 2.5.5 2.5.6
tauri-plugin 2.5.4 2.5.5
tauri-build 2.5.6 2.5.7
tauri 2.10.3 2.11.0
@tauri-apps/cli 2.10.1 2.11.0
tauri-cli 2.10.1 2.11.0

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
Copy Markdown
Contributor 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
Copy Markdown
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

Copy link
Copy Markdown
Contributor

@Legend-Master Legend-Master left a comment

Choose a reason for hiding this comment

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

Looking good from a glance, just some nitpicks on the docs, thanks!

Do you mind also adding a change file?

https://github.com/tauri-apps/tauri/blob/dev/.changes/README.md

Comment thread crates/tauri/src/webview/mod.rs Outdated
Comment thread crates/tauri-runtime/src/lib.rs Outdated
Comment thread crates/tauri/src/webview/webview_window.rs Outdated
Copy link
Copy Markdown
Contributor

@Legend-Master Legend-Master left a comment

Choose a reason for hiding this comment

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

Thanks

@Legend-Master Legend-Master merged commit b27be06 into tauri-apps:dev Apr 1, 2026
19 checks passed
@github-project-automation github-project-automation Bot moved this from 📬Proposal to 🔎 In audit in Roadmap Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🔎 In audit

Development

Successfully merging this pull request may close these issues.

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

3 participants