• This repository has been archived on 07/Aug/2022
  • Stars
    star
    294
  • Rank 136,029 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go client for PhantomJS.

deprecation warning

active phantomjs development has ended, in favor of using Chrome's new headless functionality (reference). Instead of using this library, consider using a go package that uses this new api such as chromedp.

phantomjs godoc Status

This is a Go wrapper for the phantomjs command line program. It provides the full webpage API and has a strongly typed API. The wrapper provides an idiomatic Go interface while allowing you to communicate with the underlying WebKit and JavaScript engine in a seamless way.

Installing

First, install phantomjs on your machine. This can be done using your package manager (such as apt-get or brew). Then install this package using the Go toolchain:

$ go get -u github.com/benbjohnson/phantomjs

Usage

Starting the process

This wrapper works by communicating with a separate phantomjs process over HTTP. The process can take several seconds to start up and shut down so you should do that once and then share the process. There is a package-level variable called phantomjs.DefaultProcess that exists for this purpose.

package main

import (
	"github.com/benbjohnson/phantomjs"
)

func main() {
	// Start the process once.
	if err := phantomjs.DefaultProcess.Open(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer phantomjs.DefaultProcess.Close()

	// Do other stuff in your program.
	doStuff()
}

You can have multiple processes, however, you will need to change the port used for each one so they do not conflict. This library uses port 20202 by default.

Working with WebPage

The WebPage will be the primary object you work with in phantomjs. Typically you will create a web page from a Process and then either open a URL or you can set the content directly:

// Create a web page.
// IMPORTANT: Always make sure you close your pages!
page, err := p.CreateWebPage()
if err != nil {
	return err
}
defer page.Close()

// Open a URL.
if err := page.Open("https://google.com"); err != nil {
	return err
}

The HTTP API uses a reference map to track references between the Go library and the phantomjs process. Because of this, it is important to always Close() your web pages or else you can experience memory leaks.

Executing JavaScript

You can synchronously execute JavaScript within the context of a web page by by using the Evaluate() function. This example below opens Hacker News, retrieves the text and URL from the first link, and prints it to the terminal.

// Open a URL.
if err := page.Open("https://news.ycombinator.com"); err != nil {
	return err
}

// Read first link.
info, err := page.Evaluate(`function() {
	var link = document.body.querySelector('.itemlist .title a');
	return { title: link.innerText, url: link.href };
}`)
if err != nil {
	return err
}

// Print title and URL.
link := info.(map[string]interface{})
fmt.Println("Hacker News Top Link:")
fmt.Println(link["title"])
fmt.Println(link["url"])
fmt.Println()

You can pass back any object from Evaluate() that can be marshaled over JSON.

Rendering web pages

Another common task with PhantomJS is to render a web page to an image. Once you have opened your web page, simply set the viewport size and call the Render() method:

// Open a URL.
if err := page.Open("https://news.ycombinator.com"); err != nil {
	return err
}

// Setup the viewport and render the results view.
if err := page.SetViewportSize(1024, 800); err != nil {
	return err
}
if err := page.Render("hackernews.png", "png", 100); err != nil {
	return err
}

You can also use the RenderBase64() to return a base64 encoded image to your program instead of writing the file to disk.

More Repositories

1

litestream

Streaming replication for SQLite.
Go
9,990
star
2

thesecretlivesofdata

Understanding what your bits do when you're not looking.
JavaScript
3,338
star
3

wtf

WTF Dial is an example web application written in Go.
Go
1,533
star
4

postlite

Postgres wire compatible SQLite proxy.
Go
1,202
star
5

immutable

Immutable collections for Go
Go
686
star
6

clock

Clock is a small library for mocking time in Go.
Go
665
star
7

ego

An ERB-style templating language for Go.
Go
578
star
8

testing

A small collection of functions for Go testing.
Go
525
star
9

megajson

A JSON parser generator for high performance encoding and decoding in Go.
Go
468
star
10

hashfs

Implementation of io/fs.FS that appends SHA256 hashes to filenames to allow for aggressive HTTP caching.
Go
342
star
11

sql-parser

Toy SQL parser example for Gopher Academy
Go
324
star
12

genesis

A simple tool for embedding assets in a Go binary.
Go
300
star
13

jmphash

Implementation of the Jump Consistent Hash algorithm in Go.
Go
154
star
14

scuttlebutt

A daemon for tracking and tweeting trending Github repositories by language.
Go
150
star
15

llvm-c-kaleidoscope

An implementation of the Kaleidoscope language using Flex, Bison & the LLVM-C bindings.
C
129
star
16

playback.js

A library for dynamic timeline playback.
JavaScript
117
star
17

litestream-docker-example

An example of using Litestream within a Docker container.
Go
88
star
18

css

W3C-compliant CSS3 parser and scanner
Go
84
star
19

litestream-s6-example

Example repository for building a multi-process Docker container.
Dockerfile
83
star
20

tmpl

Command line interface to Go's text/template library.
Go
81
star
21

grapevine

Trending topics for stuff you care about
Ruby
80
star
22

slowweb

An HTTP request governor
Ruby
75
star
23

litestream-read-replica-demo

A demo application for running live read replication on fly.io with Litestream
Go
69
star
24

ghfs

FUSE Filesystem for the GitHub API
Go
61
star
25

agency

A fast user agent string parser for Go.
Go
60
star
26

litestream-library-example

Example repository for embedding Litestream in a Go application.
Go
52
star
27

litestream-read-replica-example

An example of using Litestream's live read replication feature.
Go
52
star
28

melomel

External ActionScript Interface.
ActionScript
42
star
29

litestream.io

SCSS
40
star
30

pprofdump

A simple utility for collecting net/http/pprof profiles.
Go
28
star
31

peapod

A personal podcast service.
Go
26
star
32

application-development-using-boltdb

Repository for my "Application Development Using BoltDB" talk
Go
26
star
33

sieve

A command line utility for graphing piped data.
Go
23
star
34

production-sqlite-go

Companion repository for GopherCon presentation on "Production Applications Using SQLite & Go"
Go
23
star
35

goo

Thin wrapper for the Go toolchain.
Go
20
star
36

burger-stack

Presentation for "The Burger Stack"
19
star
37

structuring-applications-for-growth

GopherCon 2016 presentation for "Structuring Applications for Growth"
17
star
38

stack

Go debug/stack utility functions.
Go
17
star
39

myapp

An simple application with an HTTP server & SQLite database.
Go
14
star
40

glee

Incomplete Go port of the KLEE SymEx system.
Go
14
star
41

melomel.rb

An external interface to Flash from Ruby.
Ruby
13
star
42

raft.js

An experimental Raft implementation in Javascript.
13
star
43

vex

Variable-length, lexicographically-sortable hex format for uint64 values.
Go
13
star
44

gha

SQLite load testing application using GitHub Archive data.
Go
13
star
45

writing-a-distributed-systems-library

Companion code for the Gopher Academy blog post.
Go
11
star
46

tiny-ego

A toy application showcasing ego templates & components.
Go
10
star
47

boxer

A little app that boxes my time.
Go
10
star
48

bootstrap-ego

An ego template component library for Bootstrap 4.
Go
10
star
49

seppuku

To be executed upon implementation of generics in Go.
Go
10
star
50

roommate

A conference room scheduling application.
Go
9
star
51

syncutil

A collection of utility functions for Go synchronization.
Go
9
star
52

gist

Gist hosting and embedding.
Go
8
star
53

melomel-examples

Examples of using Melomel.
Ruby
8
star
54

describe.today

A web site to describe today.
JavaScript
7
star
55

minipack

A lightweight C MessagePack parser.
C
7
star
56

skybox

An open source funnel analysis application.
Go
7
star
57

go-raft-runner

A test runner application for the go-raft library.
Go
6
star
58

http-wiretap

It's like Charles Proxy for your Rubies!
Ruby
5
star
59

mincore

Example usage for using mincore() in Go.
Go
5
star
60

miniviz

A simplified interface to GraphViz for laying out clusters, nodes and edges.
Ruby
5
star
61

graphviz-as3

An interface to the graphviz CLI using Adobe AIR.
ActionScript
5
star
62

edb

A simple database for tracking events.
Go
5
star
63

rationl

Online journal for tracking experiments.
Go
4
star
64

sqlite-bench

Miscellaneous Go/SQLite benchmarks
Go
4
star
65

hackerbeeper

A dumb utility for playing generated notes when you type.
Go
4
star
66

opus

Command line utility for printing columnized code.
4
star
67

mockdown.as

A Markdown-inspired Mockup Language
ActionScript
4
star
68

serialkiller

ActionScript JSON & XML serialization library
ActionScript
4
star
69

whodump

A command line interface for checking domain name availability.
Ruby
4
star
70

tsld.js

Core library for "The Secret Lives of Data" project.
JavaScript
3
star
71

bandicoot

A crash reporter library for C applications.
C
3
star
72

skydb.io

The Official Sky Web Site
CSS
2
star
73

cine

A movie search application.
Go
2
star
74

chatter

Demo application using Server Side Events (SSE) and written in Go.
Go
2
star
75

authoritarian

Command line utility for authorizing Twitter users to an application.
2
star
76

matlock

Simple name extraction utility.
Ruby
2
star
77

constdump

Utility for printing a list of package-level constants.
Go
2
star
78

d3.jquery.js

A collection of D3.js charts made available as jQuery plugins.
JavaScript
1
star
79

mockdown.rb

Mockups for hackers
Ruby
1
star
80

landmarkd

The Landmark Tracking Server.
Go
1
star
81

timeshifter

A Ruby library for shifting time.
Ruby
1
star
82

httpng

A local server for saving HTML elements as PNG files.
JavaScript
1
star
83

ldbchk

Runs concurrency tests against LevelDB & Levigo.
Go
1
star
84

whollydeliciousfoods.com

The home page for Wholly Delicious Foods.
JavaScript
1
star
85

homebrew-litestream

Homebrew tap for litestream.
Ruby
1
star
86

bench-c

A small collection of benchmarks for the C programming language.
C
1
star
87

gitcoin

Turing gitcoin repository
Go
1
star
88

unistat

A utility for calculating simple statistics on unicode characters.
Go
1
star
89

termgraf

Terminal chronograf
Go
1
star
90

locald

A simple HTTP server for serving static files out of the current directory.
1
star
91

skylandlabs.github.com

Skyland Labs Blog
JavaScript
1
star
92

fql-editor

An editor for FQL queries.
ActionScript
1
star
93

tip.litestream.io

Mirror of litestream.io repository for latest changes.
HTML
1
star
94

fslice

A small utility for extracting delimited sections of a file.
Go
1
star
95

ego-example

A simple ego templating example.
Go
1
star
96

prog

Go testing
Go
1
star