Giter Club home page Giter Club logo

go-webkit2's Introduction

go-webkit2

go-webkit2 provides Go bindings for the WebKitGTK+ 2 API. It permits headless operation of WebKit as well as embedding a WebView in a GTK+ application.

status xrefs funcs top func library users

Requirements

Ubuntu 13.10 (Saucy)

sudo add-apt-repository ppa:gnome3-team/gnome3-staging
sudo apt-get update
sudo apt-get install libwebkit2gtk-3.0-dev

Ubuntu 13.04 (Raring)

sudo add-apt-repository ppa:gnome3-team/gnome3
sudo apt-get update
sudo apt-get install libwebkit2gtk-3.0-dev

Arch Linux

sudo pacman -S webkitgtk

Other platforms

Make sure you install WebKitGTK+ 2, not version 1. After installation, you should have an include file that satisfies #include <webkit2/webkit2.h>.

Usage

As a Go package

package webkit2_test

import (
	"fmt"
	"github.com/sourcegraph/go-webkit2/webkit2"
	"github.com/sqs/gojs"
	"github.com/sqs/gotk3/glib"
	"github.com/sqs/gotk3/gtk"
	"runtime"
)

func Example() {
	runtime.LockOSThread()
	gtk.Init(nil)

	webView := webkit2.NewWebView()
	defer webView.Destroy()

	webView.Connect("load-failed", func() {
		fmt.Println("Load failed.")
	})
	webView.Connect("load-changed", func(ctx *glib.CallbackContext) {
		loadEvent := webkit2.LoadEvent(ctx.Arg(0).Int())
		switch loadEvent {
		case webkit2.LoadFinished:
			fmt.Println("Load finished.")
			fmt.Printf("Title: %q\n", webView.Title())
			fmt.Printf("URI: %s\n", webView.URI())
			webView.RunJavaScript("window.location.hostname", func(val *gojs.Value, err error) {
				if err != nil {
					fmt.Println("JavaScript error.")
				} else {
					fmt.Printf("Hostname (from JavaScript): %q\n", val)
				}
				gtk.MainQuit()
			})
		}
	})

	glib.IdleAdd(func() bool {
		webView.LoadURI("https://www.google.com/")
		return false
	})

	gtk.Main()

	// output:
	// Load finished.
	// Title: "Google"
	// URI: https://www.google.com/
	// Hostname (from JavaScript): "www.google.com"
}

See the documentation and the test files for usage information and examples.

For more information about the underlying WebKitGTK+ 2 API, refer to the WebKit2 docs.

As a program for evaluating JavaScript in the context of a web page

The included webkit-eval-js program runs the contents of a JavaScript file in the context of a web page. Run with:

$ go install github.com/sourcegraph/go-webkit2/...
$ webkit-eval-js https://example.com scriptfile.js

For example:

$ echo document.title | webkit-eval-js https://google.com /dev/stdin
"Google"

Used in

The following projects use go-webkit2:

  • WebLoop - headless WebKit with a Go API

See go-webkit2 users for a full list of repositories and people using go-webkit2.

Running tests

go test ./webkit2

Note: The tests require an X display. If you are not running in a graphical environment, you can use Xvfb:

Xvfb :1 &
export DISPLAY=:1
go test ./webkit2

TODO

  • Implement more of the WebKitGTK+ 2 API. Right now, only certain parts of it are implemented.
  • Set up CI testing. This is difficult because all of the popular CI services run older versions of Ubuntu that make it difficult to install WebKitGTK+ >= 2.0.0.
  • Create example applications.
  • Fix memory leaks where C strings are allocated and not freed.

Contributors

See the AUTHORS file for a list of contributors.

Submit contributions via GitHub pull request. Patches should include tests and should pass golint.

go-webkit2's People

Contributors

bryansum avatar sqs avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.