render

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

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

Go to latest
Published: Feb 28, 2020 License: MIT Imports: 15 Imported by: 0

README

go-render

CircleCI

Docker Repository on Quay

Go Report Card

Simple WebKit2 based headless HTML/SVG rasterizer.

Docker

Building and running a headless Webkit is typically not trivial. You need a pretty extensive build and runtime environment along with services like a virtual frame buffer. In addition to wrapping this functionality as a go library, this repository includes a sane command line binary that gives you access to a few simple commands. This binary is packages as a public Docker container and gives you access to headless rendering on the Docker compatible platform of your choice.

Metadata

Get basic page and timing information.

$ docker run quay.io/redsift/go-render metadata www.google.com
{
    "Title": "Google",
    "URI": "https://www.google.co.uk/?gfe_rd=cr\u0026ei=EGxRV9_DMdTG8AefmYeoCw\u0026gws_rd=ssl",
    "Timing": {
        "Start": 0.0008351950000000001,
        "Load": 0.346783985,
        "Finish": 0.34761918
    }
}

Screenshot

Get a PNG of the page.

$ docker run quay.io/redsift/go-render snapshot www.yahoo.com -f png > yahoo.png

Javascript

Execute some javascript in the context of a loaded page. e.g. Page DOM validation tests.

$ docker run quay.io/redsift/go-render javascript --js window.location.hostname www.bbc.co.uk

You may also supply a .js file to execute as the parameter.

Additional Examples

Metadata
# Local filesystem rendering
$ ... metadata file:///opt/gopath/src/github.com/redsift/go-render/test/local.html
Format

--format allows extraction of selected fields from the JSON file. e.g. --format="{{.Title}}" to just grab the title or -f "{{json .Timing}}" to get the timing information.

Javascript
$ ... javascript -j "\"js=\" + window.location.hostname"  www.google.com
Local .js file
$ cat ./test.js
function t() {
    return { Hostname: window.location.hostname, Pathname: window.location.pathname };
}
t();

# Execute a local file, exceptions will set a non-zero exit code
$ docker run -v test.js:/tmp/host quay.io/redsift/go-render \ 
  	javascript --js /tmp/host/test.js www.google.com

# Extract a field from the JSON object
$ ... javascript -j ./test.js -f {{.Hostname}} www.google.com
Snapshot
# Capture a png to stdout
$ ... snapshot www.google.com

# Capture a webp as a file
$ ... snapshot -o google.webp www.google.com

# Capture multiple URLS and construct filenames from the hostname
$ render snapshot -o grab-{{.Host}}.png www.google.com www.yahoo.com

Using Stdin

In addition to passing the URLs on the command line, you may omit the argument and go-render will read from the command line.

# From stdin where `urls.txt` is a simple list of URLs separated by a newline.
$ cat urls.txt | ... snapshot -o list-{{.Index}}.png

How does it work?

This library uses a version of go-webkit2. The docker image bundles the Xvfb virtual frame-buffer. The runtime image is stripped down using syscall tracing to reduce the size requirements for the binary by an order of magnitude.

Developers

Xvfb

xdpyinfo can provide stats on the virtual frame buffer.

Tracing

    sudo apt-get install strace
    
    sudo strace -f -o $CIRCLE_ARTIFACTS/bash.strace.out -p 49350

Know issues

  • Due to timing issues with the cleanup of the process tree, WebKit might occasionally emit errors as it shuts down. e.g. (WebKitWebProcess:26): Gdk-WARNING **: WebKitWebProcess: Fatal IO error 11 (Resource temporarily unavailable) on X server :1.
  • Some URLs may not successfully snapshot. e.g. docker run quay.io/redsift/go-render snapshot www.shazam.com -> render: error: Unable to create snapshot: There was an error creating the snapshot. This is usually due to a 0x0 pixel display at the time of invoking the snapshot.

Documentation

Overview

Package render implements a simple wrapper around Webkit that primarily allows for the capture of screenshots.

This package will typically only compile on a modern Linux distribution and requires a running X server. A utility binary is also provided.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLoadFailed signifies that Webkit reports failure of of the page load
	ErrLoadFailed = errors.New("load-failed")
	// ErrViewClosed signifies that the View has already been closed, not further operations allowed
	ErrViewClosed = errors.New("view-closed")
	// ErrNoImage signifies that snapshot did not return a usable image
	ErrNoImage = errors.New("no-image")
	// ErrNoTiming signifies that no timing information is available
	ErrNoTiming = errors.New("load-not-timed")
	// ErrNoX signifies that we did not find a usable X display server
	ErrNoX = errors.New("no-x-display")
)

Functions

This section is empty.

Types

type Renderer

type Renderer struct {
	sync.Mutex
}

func NewRenderer

func NewRenderer() (*Renderer, error)

NewRenderer creates a new GTK based rendering context

func (*Renderer) NewView

func (r *Renderer) NewView(appName, appVersion string, autoLoadImages, consoleStdout bool, languages []string) *View

NewView creates a new Webkit view

type View

type View struct {
	*webkit2.WebView
	// contains filtered or unexported fields
}

func (*View) Close

func (v *View) Close()

func (*View) EvaluateJavaScript

func (v *View) EvaluateJavaScript(ctx context.Context, script string) (result interface{}, err error)

func (*View) LoadHTML

func (v *View) LoadHTML(content, baseURI string) error

func (*View) LoadURI

func (v *View) LoadURI(url string) error

func (*View) NewSnapshot

func (v *View) NewSnapshot(ctx context.Context) (result *image.RGBA, err error)

func (*View) TimeToFinish

func (v *View) TimeToFinish() (time.Duration, error)

func (*View) TimeToLoad

func (v *View) TimeToLoad() (time.Duration, error)

func (*View) TimeToStart

func (v *View) TimeToStart() (time.Duration, error)

func (*View) Wait

func (v *View) Wait(ctx context.Context) error

Wait for the current page to finish loading.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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