-
Notifications
You must be signed in to change notification settings - Fork 43
/
wasm.rs
42 lines (38 loc) · 1.62 KB
/
wasm.rs
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
use crate::{Browser, Error, ErrorKind, Result};
/// Deal with opening a URL in wasm32. This implementation ignores the browser attribute
/// and always opens URLs in the same browser where wasm32 vm is running.
#[inline]
pub fn open_browser_internal(_: Browser, url: &str) -> Result<()> {
// we can override the target by the env var WEBBROWSER_WASM_TARGET at compile time
let configured_target = option_env!("WEBBROWSER_WASM_TARGET");
let window = web_sys::window();
match window {
Some(w) => {
let target = configured_target.unwrap_or_else(|| "_blank");
wasm_console_log(&format!("target for url {} detected as {}", url, target));
match w.open_with_url_and_target(url, target) {
Ok(x) => match x {
Some(_) => Ok(()),
None => {
wasm_console_log(POPUP_ERR_MSG);
Err(Error::new(ErrorKind::Other, POPUP_ERR_MSG))
}
},
Err(_) => {
wasm_console_log("window error while opening url");
Err(Error::new(ErrorKind::Other, "error opening url"))
}
}
}
None => Err(Error::new(
ErrorKind::Other,
"should have a window in this context",
)),
}
}
/// Print to browser console
fn wasm_console_log(_msg: &str) {
#[cfg(all(debug_assertions, feature = "wasm-console"))]
web_sys::console::log_1(&format!("[webbrowser] {}", &_msg).into());
}
const POPUP_ERR_MSG: &'static str = "popup blocked? window detected, but open_url failed";