Webview

适用于 C/C++/Golang 的微型跨平台 Webview 库。 使用 WebKit(Gtk/Cocoa)和 Edge(Windows)。「Tiny cross-platform webview library for C/C++/Golang. Uses WebKit (Gtk/Cocoa) and MSHTML (Windows).」

Github stars Tracking Chart

webview

一个用于 C/C++/Golang 的微型跨平台 Webview 库,用于构建现代的跨平台 GUI。 另外,还有 Rust bindings, Python bindings, Nim bindings, Haskell, C# bindingsJava bindings

该项目的目标是为最广泛使用的平台创建一个通用的HTML5 UI抽象层。

它支持双向 JavaScript 绑定(从 C/C++/Go 调用 JavaScrip t和从 JavaScript 调用 C/C++/Go)。

它在 macOS 上使用 Cocoa/WebKit,在 Linux 上使用 gtk-webkit2,在 Windows 10 上使用 Edge。

面向 Go 开发者的 Webview

如果你对用 C/C++ 编写 Webview 应用程序感兴趣,请跳到下一节。

入门

用 go get 安装 Webview 库:

$ go get github.com/zserge/webview

导入包并开始使用它:

package main
import "github.com/zserge/webview"
func main() {
    debug := true
    w := webview.New(debug)
    defer w.Destroy()
    w.SetTitle("Minimal webview example")
    w.SetSize(800, 600, webview.HintNone)
    w.Navigate("https://en.m.wikipedia.org/wiki/Main_Page")
    w.Run()
}

要构建应用程序,请使用以下命令:

# Linux
$ go build -o webview-example && ./webview-example
# MacOS uses app bundles for GUI apps
$ mkdir -p example.app/Contents/MacOS
$ go build -o example.app/Contents/MacOS/example
$ open example.app # Or click on the app in Finder
# Windows requires special linker flags for GUI apps.
# It's also recommended to use TDM-GCC-64 compiler for CGo.
# http://tdm-gcc.tdragon.net/download
$ go build -ldflags="-H windowsgui" -o webview-example.exe

有关详细信息,请参阅:godoc.

分发 Webview 应用

在 Linux 上,你有一个独立的可执行文件。它将依赖于 GTK3 和 GtkWebkit2,所以如果你以 DEB 或 RPM 格式发布你的应用,则应包括这些依赖项。可以通过提供 .desktop 文件来指定应用程序图标

在 MacOS 上,你可能会发布一个应用程序包。创建以下目录结构并压缩它:

example.app
└── Contents
    ├── Info.plist
    ├── MacOS
    |   └── example
    └── Resources
        └── example.icns

在这里,Info.plist 是一个属性列表文件 而 * .icns 是一种特殊的图标格式。 您可以 在线 将PNG转换为icns。

在 Windows 上,您可能希望为可执行文件有一个自定义图标。 可以通过提供资源文件,对其进行编译并与其链接来完成。 通常,windres 实用程序用于编译资源。 此外,在 Windows 上,必须将 webview.dll 和 WebView2Loader.dll 与您的应用可执行文件放置在同一目录中。

另外,如果要交叉编译Webview应用程序,请使用 xgo

面向 C/C++ 开发人员的 Webview

下载 webview.h 并将其包含在您的 C/C++ 代码中:

