enhance(core): change eval to take Into<String> (#13135)

This commit is contained in:
Tony 2025-04-04 16:06:22 +08:00 committed by GitHub
parent 80dccb6a2e
commit ebd3dcb92f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 19 additions and 10 deletions

View File

@ -0,0 +1,5 @@
---
tauri: patch:enhance
---
`Webview::eval` and `WebviewWindow::eval` now takes `impl Into<String>` instead of `&str` to allow passing the scripts more flexible and efficiently

View File

@ -128,7 +128,7 @@ impl JavaScriptChannelId {
.unwrap()
.insert(data_id, body);
webview.eval(&format!(
webview.eval(format!(
"window.__TAURI_INTERNALS__.invoke('{FETCH_CHANNEL_DATA_COMMAND}', null, {{ headers: {{ '{CHANNEL_ID_HEADER_NAME}': '{data_id}' }} }}).then((response) => window['_' + {}]({{ message: response, id: {i} }})).catch(console.error)",
callback_id.0
))?;
@ -192,7 +192,7 @@ impl<TSend> Channel<TSend> {
.unwrap()
.insert(data_id, body);
webview.eval(&format!(
webview.eval(format!(
"window.__TAURI_INTERNALS__.invoke('{FETCH_CHANNEL_DATA_COMMAND}', null, {{ headers: {{ '{CHANNEL_ID_HEADER_NAME}': '{data_id}' }} }}).then((response) => window['_' + {}](response)).catch(console.error)",
callback.0
))?;

View File

@ -331,7 +331,7 @@ fn handle_ipc_message<R: Runtime>(request: Request<String>, manager: &AppManager
.expect("unable to serialize response error string to json"),
};
let _ = webview.eval(&eval_js);
let _ = webview.eval(eval_js);
}
let can_use_channel_for_response = cmd
@ -423,7 +423,7 @@ fn handle_ipc_message<R: Runtime>(request: Request<String>, manager: &AppManager
#[cfg(feature = "tracing")]
tracing::trace!("ipc.request.error {}", e);
let _ = webview.eval(&format!(
let _ = webview.eval(format!(
r#"console.error({})"#,
serde_json::Value::String(e.to_string())
));

View File

@ -1578,8 +1578,12 @@ fn main() {
}
/// Evaluates JavaScript on this window.
pub fn eval(&self, js: &str) -> crate::Result<()> {
self.webview.dispatcher.eval_script(js).map_err(Into::into)
pub fn eval(&self, js: impl Into<String>) -> crate::Result<()> {
self
.webview
.dispatcher
.eval_script(js.into())
.map_err(Into::into)
}
/// Register a JS event listener and return its identifier.
@ -1593,7 +1597,7 @@ fn main() {
let id = listeners.next_event_id();
self.eval(&crate::event::listen_js_script(
self.eval(crate::event::listen_js_script(
listeners.listeners_object_name(),
&serde_json::to_string(&target)?,
event,
@ -1610,7 +1614,7 @@ fn main() {
pub(crate) fn unlisten_js(&self, event: EventName<&str>, id: EventId) -> crate::Result<()> {
let listeners = self.manager().listeners();
self.eval(&crate::event::unlisten_js_script(
self.eval(crate::event::unlisten_js_script(
listeners.listeners_object_name(),
event,
id,
@ -1622,7 +1626,7 @@ fn main() {
}
pub(crate) fn emit_js(&self, emit_args: &EmitArgs, ids: &[u32]) -> crate::Result<()> {
self.eval(&crate::event::emit_js_script(
self.eval(crate::event::emit_js_script(
self.manager().listeners().function_name(),
emit_args,
&serde_json::to_string(ids)?,

View File

@ -1969,7 +1969,7 @@ impl<R: Runtime> WebviewWindow<R> {
}
/// Evaluates JavaScript on this window.
pub fn eval(&self, js: &str) -> crate::Result<()> {
pub fn eval(&self, js: impl Into<String>) -> crate::Result<()> {
self.webview.eval(js)
}