Skip to content

Commit

Permalink
Splitting repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Shaw committed Dec 28, 2017
1 parent 9424dc5 commit b0c2445
Show file tree
Hide file tree
Showing 219 changed files with 345 additions and 120,677 deletions.
78 changes: 46 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
# About chromedp [![Build Status](https://travis-ci.org/knq/chromedp.svg)](https://travis-ci.org/knq/chromedp) [![Coverage Status](https://coveralls.io/repos/knq/chromedp/badge.svg?branch=master&service=github)](https://coveralls.io/github/knq/chromedp?branch=master) #
# About chromedp [![Build Status][1]][2] [![Coverage Status][3]][4]

Package chromedp is a faster, simpler way to drive browsers in Go using the
[Chrome Debugging Protocol](https://developer.chrome.com/devtools/docs/debugger-protocol)
(for Chrome, Edge, Safari, etc) without external dependencies (ie, Selenium, PhantomJS, etc).
[Chrome Debugging Protocol][5] (for Chrome, Edge, Safari, etc) without external
dependencies (ie, Selenium, PhantomJS, etc).

**NOTE:** chromedp's API is currently unstable, and may change at a moments
notice. There are likely extremely bad bugs lurking in this code. **CAVEAT USER**.

## Installation
## Installing

Install in the usual way:

```sh
go get -u github.com/knq/chromedp
go get -u github.com/chromedp/chromedp
```

## Usage
## Using

Below is a simple Google search performed using chromedp (taken from
[examples/simple](examples/simple/main.go)):
[examples/simple][6]):

This example shows logic for a simple search for a known website, clicking on
the right link, and then taking a screenshot of a specific element on the
loaded page and saving that to a local file on disk.

