Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,15 @@ pub enum WebviewMessage {
EvaluateScript(String),
#[cfg(all(feature = "tracing", not(target_os = "android")))]
EvaluateScript(String, Sender<()>, tracing::Span),
#[cfg(not(all(feature = "tracing", not(target_os = "android"))))]
EvaluateScriptWithCallback(String, Box<dyn Fn(String) + Send + 'static>),
#[cfg(all(feature = "tracing", not(target_os = "android")))]
EvaluateScriptWithCallback(
String,
Box<dyn Fn(String) + Send + 'static>,
Sender<()>,
tracing::Span,
),
CookiesForUrl(Url, Sender<Result<Vec<tauri_runtime::Cookie<'static>>>>),
Cookies(Sender<Result<Vec<tauri_runtime::Cookie<'static>>>>),
SetCookie(tauri_runtime::Cookie<'static>),
Expand Down Expand Up @@ -1782,6 +1791,46 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
)
}

#[cfg(all(feature = "tracing", not(target_os = "android")))]
fn eval_script_with_callback<S: Into<String>>(
&self,
script: S,
callback: impl Fn(String) + Send + 'static,
) -> Result<()> {
// use a channel so the EvaluateScript task uses the current span as parent
let (tx, rx) = channel();
getter!(
self,
rx,
Message::Webview(
*self.window_id.lock().unwrap(),
self.webview_id,
WebviewMessage::EvaluateScriptWithCallback(
script.into(),
Box::new(callback),
tx,
tracing::Span::current(),
),
)
)
}

#[cfg(not(all(feature = "tracing", not(target_os = "android"))))]
fn eval_script_with_callback<S: Into<String>>(
&self,
script: S,
callback: impl Fn(String) + Send + 'static,
) -> Result<()> {
send_user_message(
&self.context,
Message::Webview(
*self.window_id.lock().unwrap(),
self.webview_id,
WebviewMessage::EvaluateScriptWithCallback(script.into(), Box::new(callback)),
),
)
}

fn set_zoom(&self, scale_factor: f64) -> Result<()> {
send_user_message(
&self.context,
Expand Down Expand Up @@ -3644,6 +3693,20 @@ fn handle_user_message<T: UserEvent>(
log::error!("{e}");
}
}
#[cfg(all(feature = "tracing", not(target_os = "android")))]
WebviewMessage::EvaluateScriptWithCallback(script, callback, tx, span) => {
let _span = span.entered();
if let Err(e) = webview.evaluate_script_with_callback(&script, callback) {
log::error!("{e}");
}
tx.send(()).unwrap();
}
#[cfg(not(all(feature = "tracing", not(target_os = "android"))))]
WebviewMessage::EvaluateScriptWithCallback(script, callback) => {
if let Err(e) = webview.evaluate_script_with_callback(&script, callback) {
log::error!("{e}");
}
}
WebviewMessage::Navigate(url) => {
if let Err(e) = webview.load_url(url.as_str()) {
log::error!("failed to navigate to url {}: {}", url, e);
Expand Down
12 changes: 12 additions & 0 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,18 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
/// Executes javascript on the window this [`WindowDispatch`] represents.
fn eval_script<S: Into<String>>(&self, script: S) -> Result<()>;

/// Evaluate JavaScript with callback function on the window this [`WindowDispatch`] represents.
/// The evaluation result will be serialized into a JSON string and passed to the callback function.
///
/// Exception is ignored because of the limitation on windows. You can catch it yourself and return as string as a workaround.
///
/// - **Android:** Not implemented yet.
fn eval_script_with_callback<S: Into<String>>(
&self,
script: S,
callback: impl Fn(String) + Send + 'static,
) -> Result<()>;

/// Moves the webview to the given window.
fn reparent(&self, window_id: WindowId) -> Result<()>;

Expand Down
13 changes: 13 additions & 0 deletions crates/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,19 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
Ok(())
}

fn eval_script_with_callback<S: Into<String>>(
&self,
script: S,
callback: impl Fn(String) + Send + 'static,
) -> Result<()> {
self
.last_evaluated_script
.lock()
.unwrap()
.replace(script.into());
Ok(())
}

fn url(&self) -> Result<String> {
Ok(self.url.lock().unwrap().clone())
}
Expand Down
18 changes: 18 additions & 0 deletions crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,24 @@ tauri::Builder::default()
.map_err(Into::into)
}

/// Evaluate JavaScript with callback function on this window.
/// The evaluation result will be serialized into a JSON string and passed to the callback function.
///
/// Exception is ignored because of the limitation on windows. You can catch it yourself and return as string as a workaround.
///
/// - **Android:** Not implemented yet.
pub fn eval_with_callback(
&self,
js: impl Into<String>,
callback: impl Fn(String) + Send + 'static,
) -> crate::Result<()> {
self
.webview
.dispatcher
.eval_script_with_callback(js.into(), callback)
.map_err(Into::into)
}

/// Register a JS event listener and return its identifier.
pub(crate) fn listen_js(
&self,
Expand Down
14 changes: 14 additions & 0 deletions crates/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2324,6 +2324,20 @@ impl<R: Runtime> WebviewWindow<R> {
self.webview.eval(js)
}

/// Evaluate JavaScript with callback function on this window.
/// The evaluation result will be serialized into a JSON string and passed to the callback function.
///
/// Exception is ignored because of the limitation on windows. You can catch it yourself and return as string as a workaround.
///
/// - **Android:** Not implemented yet.
pub fn eval_with_callback(
&self,
js: impl Into<String>,
callback: impl Fn(String) + Send + 'static,
) -> crate::Result<()> {
self.webview.eval_with_callback(js, callback)
}

/// Opens the developer tools window (Web Inspector).
/// The devtools is only enabled on debug builds or with the `devtools` feature flag.
///
Expand Down
Loading