Skip to content

Add an example for how to use chromedp.NewRemoteAllocator. #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2019
Merged

Add an example for how to use chromedp.NewRemoteAllocator. #19

merged 1 commit into from
Aug 15, 2019

Conversation

bjorndown
Copy link
Contributor

From the godocs of chromedp.NewRemoteAllocator I was not able to work out how it is supposed to be used. I ended up doing it like this. If this is how it is supposed to be used, it might make a good addition for the examples repo.

@bjorndown
Copy link
Contributor Author

Had to account for https://bugs.chromium.org/p/chromium/issues/detail?id=813540. This should now be the final version.

Copy link
Contributor

@mvdan mvdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is fine to add, but it could be done in about 60 lines instead of 140. I've left some comments; I can give another review once you're happy with the code.

remote/main.go Outdated
//
// Chromium will return the error "Host header is specified and is not an IP address or localhost" if the
// DevTools are connected to via a hostname other than localhost.
func replaceResolvedHostnameIfNeeded(url *url.URL) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chromedp already does this; remove this function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function removed

remote/main.go Outdated
}

resp, err := http.Get(chromiumRemoteUrl.String())

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove all the unnecessary spacing, for example before the error checks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

remote/main.go Outdated
log.Fatal(err)
}

ctxt, cancel := chromedp.NewRemoteAllocator(context.Background(), wsUrl)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we generally use actx for allocator contexts, and ctx for regular contexts.

it would also be better to create both contexts in the same block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

remote/main.go Outdated
var body string
if err := chromedp.Run(ctxt,
chromedp.Navigate(url),
chromedp.Sleep(time.Second), // wait for things to settle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use a sleep. you can check existing examples like text/ for reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

remote/main.go Outdated
}

for _, target := range targets {
if target.Url == "about:blank" { // we assume this is the default page and that it is always around
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, but this is wrong - RemoteAllocator should be used with the browser's websocket url, not a page's.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood

remote/main.go Outdated
return "", err
}

bytes, err := ioutil.ReadAll(resp.Body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can avoid this ReadAll via json.NewDecoder(resp.Body).Decode(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed because no longer needed.

@bjorndown
Copy link
Contributor Author

Thanks for the review and sorry for the mess. I hadn't realized that chrome prints the devtools endpoint when it starts 🤦‍♂️

FWIW In my project I start a crawler and chromedp/headless-shell as two containers. As there is no way to guess the devtools endpoint from the crawler, I took this convuluted approach.

Glad I can offer you a more canonical example now.

Copy link
Contributor

@mvdan mvdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is fine.

@mvdan mvdan merged commit 4e55d33 into chromedp:master Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants