Documentation
¶
Overview ¶
Package webview embeds the system web view (WKWebView on macOS, WebView2 on Windows, 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 ¶
- func ClearSessionCookies(done func())
- func Cookies(url string, done func(cookies []Cookie, ok bool))
- func SetCookie(url, header string, done func(ok bool))
- type Container
- type Cookie
- type LoadRequest
- type NewWindow
- type Options
- type Request
- type Response
- type SchemeHandler
- type WebView
- func (w *WebView) AddScript(js string)
- func (w *WebView) CanGoBack() (can, known bool)
- func (w *WebView) CanGoForward() (can, known bool)
- func (w *WebView) Control() *swt.Composite
- func (w *WebView) Dispose()
- func (w *WebView) Eval(js string, done func(result string, err error))
- func (w *WebView) GoBack()
- func (w *WebView) GoForward()
- func (w *WebView) HandleScheme(scheme string, h SchemeHandler) error
- func (w *WebView) Load(r LoadRequest) bool
- func (w *WebView) Navigate(url string)
- func (w *WebView) Prestart(done func()) bool
- func (w *WebView) Reload()
- func (w *WebView) SetCallHandler(f func(msg string) string) bool
- func (w *WebView) SetCloseHandler(f func()) bool
- func (w *WebView) SetHTML(html, baseURL string)
- func (w *WebView) SetNavigationPolicy(f func(url string, mainFrame bool) bool) bool
- func (w *WebView) SetNewWindowHandler(f func(n *NewWindow) *WebView) bool
- func (w *WebView) SetScriptEnabled(on bool) bool
- func (w *WebView) SetShowHandler(f func(WindowFeatures)) bool
- func (w *WebView) SetStatusHandler(f func(text string)) bool
- func (w *WebView) Stop()
- func (w *WebView) URL() string
- type WindowFeatures
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.
Types ¶
type Container ¶
Container is where a web view goes: a gowt container (Panel, Window, Group, ...) or an swt.Composite.
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 ¶
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 SchemeHandler ¶
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)
OnTitleChanged func(title string)
// contains filtered or unexported fields
}
func New ¶
New adds a web view filling a new Composite inside parent; give parent a Fill layout, gowt.Fill(...), so that the view takes its whole area. The native view is created at the first load, so HandleScheme and AddScript must come before it.
func NewOn ¶
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 ¶
AddScript runs js at document start, before page scripts, in every page loaded afterwards.
func (*WebView) CanGoBack ¶
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 (*WebView) Control ¶
Control is the Composite hosting the view: set its layout data, size and visibility.
func (*WebView) Eval ¶
Eval runs js as a global script and reports its value as JSON text ("" for undefined), or the script error, asynchronously.
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) Prestart ¶
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) SetCallHandler ¶
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 ¶
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) SetNavigationPolicy ¶
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 ¶
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 ¶
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 ¶
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.
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).