Selaa lähdekoodia

fix(core): Invoke event listener in windows safely to avoid causing uncaught errors in windows that have loaded external urls (#5563)

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Orvar Segerström 2 vuotta sitten
vanhempi
sitoutus
c14b1df372

+ 5 - 0
.changes/avoid-errors-in-external-urled-windows.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Invoke event listener in windows safely to avoid causing uncaught errors in windows that have loaded external urls

+ 1 - 1
core/tauri/src/manager.rs

@@ -1408,7 +1408,7 @@ fn on_window_event<R: Runtime>(
       let windows = windows_map.values();
       for window in windows {
         window.eval(&format!(
-          r#"window.__TAURI_METADATA__.__windows = window.__TAURI_METADATA__.__windows.filter(w => w.label !== "{}");"#,
+          r#"(function () {{ const metadata = window.__TAURI_METADATA__; if (metadata != null) {{ metadata.__windows = window.__TAURI_METADATA__.__windows.filter(w => w.label !== "{}"); }} }})()"#,
           label
         ))?;
       }

+ 1 - 1
core/tauri/src/window.rs

@@ -1456,7 +1456,7 @@ impl<R: Runtime> Window<R> {
     payload: S,
   ) -> crate::Result<()> {
     self.eval(&format!(
-      "window['{}']({{event: {}, windowLabel: {}, payload: {}}})",
+      "(function () {{ const fn = window['{}']; fn && fn({{event: {}, windowLabel: {}, payload: {}}}) }})()",
       self.manager.event_emit_function_name(),
       serde_json::to_string(event)?,
       serde_json::to_string(&source_window_label)?,