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>
This commit is contained in:
Orvar Segerström 2022-12-08 15:32:52 +01:00 committed by GitHub
parent 41a471b23a
commit c14b1df372
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 2 deletions

View File

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

View File

@ -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
))?;
}

View File

@ -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)?,