use wasm_bindgen::prelude::*;
use web_sys::{Document, HtmlElement, Window};
pub fn document() -> Document {
let window = web_sys::window().expect("no global `window` exists");
window.document().expect("unable to get `document` node")
}
pub fn body(win: Option<Window>) -> HtmlElement {
let window = win.unwrap_or_else(|| web_sys::window().expect("no global `window` exists"));
let document = window.document().expect("unable to get `document` node");
document.body().expect("unable to get `document.body` node")
}
pub fn window() -> Window {
web_sys::window().expect("no global `window` exists")
}
pub fn try_get_u64_from_prop(jsv: &JsValue, prop: &str) -> Result<u64, JsValue> {
let v = js_sys::Reflect::get(jsv, &JsValue::from(prop))?;
Ok(v.as_f64().ok_or_else(|| {
JsValue::from(format!(
"try_get_u64(): error parsing property '{}' with value '{:?}'",
prop, v
))
})? as u64)
}
pub fn try_get_f64_from_prop(jsv: &JsValue, prop: &str) -> Result<f64, JsValue> {
let v = js_sys::Reflect::get(jsv, &JsValue::from(prop))?;
v.as_f64().ok_or_else(|| {
JsValue::from(format!(
"try_get_f64(): error parsing property '{}' with value '{:?}'",
prop, v
))
})
}
pub fn try_get_bool_from_prop(jsv: &JsValue, prop: &str) -> Result<bool, JsValue> {
js_sys::Reflect::get(jsv, &JsValue::from(prop))?
.as_bool()
.ok_or_else(|| {
JsValue::from(format!(
"try_get_bool(): property {} is missing or not a boolean",
prop
))
})
}
pub fn try_get_js_value(this_jsv: &JsValue, prop: &str) -> Result<JsValue, JsValue> {
let v = js_sys::Reflect::get(this_jsv, &JsValue::from(prop))?;
Ok(v)
}