Skip to content

Commit 25f774b

Browse files
committed
remove breaking change, add new api instead.
1 parent 18795ad commit 25f774b

File tree

4 files changed

+102
-12
lines changed

4 files changed

+102
-12
lines changed

.changes/init-script-on-all-frames.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ tauri: minor
33
tauri-runtime: minor
44
---
55

6-
- Breaking API Additions:
7-
- `WebviewBuilder::initialization_script` add `run_only_on_main_frame` parameter
8-
- `WebviewWindowBuilder::initialization_script` add `run_only_on_main_frame` parameter
9-
- add API `WebviewAttributes::initialization_script_on_all_frames`
6+
- add API to run initialisation scripts on all frames
7+
- `WebviewBuilder::initialization_script_on_all_frames`
8+
- `WebviewWindowBuilder::initialization_script_on_all_frames`
9+
- `WebviewAttributes::initialization_script_on_all_frames`

crates/tauri/src/webview/mod.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

crates/tauri/src/webview/webview_window.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,46 @@ impl<R: Runtime, M: Manager<R>> WebviewWindowBuilder<'_, R, M> {
768768
/// 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),
769769
/// it's recommended to check the `window.location` to guard your script from running on unexpected origins.
770770
///
771+
/// This is executed only on the main frame.
772+
/// If you only want to run it in all frames, use [Self::initialization_script_for_all_frames] instead.
773+
///
774+
/// # Examples
775+
///
776+
/// ```rust
777+
/// use tauri::{WebviewWindowBuilder, Runtime};
778+
///
779+
/// const INIT_SCRIPT: &str = r#"
780+
/// if (window.location.origin === 'https://tauri.app') {
781+
/// console.log("hello world from js init script");
782+
///
783+
/// window.__MY_CUSTOM_PROPERTY__ = { foo: 'bar' };
784+
/// }
785+
/// "#;
786+
///
787+
/// fn main() {
788+
/// tauri::Builder::default()
789+
/// .setup(|app| {
790+
/// let webview = tauri::WebviewWindowBuilder::new(app, "label", tauri::WebviewUrl::App("index.html".into()))
791+
/// .initialization_script(INIT_SCRIPT)
792+
/// .build()?;
793+
/// Ok(())
794+
/// });
795+
/// }
796+
/// ```
797+
#[must_use]
798+
pub fn initialization_script(mut self, script: &str) -> Self {
799+
self.webview_builder = self.webview_builder.initialization_script(script);
800+
self
801+
}
802+
803+
/// Adds the provided JavaScript to a list of scripts that should be run after the global object has been created,
804+
/// but before the HTML document has been parsed and before any other script included by the HTML document is run.
805+
///
806+
/// 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),
807+
/// it's recommended to check the `window.location` to guard your script from running on unexpected origins.
808+
///
809+
/// This is executed on all frames, main frame and also sub frames.
810+
/// If you only want to run it in the main frame, use [Self::initialization_script] instead.
771811
/// # Examples
772812
///
773813
/// ```rust
@@ -785,17 +825,17 @@ impl<R: Runtime, M: Manager<R>> WebviewWindowBuilder<'_, R, M> {
785825
/// tauri::Builder::default()
786826
/// .setup(|app| {
787827
/// let webview = tauri::WebviewWindowBuilder::new(app, "label", tauri::WebviewUrl::App("index.html".into()))
788-
/// .initialization_script(INIT_SCRIPT, true)
828+
/// .initialization_script_for_all_frames(INIT_SCRIPT)
789829
/// .build()?;
790830
/// Ok(())
791831
/// });
792832
/// }
793833
/// ```
794834
#[must_use]
795-
pub fn initialization_script(mut self, script: &str, run_only_on_main_frame: bool) -> Self {
835+
pub fn initialization_script_for_all_frames(mut self, script: &str) -> Self {
796836
self.webview_builder = self
797837
.webview_builder
798-
.initialization_script(script, run_only_on_main_frame);
838+
.initialization_script_for_all_frames(script);
799839
self
800840
}
801841

examples/file-associations/src-tauri/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn handle_file_associations(app: AppHandle, files: Vec<PathBuf>) {
4242
.join(",");
4343

4444
tauri::WebviewWindowBuilder::new(&app, "main", Default::default())
45-
.initialization_script(&format!("window.openedFiles = [{files}]"), true)
45+
.initialization_script(&format!("window.openedFiles = [{files}]"))
4646
.build()
4747
.unwrap();
4848
}

0 commit comments

Comments
 (0)