webview2

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2021 License: MIT Imports: 16 Imported by: 0

README

webview

This is a fork of go-webview2

No EdgeHTML fallback

Added some functions I need:
  1. High DPI support
  2. Remove the blank icon in the title bar (because I don't know how to modify the icon, if you know, please open an Issue)
  3. Prompt to download WebView2 runtime (based on dlgs)
  4. Implemented disable debugging in non-debug mode: prevent default context menus, disable DevTools, disable built in error page, disable status bar and disable zoom control.
  5. You can call webview2.DisableWebSecurity = true before creating the window to disable CORS (CORS is still enabled by default)
  6. You can set webview2.UserDataFolder = "Your own UserDataFolder" before creating the window to custom userData folder
  7. You can set webview2.UserAgent = "Your own UserAgent" before creating the window to custom UserAgent
  8. webview2.NewWindow can now use existing windows(Untested)
Other Changes:
  • Use functions in Golang's os package instead of GetModuleFileName function
Tips

If you want to use a more modern dialog box, please import the following package to use comctl6 (thanks to andlabs' winmanifest)

import _ "github.com/Tim-Paik/webview2/manifest"

Problem:

The ARM64 architecture is currently not supported, because I don't have an ARM64 device, and the Microsoft documentation does not mention the location of the Webview2 registry under the ARM64 architecture.

TODO
  • High DPI support
  • Clean title bar
  • Prompt to download WebView2 runtime
  • Non-debug mode
  • Bindings
go-webview2

This is a proof of concept for embedding Webview2 into Go without CGo. It is based on webview/webview and provides a compatible API sans some unimplemented functionality (notably, bindings are not implemented.)

Notice

Because this version doesn't currently have an EdgeHTML fallback, it will not work unless you have a Webview2 runtime installed. In addition, it requires the Webview2Loader DLL in order to function. Adding an EdgeHTML fallback should be technically possible but will likely require much worse hacks since the API is not strictly COM to my knowledge.

Demo

For now, you'll need to install the Webview2 runtime, as it does not ship with Windows.

WebView2 runtime

After that, you should be able to run go-webview2 directly:

go run github.com/jchv/go-webview2/cmd/demo

This will use go-winloader to load an embedded copy of WebView2Loader.dll.

If this does not work, please try running from a directory that has an appropriate copy of WebView2Loader.dll for your GOARCH. If that worked, please file a bug so we can figure out what's wrong with go-winloader :)

Documentation

Rendered for windows/amd64

Index

Constants

This section is empty.

Variables

View Source
var DisableWebSecurity = false
View Source
var UserAgent = ""
View Source
var UserDataFolder = filepath.Join(os.Getenv("AppData"),
	strings.TrimSuffix(filepath.Base(os.Args[0]), path.Ext(os.Args[0])))

Functions

func GetWebview2Runtime

func GetWebview2Runtime() error

Types

type ComProc

type ComProc uintptr

ComProc stores a COM procedure.

func NewComProc

func NewComProc(fn interface{}) ComProc

NewComProc creates a new COM proc from a Go function.

func (ComProc) Call

func (p ComProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error)

Call calls a COM procedure.

type Hint

type Hint int

Hint is used to configure window sizing and resizing behavior.

const (
	// HintNone specifies that width and height are default size
	HintNone Hint = iota

	// HintFixed specifies that window size can not be changed by a user
	HintFixed

	// HintMin specifies that width and height are minimum bounds
	HintMin

	// HintMax specifies that width and height are maximum bounds
	HintMax
)

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 data URI, i.e.
	// "data:text/text,<html>...</html>". It is often ok not to url-encode it
	// properly, webview will re-encode it for you.
	Navigate(url 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
}

WebView is the interface for the webview.

func New

func New(debug bool) WebView

New creates a new webview in a new window.

func NewWindow

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

NewWindow creates a new webview using an existing window.

Directories

Path Synopsis
cmd
Package manifest: Provide modern styles for the called Win32 dialog boxes, etc.
Package manifest: Provide modern styles for the called Win32 dialog boxes, etc.

Jump to

Keyboard shortcuts

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