From 8ee0495742f158f59947a1aa102802ef528c8672 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Tue, 25 Mar 2025 18:51:43 +0100 Subject: [PATCH 1/7] api!: expose api to run initialisation scripts on all frames. --- .changes/init-script-on-all-frames.md | 9 ++++ crates/tauri-runtime-wry/src/lib.rs | 2 +- crates/tauri-runtime/src/webview.rs | 52 +++++++++++++++++-- crates/tauri/src/manager/webview.rs | 14 +++-- crates/tauri/src/webview/mod.rs | 8 +-- crates/tauri/src/webview/webview_window.rs | 10 ++-- .../file-associations/src-tauri/src/main.rs | 2 +- 7 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 .changes/init-script-on-all-frames.md diff --git a/.changes/init-script-on-all-frames.md b/.changes/init-script-on-all-frames.md new file mode 100644 index 000000000000..8dc4ea0894f1 --- /dev/null +++ b/.changes/init-script-on-all-frames.md @@ -0,0 +1,9 @@ +--- +tauri: minor +tauri-runtime: minor +--- + +- Breaking API Additions: + - `WebviewBuilder::initialization_script` add `run_only_on_main_frame` parameter + - `WebviewWindowBuilder::initialization_script` add `run_only_on_main_frame` parameter +- add API `WebviewAttributes::initialization_script_on_all_frames` diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 570e75505435..5c816d75607e 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -4557,7 +4557,7 @@ fn create_webview( )); for script in webview_attributes.initialization_scripts { - webview_builder = webview_builder.with_initialization_script(&script); + webview_builder = webview_builder.with_initialization_script_for_main_only(&script.0, script.1); } for (scheme, protocol) in uri_scheme_protocols { diff --git a/crates/tauri-runtime/src/webview.rs b/crates/tauri-runtime/src/webview.rs index 895c10141f28..d137132b295f 100644 --- a/crates/tauri-runtime/src/webview.rs +++ b/crates/tauri-runtime/src/webview.rs @@ -197,7 +197,18 @@ impl> PartialEq for DetachedWebview { pub struct WebviewAttributes { pub url: WebviewUrl, pub user_agent: Option, - pub initialization_scripts: Vec, + /// A list of initialization javascript scripts to run when loading new pages. + /// When webview load a new page, this initialization code will be executed. + /// It is guaranteed that code is executed before `window.onload`. + /// + /// Second parameter represents if script should be added to main frame only or sub frames also. + /// `true` for main frame only, `false` for all frames including sub frames. + /// + /// ## Platform-specific + /// + /// - **Android on Wry:** The Android WebView does not provide an API for initialization scripts, + /// so we prepend them to each HTML head. They are only implemented on custom protocol URLs. + pub initialization_scripts: Vec<(String, bool)>, pub data_directory: Option, pub drag_drop_handler_enabled: bool, pub clipboard: bool, @@ -307,10 +318,45 @@ impl WebviewAttributes { self } - /// Sets the init script. + /// Adds an init script for the main frame. + /// + /// When webview load a new page, this initialization code will be executed. + /// It is guaranteed that code is executed before `window.onload`. + /// + /// This is executed only on the main frame. + /// If you only want to run it in all frames, use [Self::initialization_script_on_all_frames] instead. + /// + /// + /// ## Platform-specific + /// + /// - **Android on Wry:** The Android WebView does not provide an API for initialization scripts, + /// so we prepend them to each HTML head. They are only implemented on custom protocol URLs. #[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 + } + + /// Adds an init script for all frames. + /// + /// When webview load a new page, this initialization code will be executed. + /// It is guaranteed that code is executed before `window.onload`. + /// + /// This is executed on all frames, main frame and also sub frames. + /// If you only want to run it in the main frame, use [Self::initialization_script] instead. + /// + /// ## Platform-specific + /// + /// - **Android on Wry:** The Android WebView does not provide an API for initialization scripts, + /// so we prepend them to each HTML head. They are only implemented on custom protocol URLs. + /// + /// - On **macOS** and **iOS** you can use this to overwrite browser apis to make them inacessible even in iframes. + /// You can disable WebRTC this way, for example. + #[must_use] + pub fn initialization_script_on_all_frames(mut self, script: &str) -> Self { + self + .initialization_scripts + .push((script.to_string(), false)); self } diff --git a/crates/tauri/src/manager/webview.rs b/crates/tauri/src/manager/webview.rs index 042ba47a827a..d092c4c5c29e 100644 --- a/crates/tauri/src/manager/webview.rs +++ b/crates/tauri/src/manager/webview.rs @@ -211,9 +211,12 @@ impl WebviewManager { } } - webview_attributes - .initialization_scripts - .splice(0..0, all_initialization_scripts); + webview_attributes.initialization_scripts.splice( + 0..0, + all_initialization_scripts + .into_iter() + .map(|script| (script, true /* only run on main frame */)), + ); pending.webview_attributes = webview_attributes; @@ -527,13 +530,14 @@ impl WebviewManager { 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")] diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index a7161f8acd95..f64855d4b829 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -634,7 +634,7 @@ impl WebviewBuilder { /// Adds the provided JavaScript to a list of scripts that should be run after the global object has been created, /// but before the HTML document has been parsed and before any other script included by the HTML document is run. /// - /// Since it runs on all top-level document and child frame page navigations, + /// Since it runs on all top-level document navigastions (and also child frame page navigations, if you set `run_only_on_main_frame` to false), /// it's recommended to check the `window.location` to guard your script from running on unexpected origins. /// /// # Examples @@ -658,7 +658,7 @@ fn main() { .setup(|app| { let window = tauri::window::WindowBuilder::new(app, "label").build()?; let webview_builder = tauri::webview::WebviewBuilder::new("label", tauri::WebviewUrl::App("index.html".into())) - .initialization_script(INIT_SCRIPT); + .initialization_script(INIT_SCRIPT, true); let webview = window.add_child(webview_builder, tauri::LogicalPosition::new(0, 0), window.inner_size().unwrap())?; Ok(()) }); @@ -667,11 +667,11 @@ fn main() { "#### )] #[must_use] - pub fn initialization_script(mut self, script: &str) -> Self { + pub fn initialization_script(mut self, script: &str, run_only_on_main_frame: bool) -> Self { self .webview_attributes .initialization_scripts - .push(script.to_string()); + .push((script.to_string(), run_only_on_main_frame)); self } diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index dc798efd21fe..470f91395a8f 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -765,7 +765,7 @@ impl> WebviewWindowBuilder<'_, R, M> { /// Adds the provided JavaScript to a list of scripts that should be run after the global object has been created, /// but before the HTML document has been parsed and before any other script included by the HTML document is run. /// - /// Since it runs on all top-level document and child frame page navigations, + /// Since it runs on all top-level document navigastions (and also child frame page navigations, if you set `run_only_on_main_frame` to false), /// it's recommended to check the `window.location` to guard your script from running on unexpected origins. /// /// # Examples @@ -785,15 +785,17 @@ impl> WebviewWindowBuilder<'_, R, M> { /// tauri::Builder::default() /// .setup(|app| { /// let webview = tauri::WebviewWindowBuilder::new(app, "label", tauri::WebviewUrl::App("index.html".into())) - /// .initialization_script(INIT_SCRIPT) + /// .initialization_script(INIT_SCRIPT, true) /// .build()?; /// Ok(()) /// }); /// } /// ``` #[must_use] - pub fn initialization_script(mut self, script: &str) -> Self { - self.webview_builder = self.webview_builder.initialization_script(script); + pub fn initialization_script(mut self, script: &str, run_only_on_main_frame: bool) -> Self { + self.webview_builder = self + .webview_builder + .initialization_script(script, run_only_on_main_frame); self } diff --git a/examples/file-associations/src-tauri/src/main.rs b/examples/file-associations/src-tauri/src/main.rs index ea2ec8f3b4d9..45a436510d4d 100644 --- a/examples/file-associations/src-tauri/src/main.rs +++ b/examples/file-associations/src-tauri/src/main.rs @@ -42,7 +42,7 @@ fn handle_file_associations(app: AppHandle, files: Vec) { .join(","); tauri::WebviewWindowBuilder::new(&app, "main", Default::default()) - .initialization_script(&format!("window.openedFiles = [{files}]")) + .initialization_script(&format!("window.openedFiles = [{files}]"), true) .build() .unwrap(); } From e2989d15fe7584398738c44d0751e868743b4243 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Wed, 26 Mar 2025 21:09:03 +0100 Subject: [PATCH 2/7] remove breaking change, add new api instead. --- .changes/init-script-on-all-frames.md | 8 +-- crates/tauri/src/webview/mod.rs | 58 +++++++++++++++++-- crates/tauri/src/webview/webview_window.rs | 46 ++++++++++++++- .../file-associations/src-tauri/src/main.rs | 2 +- 4 files changed, 102 insertions(+), 12 deletions(-) diff --git a/.changes/init-script-on-all-frames.md b/.changes/init-script-on-all-frames.md index 8dc4ea0894f1..a1f5af4df0ac 100644 --- a/.changes/init-script-on-all-frames.md +++ b/.changes/init-script-on-all-frames.md @@ -3,7 +3,7 @@ tauri: minor tauri-runtime: minor --- -- Breaking API Additions: - - `WebviewBuilder::initialization_script` add `run_only_on_main_frame` parameter - - `WebviewWindowBuilder::initialization_script` add `run_only_on_main_frame` parameter -- add API `WebviewAttributes::initialization_script_on_all_frames` +- add API to run initialisation scripts on all frames + - `WebviewBuilder::initialization_script_on_all_frames` + - `WebviewWindowBuilder::initialization_script_on_all_frames` + - `WebviewAttributes::initialization_script_on_all_frames` diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index f64855d4b829..2060223b60b9 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -634,9 +634,59 @@ impl WebviewBuilder { /// Adds the provided JavaScript to a list of scripts that should be run after the global object has been created, /// but before the HTML document has been parsed and before any other script included by the HTML document is run. /// - /// Since it runs on all top-level document navigastions (and also child frame page navigations, if you set `run_only_on_main_frame` to false), + /// Since it runs on all top-level document navigations, /// it's recommended to check the `window.location` to guard your script from running on unexpected origins. /// + /// This is executed only on the main frame. + /// If you only want to run it in all frames, use [Self::initialization_script_for_all_frames] instead. + /// + /// # Examples + /// + #[cfg_attr( + feature = "unstable", + doc = r####" +```rust +use tauri::{WindowBuilder, Runtime}; + +const INIT_SCRIPT: &str = r#" + if (window.location.origin === 'https://tauri.app') { + console.log("hello world from js init script"); + + window.__MY_CUSTOM_PROPERTY__ = { foo: 'bar' }; + } +"#; + +fn main() { + tauri::Builder::default() + .setup(|app| { + let window = tauri::window::WindowBuilder::new(app, "label").build()?; + let webview_builder = tauri::webview::WebviewBuilder::new("label", tauri::WebviewUrl::App("index.html".into())) + .initialization_script(INIT_SCRIPT); + let webview = window.add_child(webview_builder, tauri::LogicalPosition::new(0, 0), window.inner_size().unwrap())?; + Ok(()) + }); +} +``` + "#### + )] + #[must_use] + pub fn initialization_script(mut self, script: &str) -> Self { + self + .webview_attributes + .initialization_scripts + .push((script.to_string(), true)); + self + } + + /// Adds the provided JavaScript to a list of scripts that should be run after the global object has been created, + /// but before the HTML document has been parsed and before any other script included by the HTML document is run. + /// + /// Since it runs on all top-level document navigations and also child frame page navigations, + /// it's recommended to check the `window.location` to guard your script from running on unexpected origins. + /// + /// This is executed on all frames, main frame and also sub frames. + /// If you only want to run it in the main frame, use [Self::initialization_script] instead. + /// /// # Examples /// #[cfg_attr( @@ -658,7 +708,7 @@ fn main() { .setup(|app| { let window = tauri::window::WindowBuilder::new(app, "label").build()?; let webview_builder = tauri::webview::WebviewBuilder::new("label", tauri::WebviewUrl::App("index.html".into())) - .initialization_script(INIT_SCRIPT, true); + .initialization_script_for_all_frames(INIT_SCRIPT); let webview = window.add_child(webview_builder, tauri::LogicalPosition::new(0, 0), window.inner_size().unwrap())?; Ok(()) }); @@ -667,11 +717,11 @@ fn main() { "#### )] #[must_use] - pub fn initialization_script(mut self, script: &str, run_only_on_main_frame: bool) -> Self { + pub fn initialization_script_for_all_frames(mut self, script: &str) -> Self { self .webview_attributes .initialization_scripts - .push((script.to_string(), run_only_on_main_frame)); + .push((script.to_string(), false)); self } diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index 470f91395a8f..ba1a0700e9d0 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -768,6 +768,46 @@ impl> WebviewWindowBuilder<'_, R, M> { /// Since it runs on all top-level document navigastions (and also child frame page navigations, if you set `run_only_on_main_frame` to false), /// it's recommended to check the `window.location` to guard your script from running on unexpected origins. /// + /// This is executed only on the main frame. + /// If you only want to run it in all frames, use [Self::initialization_script_for_all_frames] instead. + /// + /// # Examples + /// + /// ```rust + /// use tauri::{WebviewWindowBuilder, Runtime}; + /// + /// const INIT_SCRIPT: &str = r#" + /// if (window.location.origin === 'https://tauri.app') { + /// console.log("hello world from js init script"); + /// + /// window.__MY_CUSTOM_PROPERTY__ = { foo: 'bar' }; + /// } + /// "#; + /// + /// fn main() { + /// tauri::Builder::default() + /// .setup(|app| { + /// let webview = tauri::WebviewWindowBuilder::new(app, "label", tauri::WebviewUrl::App("index.html".into())) + /// .initialization_script(INIT_SCRIPT) + /// .build()?; + /// Ok(()) + /// }); + /// } + /// ``` + #[must_use] + pub fn initialization_script(mut self, script: &str) -> Self { + self.webview_builder = self.webview_builder.initialization_script(script); + self + } + + /// Adds the provided JavaScript to a list of scripts that should be run after the global object has been created, + /// but before the HTML document has been parsed and before any other script included by the HTML document is run. + /// + /// Since it runs on all top-level document navigastions (and also child frame page navigations, if you set `run_only_on_main_frame` to false), + /// it's recommended to check the `window.location` to guard your script from running on unexpected origins. + /// + /// This is executed on all frames, main frame and also sub frames. + /// If you only want to run it in the main frame, use [Self::initialization_script] instead. /// # Examples /// /// ```rust @@ -785,17 +825,17 @@ impl> WebviewWindowBuilder<'_, R, M> { /// tauri::Builder::default() /// .setup(|app| { /// let webview = tauri::WebviewWindowBuilder::new(app, "label", tauri::WebviewUrl::App("index.html".into())) - /// .initialization_script(INIT_SCRIPT, true) + /// .initialization_script_for_all_frames(INIT_SCRIPT) /// .build()?; /// Ok(()) /// }); /// } /// ``` #[must_use] - pub fn initialization_script(mut self, script: &str, run_only_on_main_frame: bool) -> Self { + pub fn initialization_script_for_all_frames(mut self, script: &str) -> Self { self.webview_builder = self .webview_builder - .initialization_script(script, run_only_on_main_frame); + .initialization_script_for_all_frames(script); self } diff --git a/examples/file-associations/src-tauri/src/main.rs b/examples/file-associations/src-tauri/src/main.rs index 45a436510d4d..ea2ec8f3b4d9 100644 --- a/examples/file-associations/src-tauri/src/main.rs +++ b/examples/file-associations/src-tauri/src/main.rs @@ -42,7 +42,7 @@ fn handle_file_associations(app: AppHandle, files: Vec) { .join(","); tauri::WebviewWindowBuilder::new(&app, "main", Default::default()) - .initialization_script(&format!("window.openedFiles = [{files}]"), true) + .initialization_script(&format!("window.openedFiles = [{files}]")) .build() .unwrap(); } From e8fe8e28239340977cce9d897a85c1c31f7601cd Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Thu, 27 Mar 2025 16:18:40 +0100 Subject: [PATCH 3/7] Update .changes/init-script-on-all-frames.md Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> --- .changes/init-script-on-all-frames.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changes/init-script-on-all-frames.md b/.changes/init-script-on-all-frames.md index a1f5af4df0ac..e0a37aab089b 100644 --- a/.changes/init-script-on-all-frames.md +++ b/.changes/init-script-on-all-frames.md @@ -1,6 +1,6 @@ --- -tauri: minor -tauri-runtime: minor +tauri: minor:feat +tauri-runtime: minor:feat --- - add API to run initialisation scripts on all frames From f405be881e9aa73e75090c55ab76299867aa0e6a Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Thu, 27 Mar 2025 22:53:00 +0100 Subject: [PATCH 4/7] use struct `InitializationScript` instead of tuple --- crates/tauri-runtime-wry/src/lib.rs | 3 ++- crates/tauri-runtime/src/webview.rs | 32 ++++++++++++++++++++++++----- crates/tauri/src/manager/webview.rs | 26 ++++++++++++++--------- crates/tauri/src/webview/mod.rs | 12 ++++++++--- 4 files changed, 54 insertions(+), 19 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 5c816d75607e..b0d4ebd473b0 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -4557,7 +4557,8 @@ fn create_webview( )); for script in webview_attributes.initialization_scripts { - webview_builder = webview_builder.with_initialization_script_for_main_only(&script.0, script.1); + webview_builder = webview_builder + .with_initialization_script_for_main_only(&script.script, script.for_main_frame_only); } for (scheme, protocol) in uri_scheme_protocols { diff --git a/crates/tauri-runtime/src/webview.rs b/crates/tauri-runtime/src/webview.rs index d137132b295f..11c98c2c3e7a 100644 --- a/crates/tauri-runtime/src/webview.rs +++ b/crates/tauri-runtime/src/webview.rs @@ -208,7 +208,7 @@ pub struct WebviewAttributes { /// /// - **Android on Wry:** The Android WebView does not provide an API for initialization scripts, /// so we prepend them to each HTML head. They are only implemented on custom protocol URLs. - pub initialization_scripts: Vec<(String, bool)>, + pub initialization_scripts: Vec, pub data_directory: Option, pub drag_drop_handler_enabled: bool, pub clipboard: bool, @@ -333,7 +333,10 @@ impl WebviewAttributes { /// so we prepend them to each HTML head. They are only implemented on custom protocol URLs. #[must_use] pub fn initialization_script(mut self, script: &str) -> Self { - self.initialization_scripts.push((script.to_string(), true)); + self.initialization_scripts.push(InitializationScript { + script: script.to_string(), + for_main_frame_only: true, + }); self } @@ -354,9 +357,10 @@ impl WebviewAttributes { /// You can disable WebRTC this way, for example. #[must_use] pub fn initialization_script_on_all_frames(mut self, script: &str) -> Self { - self - .initialization_scripts - .push((script.to_string(), false)); + self.initialization_scripts.push(InitializationScript { + script: script.to_string(), + for_main_frame_only: false, + }); self } @@ -545,3 +549,21 @@ impl WebviewAttributes { /// IPC handler. pub type WebviewIpcHandler = Box, Request) + Send>; + +/// An initialization script +#[derive(Debug, Clone)] +pub struct InitializationScript { + /// The script to run + pub script: String, + /// Whether the script should be injected to main frame only + pub for_main_frame_only: bool, +} + +impl InitializationScript { + pub fn new(script: &str, for_main_frame_only: bool) -> Self { + Self { + script: script.to_owned(), + for_main_frame_only, + } + } +} diff --git a/crates/tauri/src/manager/webview.rs b/crates/tauri/src/manager/webview.rs index d092c4c5c29e..f07bbd338ff3 100644 --- a/crates/tauri/src/manager/webview.rs +++ b/crates/tauri/src/manager/webview.rs @@ -13,7 +13,7 @@ use std::{ use serde::Serialize; use serialize_to_javascript::{default_template, DefaultTemplate, Template}; use tauri_runtime::{ - webview::{DetachedWebview, PendingWebview}, + webview::{DetachedWebview, InitializationScript, PendingWebview}, window::DragDropEvent, }; use tauri_utils::config::WebviewUrl; @@ -215,7 +215,10 @@ impl WebviewManager { 0..0, all_initialization_scripts .into_iter() - .map(|script| (script, true /* only run on main frame */)), + .map(|script| InitializationScript { + script, + for_main_frame_only: true, + }), ); pending.webview_attributes = webview_attributes; @@ -530,14 +533,17 @@ impl WebviewManager { os_name: &'a str, } - pending.webview_attributes.initialization_scripts.push(( - HotkeyZoom { - os_name: std::env::consts::OS, - } - .render_default(&Default::default())? - .into_string(), - true, - )) + pending + .webview_attributes + .initialization_scripts + .push(InitializationScript { + script: HotkeyZoom { + os_name: std::env::consts::OS, + } + .render_default(&Default::default())? + .into_string(), + for_main_frame_only: true, + }) } #[cfg(feature = "isolation")] diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index 2060223b60b9..7b8aaecf0b79 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -20,7 +20,7 @@ use tauri_runtime::{ WindowDispatch, }; use tauri_runtime::{ - webview::{DetachedWebview, PendingWebview, WebviewAttributes}, + webview::{DetachedWebview, InitializationScript, PendingWebview, WebviewAttributes}, WebviewDispatch, }; pub use tauri_utils::config::Color; @@ -674,7 +674,10 @@ fn main() { self .webview_attributes .initialization_scripts - .push((script.to_string(), true)); + .push(InitializationScript { + script: script.to_string(), + for_main_frame_only: true, + }); self } @@ -721,7 +724,10 @@ fn main() { self .webview_attributes .initialization_scripts - .push((script.to_string(), false)); + .push(InitializationScript { + script: script.to_string(), + for_main_frame_only: false, + }); self } From d1bf73fe2a4b8c89fabc9a525fae733e36f34925 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Fri, 28 Mar 2025 17:32:09 +0100 Subject: [PATCH 5/7] Update crates/tauri-runtime/src/webview.rs Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> --- crates/tauri-runtime/src/webview.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/tauri-runtime/src/webview.rs b/crates/tauri-runtime/src/webview.rs index 11c98c2c3e7a..9d5d7d08f494 100644 --- a/crates/tauri-runtime/src/webview.rs +++ b/crates/tauri-runtime/src/webview.rs @@ -352,9 +352,6 @@ impl WebviewAttributes { /// /// - **Android on Wry:** The Android WebView does not provide an API for initialization scripts, /// so we prepend them to each HTML head. They are only implemented on custom protocol URLs. - /// - /// - On **macOS** and **iOS** you can use this to overwrite browser apis to make them inacessible even in iframes. - /// You can disable WebRTC this way, for example. #[must_use] pub fn initialization_script_on_all_frames(mut self, script: &str) -> Self { self.initialization_scripts.push(InitializationScript { From 049fec1fa011122943bc73e709e797dda46f5851 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Sat, 29 Mar 2025 10:34:58 +0800 Subject: [PATCH 6/7] Apply suggestions from code review --- .changes/init-script-on-all-frames.md | 2 +- crates/tauri-runtime/src/webview.rs | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.changes/init-script-on-all-frames.md b/.changes/init-script-on-all-frames.md index e0a37aab089b..68253a799a12 100644 --- a/.changes/init-script-on-all-frames.md +++ b/.changes/init-script-on-all-frames.md @@ -3,7 +3,7 @@ tauri: minor:feat tauri-runtime: minor:feat --- -- add API to run initialisation scripts on all frames +- add API to run initialization scripts on all frames - `WebviewBuilder::initialization_script_on_all_frames` - `WebviewWindowBuilder::initialization_script_on_all_frames` - `WebviewAttributes::initialization_script_on_all_frames` diff --git a/crates/tauri-runtime/src/webview.rs b/crates/tauri-runtime/src/webview.rs index 9d5d7d08f494..a6d7bc24d90e 100644 --- a/crates/tauri-runtime/src/webview.rs +++ b/crates/tauri-runtime/src/webview.rs @@ -201,9 +201,6 @@ pub struct WebviewAttributes { /// When webview load a new page, this initialization code will be executed. /// It is guaranteed that code is executed before `window.onload`. /// - /// Second parameter represents if script should be added to main frame only or sub frames also. - /// `true` for main frame only, `false` for all frames including sub frames. - /// /// ## Platform-specific /// /// - **Android on Wry:** The Android WebView does not provide an API for initialization scripts, From a8469626cea508b88bf21dd1e892ba7b38308da4 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Wed, 2 Apr 2025 08:24:05 +0800 Subject: [PATCH 7/7] Update crates/tauri/src/webview/webview_window.rs --- crates/tauri/src/webview/webview_window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index ba1a0700e9d0..993b54536245 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -765,7 +765,7 @@ impl> WebviewWindowBuilder<'_, R, M> { /// Adds the provided JavaScript to a list of scripts that should be run after the global object has been created, /// but before the HTML document has been parsed and before any other script included by the HTML document is run. /// - /// Since it runs on all top-level document navigastions (and also child frame page navigations, if you set `run_only_on_main_frame` to false), + /// Since it runs on all top-level document navigations (and also child frame page navigations, if you set `run_only_on_main_frame` to false), /// it's recommended to check the `window.location` to guard your script from running on unexpected origins. /// /// This is executed only on the main frame.