Skip to main content

rustolio_utils/web_sys/
globals.rs

1use web_sys;
2
3use crate::error::{FromCustomError, FromJsValue};
4
5pub fn window() -> crate::Result<web_sys::Window> {
6    web_sys::window().context("No global window available")
7}
8
9pub fn document() -> crate::Result<web_sys::Document> {
10    window()?.document().context("No global document available")
11}
12
13pub fn body() -> crate::Result<web_sys::HtmlElement> {
14    document()?.body().context("No global body available")
15}
16
17pub fn location() -> crate::Result<web_sys::Location> {
18    Ok(window()?.location())
19}
20
21pub fn url() -> crate::Result<web_sys::Url> {
22    web_sys::Url::new(
23        &location()?
24            .href()
25            .context("Failed to get `href` from global location")?,
26    )
27    .context("Failed to construct `Url` from global location")
28}
29
30pub fn history() -> crate::Result<web_sys::History> {
31    window()?.history().context("No global history")
32}
33
34pub fn local_storage() -> crate::Result<super::storage::LocalStorage> {
35    super::storage::LocalStorage::global()
36}