webview

package module
v0.0.0-...-7a2f88b Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 12 Imported by: 0

README

webview_go

GoDoc Go Report Card

Fork of webview/webview_go with extra features. Go language binding for the webview library.

What's New
  • Cookie Management — Full cross-platform cookie APIs (GetCookies, SetCookie, DeleteCookie, ClearCookies) with native implementations for macOS (WebKit), Linux (WebKitGTK), and Windows (WebView2).
  • SetUserAgent — Update the native user agent string used by the embedded browser engine.
Getting Started

See Go package documentation for the Go API documentation, or simply read the source code.

Start with creating a new directory structure for your project.

mkdir my-project && cd my-project

Create a new Go module.

go mod init example.com/app

Save one of the example programs into your project directory.

curl -sSLo main.go "https://raw.githubusercontent.com/GopeedLab/webview_go/master/examples/basic/main.go"

Install dependencies.

go get github.com/GopeedLab/webview_go

Build the example. On Windows, add -ldflags="-H windowsgui" to the command line.

go build
Notes

Calling Eval() or Dispatch() before Run() does not work because the webview instance has only been configured and not yet started.

Cookie APIs are available through GetCookies, SetCookie, DeleteCookie, and ClearCookies on darwin, linux, and windows. They should be called on the UI thread.

Documentation

Overview

Example
w := New(true)
defer w.Destroy()
w.SetTitle("Hello")
w.Bind("noop", func() string {
	log.Println("hello")
	return "hello"
})
w.Bind("add", func(a, b int) int {
	return a + b
})
w.Bind("quit", func() {
	w.Terminate()
})
w.SetHtml(`<!doctype html>
		<html>
			<body>hello</body>
			<script>
				window.onload = function() {
					document.body.innerText = ` + "`hello, ${navigator.userAgent}`" + `;
					noop().then(function(res) {
						console.log('noop res', res);
						add(1, 2).then(function(res) {
							console.log('add res', res);
							quit();
						});
					});
				};
			</script>
		</html>
	)`)
w.Run()

Index

Examples

Constants

View Source
const (
	// Width and height are default size
	HintNone = C.WEBVIEW_HINT_NONE

	// Window size can not be changed by a user
	HintFixed = C.WEBVIEW_HINT_FIXED

	// Width and height are minimum bounds
	HintMin = C.WEBVIEW_HINT_MIN

	// Width and height are maximum bounds
	HintMax = C.WEBVIEW_HINT_MAX
)

Variables

This section is empty.

Functions

func IsAvailable

func IsAvailable() bool

IsAvailable checks whether the required webkit2gtk runtime library is present on this system by attempting to dlopen it.

Types

type Cookie struct {
	Name     string
	Value    string
	Domain   string
	Path     string
	Expires  time.Time
	Secure   bool
	HTTPOnly bool
}

Cookie describes a browser cookie exposed by the native cookie store.

type Hint

type Hint int

Hints are used to configure window sizing and resizing

type WebView

type WebView interface {

	// Run runs the main loop until it's terminated. After this function exits -
	// you must destroy the webview.
	Run()

	// Terminate stops the main loop. It is safe to call this function from
	// a background thread.
	Terminate()

	// Dispatch posts a function to be executed on the main thread. You normally
	// do not need to call this function, unless you want to tweak the native
	// window.
	Dispatch(f func())

	// Destroy destroys a webview and closes the native window.
	Destroy()

	// Window returns a native window handle pointer. When using GTK backend the
	// pointer is GtkWindow pointer, when using Cocoa backend the pointer is
	// NSWindow pointer, when using Win32 backend the pointer is HWND pointer.
	Window() unsafe.Pointer

	// SetTitle updates the title of the native window. Must be called from the UI
	// thread.
	SetTitle(title string)

	// SetSize updates native window size. See Hint constants.
	SetSize(w int, h int, hint Hint)

	// Navigate navigates webview to the given URL. URL may be a properly encoded data.
	// URI. Examples:
	// w.Navigate("https://github.com/webview/webview")
	// w.Navigate("data:text/html,%3Ch1%3EHello%3C%2Fh1%3E")
	// w.Navigate("data:text/html;base64,PGgxPkhlbGxvPC9oMT4=")
	Navigate(url string)

	// SetHtml sets the webview HTML directly.
	// Example: w.SetHtml(w, "<h1>Hello</h1>");
	SetHtml(html string)

	// SetUserAgent updates the native user agent used by the embedded browser engine.
	// Must be called on the UI thread and typically before navigation.
	SetUserAgent(userAgent string)

	// Init injects JavaScript code at the initialization of the new page. Every
	// time the webview will open a the new page - this initialization code will
	// be executed. It is guaranteed that code is executed before window.onload.
	Init(js string)

	// Eval evaluates arbitrary JavaScript code. Evaluation happens asynchronously,
	// also the result of the expression is ignored. Use RPC bindings if you want
	// to receive notifications about the results of the evaluation.
	Eval(js string)

	// Bind binds a callback function so that it will appear under the given name
	// as a global JavaScript function. Internally it uses webview_init().
	// Callback receives a request string and a user-provided argument pointer.
	// Request string is a JSON array of all the arguments passed to the
	// JavaScript function.
	//
	// f must be a function
	// f must return either value and error or just error
	Bind(name string, f interface{}) error

	// Removes a callback that was previously set by Bind.
	Unbind(name string) error

	// GetCookies returns cookies that match the provided URL.
	// Cookie APIs currently require platform support and should be called on the UI thread.
	GetCookies(url string) ([]Cookie, error)

	// SetCookie adds or updates a cookie.
	// Cookie APIs currently require platform support and should be called on the UI thread.
	SetCookie(cookie Cookie) error

	// DeleteCookie removes cookies that match the given name, domain and path.
	// Cookie APIs currently require platform support and should be called on the UI thread.
	DeleteCookie(name string, domain string, path string) error

	// ClearCookies removes all cookies in the current cookie store.
	// Cookie APIs currently require platform support and should be called on the UI thread.
	ClearCookies() error
	// SetVisible shows or hides the native window.
	// Pass false for headless mode (window is hidden but webview still runs).
	SetVisible(visible bool)
}

func New

func New(debug bool) WebView

New calls NewWindow to create a new window and a new webview instance. If debug is non-zero - developer tools will be enabled (if the platform supports them).

func NewHeadless

func NewHeadless(debug bool) WebView

NewHeadless creates a new webview instance that runs without a visible window. The webview is fully functional (JavaScript, navigation, bindings) but the native window is never shown. Call Destroy to clean up when done.

func NewWindow

func NewWindow(debug bool, window unsafe.Pointer) WebView

NewWindow creates a new webview instance. If debug is non-zero - developer tools will be enabled (if the platform supports them). Window parameter can be a pointer to the native window handle. If it's non-null - then child WebView is embedded into the given parent window. Otherwise a new window is created. Depending on the platform, a GtkWindow, NSWindow or HWND pointer can be passed here.

Directories

Path Synopsis
examples
basic command
bind command
headless command
libs
mswebview2
Dummy file to allow vendoring.
Dummy file to allow vendoring.
mswebview2/include
Dummy file to allow vendoring.
Dummy file to allow vendoring.
webview
Dummy file to allow vendoring.
Dummy file to allow vendoring.
webview/include
Dummy file to allow vendoring.
Dummy file to allow vendoring.

Jump to

Keyboard shortcuts

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