chromy

package module
v0.0.0-...-1ce1696 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2017 License: MIT Imports: 17 Imported by: 0

README

Chromy

A tool for driving browsers via chrome-devtool-protocol, based on github.com/mafredri/cdp.

Inspired by github.com/knq/chromedp, chromy provides ordered actions with more timeout-controls.

INSTALLATION

go get -v github.com/dtynn/chromy

EXAMPLE

package main

import (
    "context"
    "log"
    "net/http"
    "regexp"
    "time"

    "github.com/dtynn/chromy"
    "github.com/dtynn/chromy/cdptype"
)

var commonJSpattern = regexp.MustCompile("http[s]?://.+/ReactJSstatic/js/entrys/common\\..+\\.bundle\\.js")

func main() {
    connector := chromy.Connect(
        chromy.ConnectTimeout(5*time.Second),
        chromy.ActionTimeout(1*time.Minute),
        chromy.TaskStepTimeout(5*time.Second),
    )

    t, err := connector.New(context.Background())
    if err != nil {
        log.Fatalln(err)
    }

    defer t.Close()

    onCategoryNodes := func(nodes ...*chromy.Node) error {
        for i, node := range nodes {
            var category, href string

            href = node.Attr("href")
            for _, child := range node.Children {
                if child.NodeType == cdptype.NodeTypeText {
                    category = child.NodeValue
                    break
                }
            }

            log.Printf("%d [%s]<%s>", i+1, category, href)
        }

        return nil
    }

    task := chromy.Task{
        chromy.Navigate("https://ezbuy.sg"),
        chromy.DocumentReady(),
        chromy.WaitResource(http.MethodGet, commonJSpattern),
        chromy.OnNodeAll(`div[id^="category-"] > div:first-child > a`, onCategoryNodes),
    }

    if err := t.Run(context.Background(), task); err != nil {
        log.Println("task error:", err)
    }
}

TODO

  • ✘ godoc
  • ✘ examples
  • ✘ connector pooling
  • ✘ other domain managers
  • ✘ more actions
  • ✘ tests and benchmarks

LICENSE

Under MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNodeNotFound        = fmt.Errorf("dom node not found")
	ErrNoQueryFunc         = fmt.Errorf("no query function")
	ErrNoMatchFunc         = fmt.Errorf("no match function")
	ErrUnableToResolveNode = fmt.Errorf("unable to resolve node")
)

Functions

This section is empty.

Types

type Action

type Action interface {
	Do(context.Context, *Target) error
}

Action

func Blur

func Blur(selector string) Action

func CaptureRequests

func CaptureRequests(macher func(*Request) bool, onRequest func(*Request)) Action

func CaptureScreenshot

func CaptureScreenshot(w io.Writer) Action

func Click

func Click(selector string) Action

func DocumentReady

func DocumentReady() Action

func Focus

func Focus(selector string) Action

func GetNode

func GetNode(selector string, node *Node) Action

func GetNodeAll

func GetNodeAll(selector string, nodes *[]*Node) Action
func Navigate(URL string) Action

func OnNode

func OnNode(selector string, fn func(ctx context.Context, t *Target, node *Node) error) Action

func OnNodeAll

func OnNodeAll(selector string, fn func(ctx context.Context, t *Target, nodes ...*Node) error) Action

func PageLoaded

func PageLoaded() Action

func Sleep

func Sleep(d time.Duration) Action

func WaitNode

func WaitNode(selector string) Action

func WaitNodeAll

func WaitNodeAll(selector string) Action

func WaitResource

func WaitResource(method string, urlpattern URLPattern) Action

func WaitResourceWithMatcher

func WaitResourceWithMatcher(matcher func(*Request) bool) Action

type ActionError

type ActionError struct {
	// contains filtered or unexported fields
}

func (*ActionError) Cause

func (a *ActionError) Cause() error

func (*ActionError) Error

func (a *ActionError) Error() string

type ActionFunc

