diff --git a/Cargo.toml b/Cargo.toml index 04bf5d8..6216058 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ web-sys = { version = "0.3.76", features = [ 'KeyboardEvent', 'CanvasRenderingContext2d', 'HtmlCanvasElement', + 'Location', ] } ratatui = { version = "0.29", default-features = false } console_error_panic_hook = "0.1.7" diff --git a/src/utils.rs b/src/utils.rs index 7762700..9373afe 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -9,3 +9,16 @@ pub fn set_document_title(title: &str) -> Result<(), Error> { .set_title(title); Ok(()) } + +/// Open a URL in a new tab or the current tab. +pub fn open_url(url: &str, new_tab: bool) -> Result<(), Error> { + let window = web_sys::window().ok_or(Error::UnableToRetrieveWindow)?; + if new_tab { + window.open_with_url(url)?; + } else { + let location = window.location(); + location.set_href(url)?; + location.replace(url)?; + } + Ok(()) +}