Crate webbrowser

source ·
Expand description

Rust library to open URLs and local files in the web browsers available on a platform, with guarantees of Consistent Behaviour.

Inspired by the webbrowser python library.

Examples

use webbrowser;

if webbrowser::open("http://github.com").is_ok() {
    // ...
}

Platform Support Status

PlatformSupportedBrowsersTest status
macosdefault + others
windowsdefault only
linux/wsl/*bsddefault only (respects $BROWSER env var, so can be used with other browsers)
androiddefault only
iosdefault only
wasmdefault only
haiku✅ (experimental)default only

Consistent Behaviour

webbrowser defines consistent behaviour on all platforms as follows:

  • Browser guarantee - This library guarantees that the browser is opened, even for local files - the only crate to make such guarantees at the time of this writing. Alternative libraries rely on existing system commands, which may lead to an editor being opened (instead of the browser) for local html files, leading to an inconsistent behaviour for users.
  • Non-Blocking for GUI based browsers (e.g. Firefox, Chrome etc.), while Blocking for text based browser (e.g. lynx etc.)
  • Suppressed output by default for GUI based browsers, so that their stdout/stderr don’t pollute the main program’s output. This can be overridden by webbrowser::open_browser_with_options.

Crate Features

webbrowser optionally allows the following features to be configured:

  • hardened - this disables handling of non-http(s) urls (e.g. file:///) as a hard security precaution
  • disable-wsl - this disables WSL file implementation (http still works)
  • wasm-console - this enables logging to wasm console (valid only on wasm platform)

Structs

BrowserOptions to override certain default behaviour. Any option named as a hint is not guaranteed to be honoured. Use BrowserOptions::new() to create.
The Error type for parsing a string into a Browser.

Enums

Browser types available

Functions

Opens the URL on the default browser of this platform
Opens the specified URL on the specific browser (if available) requested. Return semantics are the same as for open.
Opens the specified URL on the specific browser (if available) requested, while overriding the default options.