From f5ad0e62a3c1067822a122424167e807fb694d5f Mon Sep 17 00:00:00 2001 From: hossein Date: Thu, 25 Jun 2020 20:04:57 +0430 Subject: [PATCH 1/3] featuer/add support for wasm --- Cargo.toml | 3 +++ src/lib.rs | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c428f8c..16d391f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,9 @@ keywords = ["webbrowser", "browser"] license = "MIT OR Apache-2.0" edition = "2015" +[dependencies] +wasm-bindgen = "0.2" + [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.6", features = ["combaseapi", "objbase", "shellapi", "winerror"] } widestring = "0.4.0" diff --git a/src/lib.rs b/src/lib.rs index 4058ce2..0c736e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -240,7 +240,7 @@ fn open_browser_internal(browser: Browser, url: &str) -> Result { } /// Deal with opening of browsers on Mac OS X, using `open` command -#[cfg(target_os = "macos")] +#[cfg(any(target_os = "macos", target_os = "wasm32"))] #[inline] fn open_browser_internal(browser: Browser, url: &str) -> Result { let mut cmd = Command::new("open"); @@ -357,9 +357,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_os = "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 +387,12 @@ fn test_open_internet_explorer() { assert!(open_browser(Browser::InternetExplorer, "http://github.com").is_ok()); } +#[test] +#[cfg(target_os = "wasm32")] +fn test_open_default_wasm() { + assert!(open("http://github.com").is_ok()); +} + #[test] #[ignore] fn test_open_safari() { From 824847e302bdc30ee721acc98e364870d63260c9 Mon Sep 17 00:00:00 2001 From: hossein Date: Sat, 27 Jun 2020 17:47:30 +0430 Subject: [PATCH 2/3] change approach --- Cargo.toml | 10 +++++++--- src/lib.rs | 19 ++++++++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 16d391f..d2384ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,10 +9,14 @@ repository = "https://github.com/amodm/webbrowser-rs" readme = "README.md" keywords = ["webbrowser", "browser"] license = "MIT OR Apache-2.0" -edition = "2015" +edition = "2018" -[dependencies] -wasm-bindgen = "0.2" + +[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 0c736e0..161fd19 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,18 @@ 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().expect("should have a window in this context"); + window.open_with_url(url); + Ok(()) +} + /// Opens the specified URL on the specific browser (if available) requested. Return semantics are /// the same as for [open](fn.open.html). /// @@ -164,6 +175,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() { @@ -240,7 +252,7 @@ fn open_browser_internal(browser: Browser, url: &str) -> Result { } /// Deal with opening of browsers on Mac OS X, using `open` command -#[cfg(any(target_os = "macos", target_os = "wasm32"))] +#[cfg(target_os = "macos")] #[inline] fn open_browser_internal(browser: Browser, url: &str) -> Result { let mut cmd = Command::new("open"); @@ -358,7 +370,7 @@ fn open_on_unix_using_browser_env(url: &str) -> Result { target_os = "netbsd", target_os = "openbsd", target_os = "haiku", - target_os = "wasm32" + target_arch = "wasm32" )))] compile_error!("Only Windows, Mac OS, Linux, *BSD and Haiku and Wasm32 are currently supported"); @@ -387,8 +399,9 @@ fn test_open_internet_explorer() { assert!(open_browser(Browser::InternetExplorer, "http://github.com").is_ok()); } + #[test] -#[cfg(target_os = "wasm32")] +#[cfg(target_arch = "wasm32")] fn test_open_default_wasm() { assert!(open("http://github.com").is_ok()); } From 6c5d2d49c87cb41693dbad78d8f9b0965caba70e Mon Sep 17 00:00:00 2001 From: hossein Date: Thu, 2 Jul 2020 20:09:24 +0430 Subject: [PATCH 3/3] fix issues --- Cargo.toml | 3 +-- src/lib.rs | 11 ++++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d2384ae..2b91b9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,7 @@ keywords = ["webbrowser", "browser"] license = "MIT OR Apache-2.0" edition = "2018" - -[dependencies.web-sys] +[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys] version = "0.3.36" features = [ 'Window' diff --git a/src/lib.rs b/src/lib.rs index 161fd19..2921109 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -159,9 +159,14 @@ pub fn open(url: &str) -> Result { #[cfg(target_arch = "wasm32")] pub fn open(url: &str) -> Result<()> { - let window = web_sys::window().expect("should have a window in this context"); - window.open_with_url(url); - Ok(()) + 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