@@ -634,9 +634,59 @@ impl<R: Runtime> WebviewBuilder<R> {
634634 /// Adds the provided JavaScript to a list of scripts that should be run after the global object has been created,
635635 /// but before the HTML document has been parsed and before any other script included by the HTML document is run.
636636 ///
637- /// 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) ,
637+ /// Since it runs on all top-level document navigations,
638638 /// it's recommended to check the `window.location` to guard your script from running on unexpected origins.
639639 ///
640+ /// This is executed only on the main frame.
641+ /// If you only want to run it in all frames, use [Self::initialization_script_for_all_frames] instead.
642+ ///
643+ /// # Examples
644+ ///
645+ #[ cfg_attr(
646+ feature = "unstable" ,
647+ doc = r####"
648+ ```rust
649+ use tauri::{WindowBuilder, Runtime};
650+
651+ const INIT_SCRIPT: &str = r#"
652+ if (window.location.origin === 'https://tauri.app') {
653+ console.log("hello world from js init script");
654+
655+ window.__MY_CUSTOM_PROPERTY__ = { foo: 'bar' };
656+ }
657+ "#;
658+
659+ fn main() {
660+ tauri::Builder::default()
661+ .setup(|app| {
662+ let window = tauri::window::WindowBuilder::new(app, "label").build()?;
663+ let webview_builder = tauri::webview::WebviewBuilder::new("label", tauri::WebviewUrl::App("index.html".into()))
664+ .initialization_script(INIT_SCRIPT);
665+ let webview = window.add_child(webview_builder, tauri::LogicalPosition::new(0, 0), window.inner_size().unwrap())?;
666+ Ok(())
667+ });
668+ }
669+ ```
670+ "####
671+ ) ]
672+ #[ must_use]
673+ pub fn initialization_script ( mut self , script : & str ) -> Self {
674+ self
675+ . webview_attributes
676+ . initialization_scripts
677+ . push ( ( script. to_string ( ) , true ) ) ;
678+ self
679+ }
680+
681+ /// Adds the provided JavaScript to a list of scripts that should be run after the global object has been created,
682+ /// but before the HTML document has been parsed and before any other script included by the HTML document is run.
683+ ///
684+ /// Since it runs on all top-level document navigations and also child frame page navigations,
685+ /// it's recommended to check the `window.location` to guard your script from running on unexpected origins.
686+ ///
687+ /// This is executed on all frames, main frame and also sub frames.
688+ /// If you only want to run it in the main frame, use [Self::initialization_script] instead.
689+ ///
640690 /// # Examples
641691 ///
642692 #[ cfg_attr(
@@ -658,7 +708,7 @@ fn main() {
658708 .setup(|app| {
659709 let window = tauri::window::WindowBuilder::new(app, "label").build()?;
660710 let webview_builder = tauri::webview::WebviewBuilder::new("label", tauri::WebviewUrl::App("index.html".into()))
661- .initialization_script (INIT_SCRIPT, true );
711+ .initialization_script_for_all_frames (INIT_SCRIPT);
662712 let webview = window.add_child(webview_builder, tauri::LogicalPosition::new(0, 0), window.inner_size().unwrap())?;
663713 Ok(())
664714 });
@@ -667,11 +717,11 @@ fn main() {
667717 "####
668718 ) ]
669719 #[ must_use]
670- pub fn initialization_script ( mut self , script : & str , run_only_on_main_frame : bool ) -> Self {
720+ pub fn initialization_script_for_all_frames ( mut self , script : & str ) -> Self {
671721 self
672722 . webview_attributes
673723 . initialization_scripts
674- . push ( ( script. to_string ( ) , run_only_on_main_frame ) ) ;
724+ . push ( ( script. to_string ( ) , false ) ) ;
675725 self
676726 }
677727
0 commit comments