@@ -51,6 +51,9 @@ use std::process::Command;
5151#[ cfg( windows) ]
5252use 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
5659pub enum Browser {
@@ -149,10 +152,18 @@ impl FromStr for Browser {
149152/// // ...
150153/// }
151154/// ```
155+ #[ cfg( not( target_arch = "wasm32" ) ) ]
152156pub 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" ) ) ]
167179pub 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]
245257fn 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) ) ) ]
363375compile_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" ) ]
392405fn test_open_default_wasm ( ) {
393406 assert ! ( open( "http://github.com" ) . is_ok( ) ) ;
394407}
0 commit comments