From 824847e302bdc30ee721acc98e364870d63260c9 Mon Sep 17 00:00:00 2001 From: hossein Date: Sat, 27 Jun 2020 17:47:30 +0430 Subject: [PATCH] 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()); }