webview

package module
v0.0.0-...-c9ce1bb Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2018 License: MIT Imports: 12 Imported by: 0

README

webview

Build Status Build status GoDoc Go Report Card

A tiny cross-platform webview library for C/C++/Golang to build modern cross-platform GUI.

It supports two-way JavaScript bindings (to call JavaScript from C/C++/Go and to call C/C++/Go from JavaScript).

It uses Cocoa/WebKit on macOS, gtk-webkit2 on Linux and MSHTML (IE10/11) on Windows.

linux

Webview for Go developers

If you are interested in writing Webview apps in C/C++ - skip to the next section.

The program is based on the original GitHub contribution code github.com/zserge/webview

Getting started

Install Webview library with go get:

$ go get github.com/NiuStar/webview

Import the package and start using it:

package main

import (
	"github.com/NiuStar/webview"
	"strings"
	"nqc.cn/fmt"
)

func main() {
	w = webview.New(webview.Settings{
		Width:  800,
		Height: 600,
		Title:  "Simple canvas demo",
		URL:    "https://pm.cninct.com/html/test.html",
		ExternalInvokeCallback: handleRPC,
		ExternalFinishLoadForFrameCallback:didFinishLoadForFrame,
	})
	defer w.Exit()
	//
	w.Run()
	// Open wikipedia in a 800x600 resizable window
}

func handleRPC(w webview.WebView, data string) {
	fmt.Println("data:",data)
}


func didFinishLoadForFrame(w webview.WebView) {

	fmt.Start()
	if w.IsLoading() {
		fmt.Println("didFinishLoadForFrame IsLoading")
	} else {

		fmt.Println("didFinishLoadForFrame  not IsLoading",w.LoadUrl())

		fmt.Println("Cookie:",w.GetCookie())

		cookie := w.GetCookie()

		w.ClearCache()

		list := strings.Split(cookie,"&")

		c := make(map[string]interface{})

		for _,value := range list {
			l := strings.Split(value,"=")
			if len(l) > 0 {
				c[l[0]] = l[1]
			}
		}
		if strings.EqualFold( c["name"].(string) ,"PHPSESSID") {

			PHPSESSID = c["value"].(string)
		}
		w.StringByEvaluatingJavaScriptFromString(`alert('这里是JS中alert弹出的message')`)
	}

	fmt.Over()
}

News

Adding three methods

1、StringByEvaluatingJavaScriptFromString

The ObjectC call javascript.

w.StringByEvaluatingJavaScriptFromString(`alert('这里是JS中alert弹出的message')`)

2、ClearCache

clear the cookies and cache

w.ClearCache()

3、GetCookie

		cookie := w.GetCookie()
		
		list := strings.Split(cookie,"&")

		c := make(map[string]interface{})

		for _,value := range list {
			l := strings.Split(value,"=")
			if len(l) > 0 {
				c[l[0]] = l[1]
			}
		}
		if strings.EqualFold( c["name"].(string) ,"PHPSESSID") {
			PHPSESSID = c["value"].(string)
		}

4、LoadUrl

5、IsLoading

6、callback method didFinishLoadForFrame

When the frame is loaded at the end of the call

7、Add alert

you can call javascript alert

Notes

Execution on OpenBSD requires wxallowed mount(8) option.

FreeBSD is also supported, to install webkit2 run pkg install webkit2-gtk3.

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

Documentation

Overview

Package webview implements Go bindings to https://github.com/zserge/webview C library.

Bindings closely repeat the C APIs and include both, a simplified single-function API to just open a full-screen webview window, and a more advanced and featureful set of APIs, including Go-to-JavaScript bindings.

The library uses gtk-webkit, Cocoa/Webkit and MSHTML (IE8..11) as a browser engine and supports Linux, MacOS and Windows 7..10 respectively.

Index

Constants

View Source
const (
	// DialogFlagFile is a normal file picker dialog
	DialogFlagFile = C.WEBVIEW_DIALOG_FLAG_FILE
	// DialogFlagDirectory is an open directory dialog
	DialogFlagDirectory = C.WEBVIEW_DIALOG_FLAG_DIRECTORY
	// DialogFlagInfo is an info alert dialog
	DialogFlagInfo = C.WEBVIEW_DIALOG_FLAG_INFO
	// DialogFlagWarning is a warning alert dialog
	DialogFlagWarning = C.WEBVIEW_DIALOG_FLAG_WARNING
	// DialogFlagError is an error dialog
	DialogFlagError = C.WEBVIEW_DIALOG_FLAG_ERROR
)

Variables

This section is empty.

Functions

func Debug

func Debug(a ...interface{})