type ActionFunc func(context.Context, *Target) error

ActionFunc

func (ActionFunc) Do

func (a ActionFunc) Do(ctx context.Context, t *Target) error

type CallOption

type CallOption func(*runtime.CallFunctionOnArgs)

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

func Connect

func Connect(opt ...Option) *Connector

Connect return a connector to specified remote debugging url

func (*Connector) New

func (c *Connector) New(ctx context.Context) (*Target, error)

type DOM

type DOM struct {
	cdp.DOM
	ErrTracer
	// contains filtered or unexported fields
}

type ErrTracer

type ErrTracer interface {
	Trace(err error)
}

type Network

type Network struct {
	cdp.Network
	ErrTracer
	// contains filtered or unexported fields
}

type Node

type Node struct {
	dom.Node
}

func (*Node) Attr

func (n *Node) Attr(attr string) string

type NonErrTracer

type NonErrTracer struct {
}

func (*NonErrTracer) Trace

func (n *NonErrTracer) Trace(err error)

type Option

type Option func(*Connector)

Option connect option

func ActionTimeout

func ActionTimeout(timeout time.Duration) Option

func ConnectTimeout

func ConnectTimeout(timeout time.Duration) Option

func RemoteDebuggingURL

func RemoteDebuggingURL(remoteURL string) Option

RemoteDebuggingURL mo

func TaskStepTimeout

func TaskStepTimeout(timeout time.Duration) Option

type Page

type Page struct {
	cdp.Page
	ErrTracer
	// contains filtered or unexported fields
}

type Query

type Query struct {
	// contains filtered or unexported fields
}

func NewQuery

func NewQuery(opt ...QueryOption) *Query

func (*Query) Do

func (q *Query) Do(ctx context.Context, t *Target) error

type QueryOption

type QueryOption func(q *Query)

func QueryAfter

func QueryAfter(fn func(ctx context.Context, t *Target, node *Node) error) QueryOption

func QueryAfterAll

func QueryAfterAll(fn func(ctx context.Context, t *Target, nodes ...*Node) error) QueryOption

func QueryFunc

func QueryFunc(fn func(ctx context.Context, t *Target, root *Node) ([]dom.NodeID, error)) QueryOption

func QueryNode

func QueryNode(ptr *Node) QueryOption

func QueryNodes

func QueryNodes(ptr *[]*Node) QueryOption

func QuerySearch

func QuerySearch(query string) QueryOption

func QuerySelector

func QuerySelector(selector string) QueryOption

func QuerySelectorAll

func QuerySelectorAll(selector string) QueryOption

type Request

func (*Request) Done

func (r *Request) Done() bool

func (*Request) IsFailed

func (r *Request) IsFailed() bool

func (*Request) IsFinished

func (r *Request) IsFinished() bool

type Resource

type Resource struct {
	// contains filtered or unexported fields
}

func NewResource

func NewResource(opt ...ResourceOption) *Resource

func (*Resource) Do

func (r *Resource) Do(ctx context.Context, t *Target) error

type ResourceOption

type ResourceOption func(*Resource)

func ResourceAfter

func ResourceAfter(fn func(*Request) error) ResourceOption

func ResourceDone

func ResourceDone() ResourceOption

func ResourceMatch

func ResourceMatch(fn func(*Request) bool) ResourceOption

func ResourcePattern

func ResourcePattern(method string, urlpattern URLPattern) ResourceOption

type Target

type Target struct {
	// contains filtered or unexported fields
}

Target devtool target

func (*Target) Client

func (t *Target) Client() *cdp.Client

func (*Target) Close

func (t *Target) Close() error

func (*Target) Run

func (t *Target) Run(ctx context.Context, action Action) error

func (*Target) Wait

func (t *Target) Wait()

type Task

type Task []Action

func (Task) Do

func (t Task) Do(ctx context.Context, tar *Target) error

type URLPattern

type URLPattern interface {
	MatchString(string) bool
}

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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