Code Monkey home page Code Monkey logo

webloop's Introduction

WebLoop

Scriptable, headless WebKit with a Go API. Like PhantomJS, but for Go. Render static HTML versions of dynamic JavaScript applications, automate browsing, run arbitrary JavaScript in a browser window context, etc., all from Go or the command line.

status

Screenshot of a dynamic AngularJS application running side-by-side with a statically rendered HTML equivalent, using WebLoop

Requirements

For instructions on installing these dependencies, see the go-webkit2 README.

To install WebLoop, run: go get github.com/sourcegraph/webloop/...

Usage

Static HTML rendering reverse proxy

The included command static-reverse-proxy proxies a dynamic JavaScript application and serves an equivalent statically rendered HTML website to clients. Run it with:

$ go install github.com/sourcegraph/webloop/...
$ static-reverse-proxy

For example, to proxy a dynamic application at http://example.com and serve an equivalent statically rendered HTML website on http://localhost:13000, run:

$ static-reverse-proxy -target=http://example.com -http=:13000

Run with -h to see more information.

Rendering static HTML from a dynamic, single-page AngularJS app

StaticRenderer is an HTTP handler that serves a static HTML version of a dynamic web application. Use it like:

staticHandler := &webloop.StaticRenderer{
        TargetBaseURL:         "http://dynamic-app.example.com",
        WaitTimeout:           time.Second * 3,
        ReturnUnfinishedPages: true
}
http.Handle("/", staticHandler)

See the examples/angular-static-seo/ directory for example code. Run the included binary with:

$ go run examples/angular-static-seo/server.go

Instructions will be printed for accessing the 2 local demo HTTP servers. Run with -h to see more information.

Operating a headless WebKit and running arbitrary JavaScript in the page

package webloop_test

import (
	"fmt"
	"os"
	"runtime"

	"github.com/gotk3/gotk3/gtk"
	"github.com/sourcegraph/webloop"
)

func Example() {
	gtk.Init(nil)
	go func() {
		runtime.LockOSThread()
		gtk.Main()
	}()

	ctx := webloop.New()
	view := ctx.NewView()
	defer view.Close()
	view.Open("http://google.com")
	err := view.Wait()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load URL: %s", err)
		os.Exit(1)
	}
	res, err := view.EvaluateJavaScript("document.title")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to run JavaScript: %s", err)
		os.Exit(1)
	}
	fmt.Printf("JavaScript returned: %q\n", res)
	// output:
	// JavaScript returned: "Google"
}

See webloop_test.go for more examples.

TODO

Users

  • WebLoop is used to render static HTML pages on Sourcegraph for search engine crawlers

Contributors

See the AUTHORS file for a list of contributors.

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

webloop's People

Contributors

attfarhan avatar beyang avatar dictav avatar elgohr avatar sqs avatar zachlatta avatar

Watchers

 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.