workflow_dom/
utils.rs

1//! Helper functions for accessing DOM environment
2use wasm_bindgen::JsCast;
3use web_sys::{Document, Element, Window};
4
5/// Return the current browser [`web_sys::Window`] element
6pub fn window() -> Window {
7    web_sys::window().unwrap()
8}
9
10/// Return the current browser [`web_sys::Document`] element
11pub fn document() -> Document {
12    web_sys::window().unwrap().document().unwrap()
13}
14
15/// Return the `body` element of the current document
16pub fn body() -> std::result::Result<Element, String> {
17    let b = document()
18        .query_selector("body")
19        .unwrap()
20        .ok_or_else(|| "Unable to get body element".to_string())?;
21    Ok(b)
22}
23
24pub fn location() -> Result<web_sys::Location, wasm_bindgen::JsValue> {
25    let location = js_sys::Reflect::get(&js_sys::global(), &"location".into())?;
26    location.dyn_into()
27}