browser

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2021 License: BSD-3-Clause Imports: 7 Imported by: 2

README

Introduction

browser is a dependency-free library for opening URLs (or files) in the most appropriate browser or assigned application, respectively. Based on the operating system and available executables, it tries to open URLs in the following sequence:

  1. command defined by the environment variable $BROWSER (if set)
  2. operating system specific launcher
    • macOS: /usr/bin/open
    • Windows: cmd /c start
    • otherwise: xdg-open (if environment variable $DISPLAY is set)
  3. one of the following binaries from $PATH
    • chrome
    • google-chrome
    • chromium
    • firefox

Use browser as library

Installation

go get -d -u github.com/abc-inc/browser

Usage

import "github.com/abc-inc/browser"

...

if ok := browser.Open(url); !ok {
    // something bad happened
}

Use browser as standalone application

Installation

go get -u github.com/abc-inc/browser

Examples

# open a URL in the default browser
browser http://localhost/

# open a local file in the default browser
browser file:///C:/Temp/report.html

# macOS: use Firefox even if another default web browser is set
BROWSER="open -a Firefox" browser http://localhost/file.zip

# download a file with wget or curl - whatever is available
BROWSER="wget:curl -o index.html" browser http://localhost/

Since browser leverages operating system specific launchers, it can also be used to open other files and directories:

# play audio file using the media player associated with the file type
browser file:///home/me/Music/track.mp3

# open a folder in Visual Studio Code or vi (must be installed separately)
BROWSER=code:vi browser file:///Users/me/dev/project

Similar Projects

Feature abc-inc/browser pkg/browser hashicorp/terraform
Windows/Linux/macOS/OpenBSD
FreeBSD/NetBSD (xdg-open)
WSL support
$BROWSER support
open from io.Reader
mock support
custom Cmd (Args, Dir, Env, etc.)

Documentation

Overview

Package browser provides utilities for interacting with users' browsers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Commands

func Commands() [][]string

Commands returns a list of possible commands to use to open a url.

func Open

func Open(url string) bool

Open tries to open url in a browser and reports whether it succeeded.

Example (File)
package main

import (
	"github.com/abc-inc/browser"
)

func main() {
	_ = browser.Open("file:///tmp")
}
Example (Url)
package main

import (
	"github.com/abc-inc/browser"
)

func main() {
	_ = browser.Open("http://localhost/")
}

func OpenCmd

func OpenCmd(url string, cust func(cmd *exec.Cmd) *exec.Cmd) bool

OpenCmd tries to open url in a browser using customized Cmds and reports whether it succeeded. If cust returns nil, the Cmd is skipped.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL