helper

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2020 License: MIT Imports: 18 Imported by: 0

README

chromedp-helper

chromedp helper utilities.

go get -u github.com/go-oss/chromedp-helper

Documentation

Overview

Example (Navigate)
package main

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"time"

	"github.com/chromedp/cdproto/network"
	"github.com/chromedp/chromedp"
	helper "github.com/go-oss/chromedp-helper"
)

func main() {
	ctx, cancel := chromedp.NewContext(context.Background())
	defer cancel()

	ts := httptest.NewServer(exampleHandler())
	defer ts.Close()

	var next string
	var title string
	tasks := chromedp.Tasks{
		// It is necessary in the helper methods.
		network.Enable(),
		helper.EnableLifeCycleEvents(),

		helper.Navigate(ts.URL, time.Minute),
		chromedp.AttributeValue("//a[contains(., 'Link')]", "href", &next, nil),
		helper.Navigate(helper.URL(ts.URL, "%s", &next), time.Minute),
		chromedp.Title(&title),
	}
	err := chromedp.Run(ctx, tasks)
	if err != nil {
		panic(err)
	}
	fmt.Println(title)
}

func exampleHandler() http.Handler {
	index := strings.TrimSpace(`
<DOCTYPE html>
<html>
	<head>
		<title>Index</title>
	</head>
	<body>
		<a href="/next">Link</a>
	</body>
</html>`)
	next := strings.TrimSpace(`
<DOCTYPE html>
<html>
<head>
	<title>Example</title>
</head>
<body>
	Example
</body>
</html>`)
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		io.WriteString(w, index)
	})
	mux.HandleFunc("/next", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		io.WriteString(w, next)
	})
	return mux
}
Output:

Example

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrCanceledByUser is an error because of canceled by user.
	ErrCanceledByUser = errors.New("canceled by user")
)

Functions

func EnableLifeCycleEvents

func EnableLifeCycleEvents() chromedp.Action

EnableLifeCycleEvents enables life cycle events.

func IgnoreCacheReload

func IgnoreCacheReload(timeout time.Duration) chromedp.NavigateAction

IgnoreCacheReload is an action that reloads the current page without cache.

func Navigate(urlstr interface{}, timeout time.Duration) chromedp.NavigateAction

Navigate is an action that navigates the current frame.

urlstr can be specified by string, string pointer or fmt.Stringer.

func RestoreCookies added in v1.1.0

func RestoreCookies(filename interface{}, filters ...func(*network.Cookie) bool) chromedp.Action

RestoreCookies is an action that restores cookies from json lines file.

filename can be specified by string, string pointer or fmt.Stringer.

func SaveCookies added in v1.1.0

func SaveCookies(filename interface{}, maps ...func(*network.Cookie)) chromedp.Action

SaveCookies is an action that saves cookies as json lines file.

filename can be specified by string, string pointer or fmt.Stringer.

func Screenshot

func Screenshot(filename interface{}) chromedp.Action

Screenshot is an action that takes a screenshot of the entire browser viewport and save as image file.

Note: this will override the viewport emulation settings.

This function is based on https://github.com/chromedp/examples

filename can be specified by string, string pointer or fmt.Stringer.

func URL

func URL(endpoint, path string, val ...*string) fmt.Stringer

URL returns url string from endpoint and path.

func WaitForTime

func WaitForTime(t time.Time) chromedp.Action

WaitForTime is an action that waits until for time.

func WaitInput

func WaitInput(r io.Reader, message string, expected ...string) chromedp.Action

WaitInput is an action that waits until input.

func WaitLoaded

func WaitLoaded(timeout time.Duration) chromedp.Action

WaitLoaded is an action that waits until load event fired or timeout exceeded.

func WaitResponse

func WaitResponse(urlstr interface{}, timeout time.Duration, acts ...chromedp.Action) chromedp.Action

WaitResponse is an action that waits until response received or timeout exceeded.

urlstr can be specified by string, string pointer or fmt.Stringer.

Types

This section is empty.

Jump to

Keyboard shortcuts

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