Merge pull request #318 from overlisted/eval-stuff

Eval stuff
This commit is contained in:
Jon Kelley authored and GitHub committed 2022-04-04 13:01:35 -04:00
commit 56ea9e42fa
7 files changed
+83 -4

No files matched your search

+2
View File
@@ -57,6 +57,7 @@
use std::rc::Rc;
pub use crate::cfg::WebConfig;
pub use crate::util::use_eval;
use dioxus::SchedulerMsg;
use dioxus::VirtualDom;
pub use dioxus_core as dioxus;
@@ -68,6 +69,7 @@ mod cfg;
mod dom;
mod rehydrate;
mod ric_raf;
mod util;
/// Launch the VirtualDOM given a root component and a configuration.
///
+25
View File
@@ -1 +1,26 @@
//! 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<S: std::string::ToString>(cx: &ScopeState) -> &dyn Fn(S) {
cx.use_hook(|_| {
|script: S| {
js_sys::Function::new_no_args(&script.to_string())
.call0(&wasm_bindgen::JsValue::NULL)
.expect("failed to eval script");
}
})
}