Debug prints a debug string using stderr on Linux/BSD, NSLog on MacOS and OutputDebugString on Windows.

func Debugf

func Debugf(format string, a ...interface{})

Debugf prints a formatted debug string using stderr on Linux/BSD, NSLog on MacOS and OutputDebugString on Windows.

func Open

func Open(title, url string, w, h int, resizable bool) error

Open is a simplified API to open a single native window with a full-size webview in it. It can be helpful if you want to communicate with the core app using XHR or WebSockets (as opposed to using JavaScript bindings).

Window appearance can be customized using title, width, height and resizable parameters. URL must be provided and can user either a http or https protocol, or be a local file:// URL. On some platforms "data:" URLs are also supported (Linux/MacOS).

Types

type DialogType

type DialogType int

DialogType is an enumeration of all supported system dialog types

const (
	// DialogTypeOpen is a system file open dialog
	DialogTypeOpen DialogType = iota
	// DialogTypeSave is a system file save dialog
	DialogTypeSave
	// DialogTypeAlert is a system alert dialog (message box)
	DialogTypeAlert
)

type ExternalDidFinishLoadForFrameCallbackFunc

type ExternalDidFinishLoadForFrameCallbackFunc func(w WebView)

type ExternalInvokeCallbackFunc

type ExternalInvokeCallbackFunc func(w WebView, data string)

ExternalInvokeCallbackFunc is a function type that is called every time "window.external.invoke()" is called from JavaScript. Data is the only obligatory string parameter passed into the "invoke(data)" function from JavaScript. To pass more complex data serialized JSON or base64 encoded string can be used.

type Settings

type Settings struct {
	// WebView main window title
	Title string
	// URL to open in a webview
	URL string
	// Window width in pixels
	Width int
	// Window height in pixels
	Height int
	// Allows/disallows window resizing
	Resizable bool
	// Enable debugging tools (Linux/BSD/MacOS, on Windows use Firebug)
	Debug bool
	// A callback that is executed when JavaScript calls "window.external.invoke()"
	ExternalInvokeCallback             ExternalInvokeCallbackFunc
	ExternalFinishLoadForFrameCallback ExternalDidFinishLoadForFrameCallbackFunc
}

Settings is a set of parameters to customize the initial WebView appearance and behavior. It is passed into the webview.New() constructor.

type WebView

type WebView interface {
	// Run() starts the main UI loop until the user closes the webview window or
	// Terminate() is called.
	Run()
	// Loop() runs a single iteration of the main UI.
	Loop(blocking bool) bool
	// SetTitle() changes window title. This method must be called from the main
	// thread only. See Dispatch() for more details.
	SetTitle(title string)
	// SetFullscreen() controls window full-screen mode. This method must be
	// called from the main thread only. See Dispatch() for more details.
	SetFullscreen(fullscreen bool)
	// Eval() evaluates an arbitrary JS code inside the webview. This method must
	// be called from the main thread only. See Dispatch() for more details.
	Eval(js string)

	StringByEvaluatingJavaScriptFromString(js string)
	// InjectJS() injects an arbitrary block of CSS code using the JS API. This
	// method must be called from the main thread only. See Dispatch() for more
	// details.
	InjectCSS(css string)
	// Dialog() opens a system dialog of the given type and title. String
	// argument can be provided for certain dialogs, such as alert boxes. For
	// alert boxes argument is a message inside the dialog box.
	Dialog(dlgType DialogType, flags int, title string, arg string) string
	// Terminate() breaks the main UI loop. This method must be called from the main thread
	// only. See Dispatch() for more details.
	Terminate()
	// Dispatch() schedules some arbitrary function to be executed on the main UI
	// thread. This may be helpful if you want to run some JavaScript from
	// background threads/goroutines, or to terminate the app.
	Dispatch(func())
	// Exit() closes the window and cleans up the resources. Use Terminate() to
	// forcefully break out of the main UI loop.
	Exit()
	ClearCache()

	GetCookie() string
	LoadUrl() string
	IsLoading() bool
	// Bind() registers a binding between a given value and a JavaScript object with the
	// given name.  A value must be a struct or a struct pointer. All methods are
	// available under their camel-case names, starting with a lower-case letter,
	// e.g. "FooBar" becomes "fooBar" in JavaScript.
	// Bind() returns a function that updates JavaScript object with the current
	// Go value. You only need to call it if you change Go value asynchronously.
	Bind(name string, v interface{}) (sync func(), err error)
}

WebView is an interface that wraps the basic methods for controlling the UI loop, handling multithreading and providing JavaScript bindings.

func New

func New(settings Settings) WebView

New creates and opens a new webview window using the given settings. The returned object implements the WebView interface. This function returns nil if a window can not be created.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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