Skip to content

Commit

Permalink
fix: run tauri's internal init scripts before user's scripts (tauri-a…
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyg authored Jan 17, 2025
1 parent b643dcc commit d2c8f0e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changes/initialization_scripts_order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri': 'patch:bug'
---

Ensure that tauri's builtin initialization scripts and plugin initialization scripts are executed before any user-added initialization scripts in a webview.
55 changes: 32 additions & 23 deletions crates/tauri/src/manager/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ impl<R: Runtime> WebviewManager<R> {
}
.render_default(&Default::default())?;

webview_attributes = webview_attributes
.initialization_script(
r"
let mut all_initialization_scripts: Vec<String> = vec![];
all_initialization_scripts.push(
r"
Object.defineProperty(window, 'isTauri', {
value: true,
});
Expand All @@ -164,51 +164,60 @@ impl<R: Runtime> WebviewManager<R> {
}
})
}
",
)
.initialization_script(&self.invoke_initialization_script)
.initialization_script(&format!(
r#"
"
.to_string(),
);
all_initialization_scripts.push(self.invoke_initialization_script.to_string());
all_initialization_scripts.push(format!(
r#"
Object.defineProperty(window.__TAURI_INTERNALS__, 'metadata', {{
value: {{
currentWindow: {{ label: {current_window_label} }},
currentWebview: {{ label: {current_webview_label} }}
}}
}})
"#,
current_window_label = serde_json::to_string(window_label)?,
current_webview_label = serde_json::to_string(&label)?,
))
.initialization_script(&self.initialization_script(
app_manager,
&ipc_init.into_string(),
&pattern_init.into_string(),
is_init_global,
use_https_scheme,
)?);
current_window_label = serde_json::to_string(window_label)?,
current_webview_label = serde_json::to_string(&label)?,
));
all_initialization_scripts.push(
self
.initialization_script(
app_manager,
&ipc_init.into_string(),
&pattern_init.into_string(),
is_init_global,
use_https_scheme,
)?
.to_string(),
);

for plugin_init_script in plugin_init_scripts {
webview_attributes = webview_attributes.initialization_script(&plugin_init_script);
all_initialization_scripts.push(plugin_init_script.to_string());
}

#[cfg(feature = "isolation")]
if let crate::Pattern::Isolation { schema, .. } = &*app_manager.pattern {
webview_attributes = webview_attributes.initialization_script(
&IsolationJavascript {
all_initialization_scripts.push(
IsolationJavascript {
isolation_src: &crate::pattern::format_real_schema(schema, use_https_scheme),
style: tauri_utils::pattern::isolation::IFRAME_STYLE,
}
.render_default(&Default::default())?
.into_string(),
.to_string(),
);
}

if let Some(plugin_global_api_scripts) = &*app_manager.plugin_global_api_scripts {
for script in plugin_global_api_scripts.iter() {
webview_attributes = webview_attributes.initialization_script(script);
all_initialization_scripts.push(script.to_string());
}
}

webview_attributes
.initialization_scripts
.splice(0..0, all_initialization_scripts);

pending.webview_attributes = webview_attributes;

let mut registered_scheme_protocols = Vec::new();
Expand Down

0 comments on commit d2c8f0e

Please sign in to comment.