Skip to content

Commit

Permalink
Add subframe option for tauri webviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometrically committed Dec 14, 2024
1 parent ca7f025 commit 9c36dd3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4369,8 +4369,8 @@ fn create_webview<T: UserEvent>(
ipc_handler,
));

for script in webview_attributes.initialization_scripts {
webview_builder = webview_builder.with_initialization_script(&script);
for (script, main_only) in webview_attributes.initialization_scripts {
webview_builder = webview_builder.with_initialization_script_for_main_only(&script, main_only);
}

for (scheme, protocol) in uri_scheme_protocols {
Expand Down
12 changes: 10 additions & 2 deletions crates/tauri-runtime/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<T: UserEvent, R: Runtime<T>> PartialEq for DetachedWebview<T, R> {
pub struct WebviewAttributes {
pub url: WebviewUrl,
pub user_agent: Option<String>,
pub initialization_scripts: Vec<String>,
pub initialization_scripts: Vec<(String, bool)>,
pub data_directory: Option<PathBuf>,
pub drag_drop_handler_enabled: bool,
pub clipboard: bool,
Expand Down Expand Up @@ -292,7 +292,15 @@ impl WebviewAttributes {
/// Sets the init script.
#[must_use]
pub fn initialization_script(mut self, script: &str) -> Self {
self.initialization_scripts.push(script.to_string());
self.initialization_scripts.push((script.to_string(), true));
self
}

/// Sets the init script with the option to inject into sub-frames
pub fn initialization_script_for_main_only(mut self, script: &str, main_only: bool) -> Self {
self
.initialization_scripts
.push((script.to_string(), main_only));
self
}

Expand Down
5 changes: 3 additions & 2 deletions crates/tauri/src/manager/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,14 @@ impl<R: Runtime> WebviewManager<R> {
os_name: &'a str,
}

pending.webview_attributes.initialization_scripts.push(
pending.webview_attributes.initialization_scripts.push((
HotkeyZoom {
os_name: std::env::consts::OS,
}
.render_default(&Default::default())?
.into_string(),
)
true,
))
}

#[cfg(feature = "isolation")]
Expand Down
12 changes: 11 additions & 1 deletion crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,17 @@ fn main() {
self
.webview_attributes
.initialization_scripts
.push(script.to_string());
.push((script.to_string(), true));
self
}

/// Adds an initialization script with the option to inject into sub-frames
#[must_use]
pub fn initialization_script_for_main_only(mut self, script: &str, main_only: bool) -> Self {
self
.webview_attributes
.initialization_scripts
.push((script.to_string(), main_only));
self
}

Expand Down

0 comments on commit 9c36dd3

Please sign in to comment.