2022-11-27 02:37:23 +00:00
|
|
|
use super::*;
|
|
|
|
use core::sync::atomic::{AtomicI8, Ordering};
|
|
|
|
use js_sys::{global, Reflect};
|
2022-11-27 15:52:07 +00:00
|
|
|
use wasm_bindgen::prelude::*;
|
2022-11-27 02:37:23 +00:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
extern "C" {
|
|
|
|
// Use `js_namespace` here to bind `console.log(..)` instead of just
|
|
|
|
// `log(..)`
|
|
|
|
#[wasm_bindgen(js_namespace = console, js_name = log)]
|
|
|
|
pub fn console_log(s: &str);
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn alert(s: &str);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_browser() -> bool {
|
|
|
|
static CACHE: AtomicI8 = AtomicI8::new(-1);
|
|
|
|
let cache = CACHE.load(Ordering::Relaxed);
|
|
|
|
if cache != -1 {
|
|
|
|
return cache != 0;
|
|
|
|
}
|
|
|
|
|
2022-12-19 22:01:42 +00:00
|
|
|
let res = Reflect::has(&global().as_ref(), &"navigator".into()).unwrap_or_default();
|
2022-11-27 02:37:23 +00:00
|
|
|
|
|
|
|
CACHE.store(res as i8, Ordering::Relaxed);
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2022-11-27 15:52:07 +00:00
|
|
|
pub fn is_browser_https() -> bool {
|
|
|
|
static CACHE: AtomicI8 = AtomicI8::new(-1);
|
|
|
|
let cache = CACHE.load(Ordering::Relaxed);
|
|
|
|
if cache != -1 {
|
|
|
|
return cache != 0;
|
|
|
|
}
|
2022-11-27 02:37:23 +00:00
|
|
|
|
2022-12-19 22:01:42 +00:00
|
|
|
let res = js_sys::eval("self.location.protocol === 'https'")
|
2022-11-27 15:52:07 +00:00
|
|
|
.map(|res| res.is_truthy())
|
|
|
|
.unwrap_or_default();
|
2022-11-27 02:37:23 +00:00
|
|
|
|
2022-11-27 15:52:07 +00:00
|
|
|
CACHE.store(res as i8, Ordering::Relaxed);
|
2022-11-27 02:37:23 +00:00
|
|
|
|
2022-11-27 15:52:07 +00:00
|
|
|
res
|
|
|
|
}
|
2022-11-27 02:37:23 +00:00
|
|
|
|
2022-12-19 22:01:42 +00:00
|
|
|
pub fn get_wasm_global_string_value<K: AsRef<str>>(key: K) -> Option<String> {
|
|
|
|
let Ok(v) = Reflect::get(&global().as_ref(), &JsValue::from_str(key.as_ref())) else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
v.as_string()
|
|
|
|
}
|
|
|
|
|
2022-11-27 02:37:23 +00:00
|
|
|
#[derive(ThisError, Debug, Clone, Eq, PartialEq)]
|
|
|
|
#[error("JsValue error")]
|
|
|
|
pub struct JsValueError(String);
|
|
|
|
|
|
|
|
pub fn map_jsvalue_error(x: JsValue) -> JsValueError {
|
|
|
|
JsValueError(x.as_string().unwrap_or_default())
|
|
|
|
}
|