Skip to content

Commit

Permalink
fix(core): Return early in core.js if it's inside an iframe (#10300)
Browse files Browse the repository at this point in the history
* fix(core): Return early in core.js if it's inside an iframe

* ipc.js
  • Loading branch information
FabianLars authored Jul 30, 2024
1 parent b59b755 commit eb58ac3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-init-script-iframe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
tauri: "patch:bug"
---

Fixed an issue causing native apis such as `window.alert` to break inside iframes on Windows.
11 changes: 8 additions & 3 deletions core/tauri/scripts/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
return window.crypto.getRandomValues(new Uint32Array(1))[0]
}

const osName = __TEMPLATE_os_name__
const protocolScheme = __TEMPLATE_protocol_scheme__

// Workaround for webview2 injecting scripts into subframes, unlike webkitgtk/wkwebview.
if (osName === 'windows' && window.location !== window.parent.location) {
return
}

if (!window.__TAURI__) {
Object.defineProperty(window, '__TAURI__', {
value: {}
})
}

const osName = __TEMPLATE_os_name__
const protocolScheme = __TEMPLATE_protocol_scheme__

window.__TAURI__.convertFileSrc = function convertFileSrc(filePath, protocol = 'asset') {
const path = encodeURIComponent(filePath)
return osName === 'windows'
Expand Down
8 changes: 7 additions & 1 deletion core/tauri/scripts/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
*/

;
(function() {
(function () {
// Workaround for webview2 injecting scripts into subframes, unlike webkitgtk/wkwebview.
const osName = __TEMPLATE_os_name__
if (osName === 'windows' && window.location !== window.parent.location) {
return
}

/**
* @type {string}
*/
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub type OnPageLoad<R> = dyn Fn(Window<R>, PageLoadPayload) + Send + Sync + 'sta
pub(crate) struct IpcJavascript<'a> {
pub(crate) isolation_origin: &'a str,
pub(crate) invoke_key: &'a str,
pub(crate) os_name: &'a str,
}

#[cfg(feature = "isolation")]
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ impl<R: Runtime> WindowManager<R> {
_ => "".to_string(),
},
invoke_key: self.invoke_key(),
os_name: std::env::consts::OS,
}
.render_default(&Default::default())?;

Expand Down

0 comments on commit eb58ac3

Please sign in to comment.