From e6012b88afb8706ce2bae9dfadc60435b418e45b Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Tue, 2 Aug 2022 18:53:55 -0300 Subject: [PATCH] docs(core): enhance `initialization_script` documentation, ref #4831 (#4832) --- core/tauri/src/plugin.rs | 24 +++++++++++++++++------- core/tauri/src/window.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/core/tauri/src/plugin.rs b/core/tauri/src/plugin.rs index 3d965d0c7..f74c0f088 100644 --- a/core/tauri/src/plugin.rs +++ b/core/tauri/src/plugin.rs @@ -28,11 +28,14 @@ pub trait Plugin: Send { Ok(()) } - /// The JS script to evaluate on webview initialization. + /// Add 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, + /// it's recommended to check the `window.location` to guard your script from running on unexpected origins. + /// /// The script is wrapped into its own context with `(function () { /* your script here */ })();`, /// so global variables must be assigned to `window` instead of implicity declared. - /// - /// It's guaranteed that this script is executed before the page is loaded. fn initialization_script(&self) -> Option { None } @@ -193,11 +196,16 @@ impl Builder { self } - /// The JS script to evaluate on webview initialization. + /// Sets the provided JavaScript to 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, + /// it's recommended to check the `window.location` to guard your script from running on unexpected origins. + /// /// The script is wrapped into its own context with `(function () { /* your script here */ })();`, /// so global variables must be assigned to `window` instead of implicity declared. /// - /// It's guaranteed that this script is executed before the page is loaded. + /// Note that calling this function multiple times overrides previous values. /// /// # Examples /// @@ -205,9 +213,11 @@ impl Builder { /// use tauri::{plugin::{Builder, TauriPlugin}, Runtime}; /// /// const INIT_SCRIPT: &str = r#" - /// console.log("hello world from js init script"); + /// if (window.location.origin === 'https://tauri.app') { + /// console.log("hello world from js init script"); /// - /// window.__MY_CUSTOM_PROPERTY__ = { foo: 'bar' } + /// window.__MY_CUSTOM_PROPERTY__ = { foo: 'bar' }; + /// } /// "#; /// /// fn init() -> TauriPlugin { diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index 7c9f8ca58..54870a04e 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -436,7 +436,35 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> { // ------------------------------------------- Webview attributes ------------------------------------------- - /// Sets the init script. + /// 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, + /// it's recommended to check the `window.location` to guard your script from running on unexpected origins. + /// + /// # Examples + /// + /// ```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::WindowBuilder::new(app, "label", tauri::WindowUrl::App("index.html".into())) + /// .initialization_script(INIT_SCRIPT) + /// .build()?; + /// Ok(()) + /// }); + /// } + /// ``` #[must_use] pub fn initialization_script(mut self, script: &str) -> Self { self