// main.c
#include "webview.h"
#ifdef WIN32
int WINAPI WinMain(HINSTANCE hInt, HINSTANCE hPrevInst, LPSTR lpCmdLine,
                   int nCmdShow) {
#else
int main() {
#endif
  webview::webview w(true, nullptr);
  w.set_title("Minimal example");
  w.set_size(480, 320, WEBVIEW_HINT_NONE);
    w.navigate("https://en.m.wikipedia.org/wiki/Main_Page");
    w.run();
  return 0;
}

构建它:

# Linux
$ c++ main.cc `pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0` -o webview-example
# MacOS
$ c++ main.cc -framework WebKit -o webview-example
# Windows (x64)
$ c++ main.cc -mwindows -L./dll/x64 -lwebview -lWebView2Loader -o webview-example.exe

在 Windows 上,使用 cl.exe 编译时可以直接使用 webview 库,但是仍然需要 WebView2Loader.dll。 要使用 MinGW,您可以动态链接预建的 webview.dll(在Cgo 绑定中使用此方法)。

完整的 C/C++ API 在 webview.h 文件的顶部进行了描述。

说明

在 OpenBSD 上执行需要 wxallowed mount(8) 选项。对于 Ubuntu 用户,请运行 sudo apt install webkit2gtk-4.0 来安装 webkit2gtk-4.0 相关项目。 还支持 FreeBSD,要安装 webkit2,请运行 pkg install webkit2-gtk3。

License许可

代码是根据 MIT 许可分发的,也可以在您的专有项目中随意使用它。

Main metrics

Overview
Name With Ownerwebview/webview
Primary LanguageC++
Program languageC (Language Count: 5)
PlatformLinux, Mac, Windows
License:MIT License
所有者活动
Created At2017-08-19 08:26:00
Pushed At2025-04-02 12:10:01
Last Commit At2025-04-02 21:10:00
Release Count5
Last Release Name0.12.0 (Posted on 2024-09-12 01:22:24)
First Release Name0.1.0 (Posted on )
用户参与
Stargazers Count13k
Watchers Count220
Fork Count1k
Commits Count624
Has Issues Enabled
Issues Count784
Issue Open Count168
Pull Requests Count335
Pull Requests Open Count19
Pull Requests Close Count96
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

webview

Join the chat at https://gitter.im/zserge/webview
Build Status
Build status
GoDoc
Go Report Card

A tiny cross-platform webview library for C/C++/Golang to build modern cross-platform GUIs. Also, there are Rust bindings, Python bindings, Nim bindings, Haskell, C# bindings and Java bindings available.

IMPORTANT NOTE: Webview is now being rewritten from scratch, with the support of EdgeHTML and using C++14 as a primary language (code becomes much shorter/cleaner, and we still have C and Go APIs as the primary interface). Please have a look at webview-x branch before opening new issues. Current version of webview is still maintained, PRs with bugfixes are welcome, but new functionality will be added to the new branch. I expect to finish the new branch before March 2020, but no hard deadlines.

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.

Webview for Go developers

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

Getting started

Install Webview library with go get:

$ go get github.com/zserge/webview

Import the package and start using it:

package main

import "github.com/zserge/webview"

func main() {
	// Open wikipedia in a 800x600 resizable window
	webview.Open("Minimal webview example",
		"https://en.m.wikipedia.org/wiki/Main_Page", 800, 600, true)
}

It is not recommended to use go run (although it works perfectly fine on Linux). Use go build instead:

# Linux
$ go build -o webview-example && ./webview-example

# MacOS uses app bundles for GUI apps
$ mkdir -p example.app/Contents/MacOS
$ go build -o example.app/Contents/MacOS/example
$ open example.app # Or click on the app in Finder

# Windows requires special linker flags for GUI apps.
# It's also recommended to use TDM-GCC-64 compiler for CGo.
# http://tdm-gcc.tdragon.net/download
$ go build -ldflags="-H windowsgui" -o webview-example.exe

API

See godoc.

How to serve or inject the initial HTML/CSS/JavaScript into the webview?

First of all, you probably want to embed your assets (HTML/CSS/JavaScript) into the binary to have a standalone executable. Consider using go-bindata or any other similar tools.

Now there are two major approaches to deploy the content:

  • Serve HTML/CSS/JS with an embedded HTTP server
  • Injecting HTML/CSS/JS via the JavaScript binding API

To serve the content it is recommended to use ephemeral ports:

ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
	log.Fatal(err)
}
defer ln.Close()
go func() {
 	// Set up your http server here
	log.Fatal(http.Serve(ln, nil))
}()
webview.Open("Hello", "http://"+ln.Addr().String(), 400, 300, false)

Injecting the content via JS bindings is a bit more complicated, but feels more solid and does not expose any additional open TCP ports.

Leave webview.Settings.URL empty to start with bare minimal HTML5. It will open a webview with <div id="app"></div> in it. Alternatively, use a data URI to inject custom HTML code (don't forget to URL-encode it):

const myHTML = `<!doctype html><html>....</html>`
w := webview.New(webview.Settings{
	URL: `data:text/html,` + url.PathEscape(myHTML),
})

Keep your initial HTML short (a few kilobytes maximum).

Now you can inject more JavaScript once the webview becomes ready using webview.Eval(). You can also inject CSS styles using JavaScript:

w.Dispatch(func() {
	// Inject CSS
	w.Eval(fmt.Sprintf(`(function(css){
		var style = document.createElement('style');
		var head = document.head