Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for injecting script into subframes (from wry) #11971

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading