webview

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: EPL-2.0 Imports: 19 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package webview embeds the system web view (WKWebView on macOS, WebKitGTK 4.1 on Linux) in an SWT Composite and bridges it to Go: custom URL schemes served from Go, page-to-Go messages, async script evaluation. Everything must be called on the SWT UI thread; callbacks run there too.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearSessionCookies

func ClearSessionCookies(done func())

ClearSessionCookies deletes the cookies without an expiry date; done runs when that is over.

func Cookies

func Cookies(url string, done func(cookies []Cookie, ok bool))

Cookies reports the cookies the session would send to url. Calls and callbacks are on the UI thread. ok is false when the engine has no cookie store to ask (no view exists yet on Windows).

func SetCookie

func SetCookie(url, header string, done func(ok bool))

SetCookie stores header (the value of a Set-Cookie field: "name=value; Path=/; Expires=...") as if the page at url had sent it, and reports whether the store took it.

Types

type Cookie struct{ Name, Value string }

Cookie is a cookie the session holds.

type LoadRequest

type LoadRequest struct {
	URL string
	// Method is "GET" or "POST"; "" is GET.
	Method  string
	Headers map[string]string
	Body    []byte
}

LoadRequest is a navigation with a method, headers and a body.

type NewWindow

type NewWindow struct {
	// URL the page asked for; "" or "about:blank" for window.open() without an address.
	URL string
	// contains filtered or unexported fields
}

NewWindow is a page's request for a new window (window.open, a link with target=_blank). It is valid only while the handler given to SetNewWindowHandler runs.

func (*NewWindow) NewOn

func (n *NewWindow) NewOn(host *swt.Composite, opts Options) (*WebView, error)

NewOn makes the view that takes the page of the request, with host as its hosting Composite as the package-level NewOn does. Call it inside the handler, at most once; the handler returns the view. The new view shares the user scripts, the message handler and the session (cookies, storage) of the opener, as the engine's own popups do: scripts added with AddScript later are not added to a popup, and on Linux OnMessage of the popup and the opener both hear the page of either.

type Options

type Options struct {
	// Inspectable lets Safari's Web Inspector attach (macOS 13.3+).
	Inspectable bool
	// contains filtered or unexported fields
}

type Request

type Request struct{ URL, Method string }

type Response

type Response struct {
	Status   int // 0 means 200
	MimeType string
	Headers  map[string]string
	Body     []byte
}

type SchemeHandler

type SchemeHandler func(req Request) *Response

SchemeHandler answers requests to a custom scheme; a nil Response is a 404.

type WebView

type WebView struct {
	// OnMessage gets what the page passes to window.gowt.postMessage (non-strings as JSON).
	OnMessage            func(msg string)
	OnNavigationStarted  func(url string)
	OnNavigationFinished func(url string)
	OnNavigationFailed   func(url string, err error)
	OnTitleChanged       func(title string)
	// contains filtered or unexported fields
}

func New

func New(parent *swt.Composite, opts Options) (*WebView, error)

New adds a web view filling a new Composite inside parent. The native view is created at the first load, so HandleScheme and AddScript must come before it.

func NewOn

func NewOn(host *swt.Composite, opts Options) (*WebView, error)

NewOn is New with host itself as the hosting Composite instead of a new child of it, for a widget that is the web view (swt Browser). An engine that cannot host in an existing Composite adds a child to host as New does.

func (*WebView) AddScript

func (w *WebView) AddScript(js string)

AddScript runs js at document start, before page scripts, in every page loaded afterwards.

func (*WebView) CanGoBack

func (w *WebView) CanGoBack() (can, known bool)

CanGoBack and CanGoForward report whether the history has an entry to go to; known is false when the engine cannot tell.

func (*WebView) CanGoForward

func (w *WebView) CanGoForward() (can, known bool)

func (*WebView) Control

func (w *WebView) Control() *swt.Composite

Control is the Composite hosting the view: set its layout data, size and visibility.

func (*WebView) Dispose

func (w *WebView) Dispose()

func (*WebView) Eval

func (w *WebView) Eval(js string, done func(result string, err error))

Eval runs js as a global script and reports its value as JSON text ("" for undefined), or the script error, asynchronously.

func (*WebView) GoBack

func (w *WebView) GoBack()

func (*WebView) GoForward

func (w *WebView) GoForward()

func (*WebView) HandleScheme

func (w *WebView) HandleScheme(scheme string, h SchemeHandler) error

HandleScheme serves scheme:// URLs from Go. It fails once the page has been loaded.

func (*WebView) Load

func (w *WebView) Load(r LoadRequest) bool

Load navigates with r. It reports whether the engine can send r: the engines can send a GET with headers and a POST. On Linux the body of a POST goes through Go's net/http, not WebKit (WebKitGTK has no API for it), so WebKit's cookie jar and proxy settings are not used for that request, and a failure shows as a page with the error text.

func (*WebView) Navigate

func (w *WebView) Navigate(url string)

func (*WebView) Prestart

func (w *WebView) Prestart(done func()) bool

Prestart creates the native view now instead of at the first load and calls done once it is ready or has failed; it reports false, without calling done, on an engine that has nothing to prepare. Like the first load, it ends HandleScheme and AddScript.

func (*WebView) Reload

func (w *WebView) Reload()

func (*WebView) SetCallHandler

func (w *WebView) SetCallHandler(f func(msg string) string) bool

SetCallHandler answers window.gowt.call(msg) in the page: the page blocks until f returns its reply as a string (msg is a string or the JSON of any other value). It reports whether the engine can; without it window.gowt.call is missing or returns null. Set it before the first load. Only calls made by the main frame reach f: a subframe (maybe cross-origin) must get null and f is not called, or any iframe could use what f hands out. f may panic; the engine then answers null. While f runs the page is blocked, so f cannot wait for a script evaluated in that page.

func (*WebView) SetCloseHandler

func (w *WebView) SetCloseHandler(f func()) bool

SetCloseHandler calls f when the page asks to close its window (window.close); the view is not closed by the package: f disposes whatever hosts it. It reports whether the engine can.

func (*WebView) SetHTML

func (w *WebView) SetHTML(html, baseURL string)

func (*WebView) SetNavigationPolicy

func (w *WebView) SetNavigationPolicy(f func(url string, mainFrame bool) bool) bool

SetNavigationPolicy makes the engine ask f before every navigation, in the main frame or a subframe; false cancels it. It reports whether the engine can ask. Set it before the first load.

func (*WebView) SetNewWindowHandler

func (w *WebView) SetNewWindowHandler(f func(n *NewWindow) *WebView) bool

SetNewWindowHandler lets f decide what a page's request for a new window gets: f returns the WebView made with n.NewOn that shows the page, or nil to refuse the window. The new view is loaded by the engine with the requested address; it is not shown or placed by the package: put its Control in a window and listen to SetShowHandler for the moment the page wants it shown. It reports whether the engine can; set it before the first load. Without a handler the engine's own default applies: the window is refused on macOS and Linux, and WebView2 opens a browser window of its own on Windows. While f runs the opener's page is blocked: do not wait there for a script of the opener.

func (*WebView) SetScriptEnabled

func (w *WebView) SetScriptEnabled(on bool) bool

SetScriptEnabled switches the scripts of the pages loaded after the call; the page on screen keeps what it has. On macOS and Windows Eval still runs scripts in such a page; on Linux it does not answer. It reports whether the engine can.

func (*WebView) SetShowHandler

func (w *WebView) SetShowHandler(f func(WindowFeatures)) bool

SetShowHandler calls f once for a view made by NewOn, when its page wants to be shown, with the window.open features. The engines that have no such moment (macOS, Windows) call it right after the view is made. It reports whether the engine can.

func (*WebView) SetStatusHandler

func (w *WebView) SetStatusHandler(f func(text string)) bool

SetStatusHandler calls f with the address of the link under the pointer, and with "" when the pointer leaves it. It reports whether the engine can; on macOS the page tells it (a script injected in every frame), so a page that stops mouse events or a frame the script does not reach is silent.

func (*WebView) Stop

func (w *WebView) Stop()

Stop cancels the page load in progress; a no-op without the feature.

func (*WebView) URL

func (w *WebView) URL() string

URL is the current page's URL ("" before the first load).

type WindowFeatures

type WindowFeatures struct {
	X, Y, Width, Height                      int
	MenuBar, StatusBar, ToolBar, LocationBar bool
}

WindowFeatures are what window.open asked for. A zero Width and Height mean the page gave no size. X and Y are in the engine's own units (points on macOS, pixels elsewhere).

Jump to

Keyboard shortcuts

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