Skip to content

Commit 824847e

Browse files
committed
change approach
1 parent f5ad0e6 commit 824847e

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ repository = "https://github.com/amodm/webbrowser-rs"
99
readme = "README.md"
1010
keywords = ["webbrowser", "browser"]
1111
license = "MIT OR Apache-2.0"
12-
edition = "2015"
12+
edition = "2018"
1313

14-
[dependencies]
15-
wasm-bindgen = "0.2"
14+
15+
[dependencies.web-sys]
16+
version = "0.3.36"
17+
features = [
18+
'Window'
19+
]
1620

1721
[target.'cfg(windows)'.dependencies]
1822
winapi = { version = "0.3.6", features = ["combaseapi", "objbase", "shellapi", "winerror"] }

src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ use std::process::Command;
5151
#[cfg(windows)]
5252
use widestring::U16CString;
5353

54+
#[cfg(target_arch = "wasm32")]
55+
use web_sys::{Window};
56+
5457
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
5558
/// Browser types available
5659
pub enum Browser {
@@ -149,10 +152,18 @@ impl FromStr for Browser {
149152
/// // ...
150153
/// }
151154
/// ```
155+
#[cfg(not(target_arch = "wasm32"))]
152156
pub fn open(url: &str) -> Result<Output> {
153157
open_browser(Browser::Default, url)
154158
}
155159

160+
#[cfg(target_arch = "wasm32")]
161+
pub fn open(url: &str) -> Result<()> {
162+
let window = web_sys::window().expect("should have a window in this context");
163+
window.open_with_url(url);
164+
Ok(())
165+
}
166+
156167
/// Opens the specified URL on the specific browser (if available) requested. Return semantics are
157168
/// the same as for [open](fn.open.html).
158169
///
@@ -164,6 +175,7 @@ pub fn open(url: &str) -> Result<Output> {
164175
/// // ...
165176
/// }
166177
/// ```
178+
#[cfg(not(target_arch = "wasm32"))]
167179
pub fn open_browser(browser: Browser, url: &str) -> Result<Output> {
168180
open_browser_internal(browser, url).and_then(|status| {
169181
if let Some(code) = status.code() {
@@ -240,7 +252,7 @@ fn open_browser_internal(browser: Browser, url: &str) -> Result<ExitStatus> {
240252
}
241253

242254
/// Deal with opening of browsers on Mac OS X, using `open` command
243-
#[cfg(any(target_os = "macos", target_os = "wasm32"))]
255+
#[cfg(target_os = "macos")]
244256
#[inline]
245257
fn open_browser_internal(browser: Browser, url: &str) -> Result<ExitStatus> {
246258
let mut cmd = Command::new("open");
@@ -358,7 +370,7 @@ fn open_on_unix_using_browser_env(url: &str) -> Result<ExitStatus> {
358370
target_os = "netbsd",
359371
target_os = "openbsd",
360372
target_os = "haiku",
361-
target_os = "wasm32"
373+
target_arch = "wasm32"
362374
)))]
363375
compile_error!("Only Windows, Mac OS, Linux, *BSD and Haiku and Wasm32 are currently supported");
364376

@@ -387,8 +399,9 @@ fn test_open_internet_explorer() {
387399
assert!(open_browser(Browser::InternetExplorer, "http://github.com").is_ok());
388400
}
389401

402+
390403
#[test]
391-
#[cfg(target_os = "wasm32")]
404+
#[cfg(target_arch = "wasm32")]
392405
fn test_open_default_wasm() {
393406
assert!(open("http://github.com").is_ok());
394407
}

0 commit comments

Comments
 (0)