1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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)
}