Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
29 changes: 27 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -149,10 +152,23 @@ impl FromStr for Browser {
/// // ...
/// }
/// ```
#[cfg(not(target_arch = "wasm32"))]
pub fn open(url: &str) -> Result<Output> {
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).
///
Expand All @@ -164,6 +180,7 @@ pub fn open(url: &str) -> Result<Output> {
/// // ...
/// }
/// ```
#[cfg(not(target_arch = "wasm32"))]
pub fn open_browser(browser: Browser, url: &str) -> Result<Output> {
open_browser_internal(browser, url).and_then(|status| {
if let Some(code) = status.code() {
Expand Down Expand Up @@ -357,9 +374,10 @@ fn open_on_unix_using_browser_env(url: &str) -> Result<ExitStatus> {
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() {
Expand All @@ -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() {
Expand Down