```go
// examples/simple/main.go
// Command simple is a chromedp example demonstrating how to do a simple google
// search.
package main

import (
Expand All @@ -35,8 +36,8 @@ import (
"log"
"time"

cdp "github.com/knq/chromedp"
cdptypes "github.com/knq/chromedp/cdp"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp"
)

func main() {
Expand All @@ -47,7 +48,7 @@ func main() {
defer cancel()

// create chrome instance
c, err := cdp.New(ctxt, cdp.WithLog(log.Printf))
c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf))
if err != nil {
log.Fatal(err)
}
Expand All @@ -74,40 +75,53 @@ func main() {
log.Printf("saved screenshot from search result listing `%s` (%s)", res, site)
}

func googleSearch(q, text string, site, res *string) cdp.Tasks {
func googleSearch(q, text string, site, res *string) chromedp.Tasks {
var buf []byte
sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
return cdp.Tasks{
cdp.Navigate(`https://www.google.com`),
cdp.WaitVisible(`#hplogo`, cdp.ByID),
cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
cdp.WaitVisible(`#res`, cdp.ByID),
cdp.Text(sel, res),
cdp.Click(sel),
cdp.WaitVisible(`a[href="/brankas-for-business"]`, cdp.ByQuery),
cdp.WaitNotVisible(`.preloader-content`, cdp.ByQuery),
cdp.Location(site),
cdp.ScrollIntoView(`.banner-section.third-section`, cdp.ByQuery),
cdp.Sleep(2 * time.Second), // wait for animation to finish
cdp.Screenshot(`.banner-section.third-section`, &buf, cdp.ByQuery),
cdp.ActionFunc(func(context.Context, cdptypes.Handler) error {
return chromedp.Tasks{
chromedp.Navigate(`https://www.google.com`),
chromedp.WaitVisible(`#hplogo`, chromedp.ByID),
chromedp.SendKeys(`#lst-ib`, q+"\n", chromedp.ByID),
chromedp.WaitVisible(`#res`, chromedp.ByID),
chromedp.Text(sel, res),
chromedp.Click(sel),
chromedp.WaitNotVisible(`.preloader-content`, chromedp.ByQuery),
chromedp.WaitVisible(`a[href*="twitter"]`, chromedp.ByQuery),
chromedp.Location(site),
chromedp.ScrollIntoView(`.banner-section.third-section`, chromedp.ByQuery),
chromedp.Sleep(2 * time.Second), // wait for animation to finish
chromedp.Screenshot(`.banner-section.third-section`, &buf, chromedp.ByQuery),
chromedp.ActionFunc(func(context.Context, cdp.Executor) error {
return ioutil.WriteFile("screenshot.png", buf, 0644)
}),
}
}
```

Please see the [examples](examples/) directory for some more examples, and
please refer to the [GoDoc API listing](https://godoc.org/github.com/knq/chromedp)
for a summary of the API and Actions.
Please see the [examples][6] project for more examples. Please refer to the
[GoDoc API listing][7] for a summary of the API and Actions.

## Links + Resources
* [chromedp: A New Way to Drive the Web (GopherCon SG 2017)](https://www.youtube.com/watch?v=_7pWCg94sKw)
* [Chrome DevTools Protocol Viewer](https://chromedevtools.github.io/devtools-protocol/)
## Resources

* [chromedp: A New Way to Drive the Web][8] - GopherCon 2017 talk by Kenneth Shaw
* [Chrome DevTools Protocol][5] - Chrome Debugging Protocol Domain documentation
* [chromedp examples][6] - various `chromedp` examples
* [`github.com/chromedp/cdproto`] - GoDoc listing for
* [`github.com/chromedp/chromedp-gen`][9] - tool used to generate

## TODO

* Move timeouts to context (defaults)
* Implement more query selector options (allow over riding context timeouts)
* Contextual actions for "dry run" (or via an accumulator?)
* Network loader / manager
* Profiler

[1]: https://travis-ci.org/chromedp/chromedp.svg
[2]: https://travis-ci.org/chromedp/chromedp
[3]: https://coveralls.io/repos/chromedp/chromedp/badge.svg?branch=master&service=github
[4]: https://coveralls.io/github/chromedp/chromedp?branch=master
[5]: https://chromedevtools.github.io/devtools-protocol/
[6]: https://github.com/chromedp/examples
[7]: https://godoc.org/github.com/chromedp/chromedp
[8]: https://www.youtube.com/watch?v=_7pWCg94sKw
12 changes: 6 additions & 6 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import (
"context"
"time"

"github.com/knq/chromedp/cdp"
"github.com/chromedp/cdproto/cdp"
)

// Action is the common interface for an action that will be executed against a
// context and frame handler.
type Action interface {
// Do executes the action using the provided context and frame handler.
Do(context.Context, cdp.Handler) error
Do(context.Context, cdp.Executor) error
}

// ActionFunc is a adapter to allow the use of ordinary func's as an Action.
type ActionFunc func(context.Context, cdp.Handler) error
type ActionFunc func(context.Context, cdp.Executor) error

// Do executes the func f using the provided context and frame handler.
func (f ActionFunc) Do(ctxt context.Context, h cdp.Handler) error {
func (f ActionFunc) Do(ctxt context.Context, h cdp.Executor) error {
return f(ctxt, h)
}

Expand All @@ -27,7 +27,7 @@ type Tasks []Action

// Do executes the list of Actions sequentially, using the provided context and
// frame handler.
func (t Tasks) Do(ctxt context.Context, h cdp.Handler) error {
func (t Tasks) Do(ctxt context.Context, h cdp.Executor) error {
// TODO: put individual task timeouts from context here
for _, a := range t {
// ctxt, cancel = context.WithTimeout(ctxt, timeout)
Expand All @@ -46,7 +46,7 @@ func (t Tasks) Do(ctxt context.Context, h cdp.Handler) error {
// be marked for deprecation in the future, after the remaining Actions have
// been able to be written/tested.
func Sleep(d time.Duration) Action {
return ActionFunc(func(ctxt context.Context, h cdp.Handler) error {
return ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
select {
case <-time.After(d):

Expand Down
59 changes: 0 additions & 59 deletions cdp/accessibility/accessibility.go

This file was deleted.

Loading

0 comments on commit b0c2445

Please sign in to comment.