From 5cbf2698fdd9c7d94075d266dba1f2c2d7872e9d Mon Sep 17 00:00:00 2001 From: Ilya Maximov Date: Thu, 17 Mar 2022 23:47:11 +0100 Subject: [PATCH] add `use_eval()`to desktop and web --- packages/desktop/src/desktop_context.rs | 17 ++++++++++++++++- packages/desktop/src/lib.rs | 2 +- packages/web/src/lib.rs | 2 ++ packages/web/src/util.rs | 23 +++++++++++++++++++++++ src/lib.rs | 6 ++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/desktop/src/desktop_context.rs b/packages/desktop/src/desktop_context.rs index def9a21d0..b15e26ea3 100644 --- a/packages/desktop/src/desktop_context.rs +++ b/packages/desktop/src/desktop_context.rs @@ -193,6 +193,21 @@ pub(super) fn handler( DevTool => webview.devtool(), - Eval(code) => webview.evaluate_script(code.as_str()).expect("failed to eval script"), + Eval(code) => webview + .evaluate_script(code.as_str()) + .expect("failed to eval script"), } } + +/// Get a closure that executes any JavaScript in the WebView context. +/// +/// # Panics +/// +/// The closure will cause the message processing thread to panic if the +/// provided script is not valid JavaScript code or if it returns an uncaught +/// error. +pub fn use_eval(cx: &ScopeState) -> impl Fn(S) + '_ { + let desktop = use_window(&cx); + + move |script| desktop.eval(script) +} diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs index 4b2615d54..5f41b98ce 100644 --- a/packages/desktop/src/lib.rs +++ b/packages/desktop/src/lib.rs @@ -11,7 +11,7 @@ mod events; mod protocol; use desktop_context::UserWindowEvent; -pub use desktop_context::{use_window, DesktopContext}; +pub use desktop_context::{use_window, use_eval, DesktopContext}; pub use wry; pub use wry::application as tao; diff --git a/packages/web/src/lib.rs b/packages/web/src/lib.rs index c82dc4975..1ec9fe068 100644 --- a/packages/web/src/lib.rs +++ b/packages/web/src/lib.rs @@ -62,12 +62,14 @@ use dioxus::VirtualDom; pub use dioxus_core as dioxus; use dioxus_core::prelude::Component; use futures_util::FutureExt; +pub use crate::util::use_eval; mod cache; mod cfg; mod dom; mod rehydrate; mod ric_raf; +mod util; /// Launch the VirtualDOM given a root component and a configuration. /// diff --git a/packages/web/src/util.rs b/packages/web/src/util.rs index 638cf3dc2..696af7b21 100644 --- a/packages/web/src/util.rs +++ b/packages/web/src/util.rs @@ -1 +1,24 @@ //! Utilities specific to websys + +use dioxus_core::*; + +/// Get a closure that executes any JavaScript in the webpage. +/// +/// # Safety +/// +/// Please be very careful with this function. A script with too many dynamic +/// parts is practically asking for a hacker to find an XSS vulnerability in +/// it. **This applies especially to web targets, where the JavaScript context +/// has access to most, if not all of your application data.** +/// +/// # Panics +/// +/// The closure will panic if the provided script is not valid JavaScript code +/// or if it returns an uncaught error. +pub fn use_eval(_cx: &ScopeState) -> impl Fn(S) { + |script| { + js_sys::Function::new_no_args(&script.to_string()) + .call0(&wasm_bindgen::JsValue::NULL) + .expect("failed to eval script"); + } +} diff --git a/src/lib.rs b/src/lib.rs index cebffcf17..2ba5a9045 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,3 +44,9 @@ pub mod prelude { #[cfg(feature = "fermi")] pub use fermi::{use_atom_ref, use_init_atom_root, use_read, use_set, Atom, AtomRef}; } + +#[cfg(all(target = "wasm", feature = "web"))] +pub use dioxus_web::use_eval; + +#[cfg(all(not(target = "wasm"), feature = "desktop"))] +pub use dioxus_desktop::use_eval; \ No newline at end of file