diff --git a/Cargo.toml b/Cargo.toml index c428f8c..2b91b9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,13 @@ repository = "https://github.com/amodm/webbrowser-rs" readme = "README.md" keywords = ["webbrowser", "browser"] license = "MIT OR Apache-2.0" -edition = "2015" +edition = "2018" + +[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys] +version = "0.3.36" +features = [ + 'Window' +] [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.6", features = ["combaseapi", "objbase", "shellapi", "winerror"] } diff --git a/src/lib.rs b/src/lib.rs index 4058ce2..2921109 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,6 +51,9 @@ use std::process::Command; #[cfg(windows)] use widestring::U16CString; +#[cfg(target_arch = "wasm32")] +use web_sys::{Window}; + #[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)] /// Browser types available pub enum Browser { @@ -149,10 +152,23 @@ impl FromStr for Browser { /// // ... /// } /// ``` +#[cfg(not(target_arch = "wasm32"))] pub fn open(url: &str) -> Result { open_browser(Browser::Default, url) } +#[cfg(target_arch = "wasm32")] +pub fn open(url: &str) -> Result<()> { + let window = web_sys::window(); + match window { + Some(w) => { + w.open_with_url(url); + Ok(()) + }, + None => Err(std::io::Error::new(ErrorKind::Other, "should have a window in this context")) + } +} + /// Opens the specified URL on the specific browser (if available) requested. Return semantics are /// the same as for [open](fn.open.html). /// @@ -164,6 +180,7 @@ pub fn open(url: &str) -> Result { /// // ... /// } /// ``` +#[cfg(not(target_arch = "wasm32"))] pub fn open_browser(browser: Browser, url: &str) -> Result { open_browser_internal(browser, url).and_then(|status| { if let Some(code) = status.code() { @@ -357,9 +374,10 @@ fn open_on_unix_using_browser_env(url: &str) -> Result { target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", - target_os = "haiku" + target_os = "haiku", + target_arch = "wasm32" )))] -compile_error!("Only Windows, Mac OS, Linux, *BSD and Haiku are currently supported"); +compile_error!("Only Windows, Mac OS, Linux, *BSD and Haiku and Wasm32 are currently supported"); #[test] fn test_open_default() { @@ -386,6 +404,13 @@ fn test_open_internet_explorer() { assert!(open_browser(Browser::InternetExplorer, "http://github.com").is_ok()); } + +#[test] +#[cfg(target_arch = "wasm32")] +fn test_open_default_wasm() { + assert!(open("http://github.com").is_ok()); +} + #[test] #[ignore] fn test_open_safari() {