mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-02-06 13:17:02 +00:00
enhance(core): change eval to take Into<String> (#13135)
This commit is contained in:
parent
80dccb6a2e
commit
ebd3dcb92f
5
.changes/eval-take-into-string.md
Normal file
5
.changes/eval-take-into-string.md
Normal 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
|
||||
@ -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
|
||||
))?;
|
||||
|
||||
@ -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())
|
||||
));
|
||||
|
||||
@ -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)?,
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user