Skip to main content

workflow_html/
utils.rs

1pub use wasm_bindgen::prelude::JsValue;
2pub use web_sys::{Document, Element, Window};
3
4/// Result type for DOM operations, carrying a [`JsValue`] error on failure.
5pub type ElementResult<T> = std::result::Result<T, JsValue>;
6
7/// Returns the global `document` node, panicking if no browser context exists.
8pub fn document() -> Document {
9    let window = web_sys::window().expect("no global `window` exists");
10    window.document().expect("unable to get `document` node")
11}
12
13/// Returns the global `window` object, panicking if no browser context exists.
14pub fn window() -> Window {
15    web_sys::window().expect("no global `window` exists")
16}
17
18/// Returns the DOM element with the given `id`, or `None` if not found.
19pub fn get_element_by_id(id: &str) -> Option<Element> {
20    document().get_element_by_id(id)
21}