Created
September 22, 2022 06:23
-
-
Save zacharysyoung/847b8aee50471e95d37f184de7050038 to your computer and use it in GitHub Desktop.
ChromeDP, get whole response body
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the entire response body of the Navigate() (?) as a string. | |
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"time" | |
"github.com/chromedp/cdproto/cdp" | |
"github.com/chromedp/cdproto/network" | |
"github.com/chromedp/chromedp" | |
) | |
func main() { | |
opts := append(chromedp.DefaultExecAllocatorOptions[:], | |
chromedp.DisableGPU, | |
chromedp.NoDefaultBrowserCheck, | |
chromedp.Flag("headless", false), | |
chromedp.Flag("ignore-certificate-errors", true), | |
chromedp.Flag("window-size", "50,400"), | |
) | |
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...) | |
defer cancel() | |
// also set up a custom logger | |
taskCtx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf)) | |
defer cancel() | |
// create a timeout | |
taskCtx, cancel = context.WithTimeout(taskCtx, 3*time.Second) | |
defer cancel() | |
cRB := make(chan string) | |
chromedp.ListenTarget( | |
taskCtx, | |
func(ev interface{}) { | |
evtResp, ok := ev.(*network.EventResponseReceived) | |
if !ok { | |
return | |
} | |
if evtResp.Type != network.ResourceTypeDocument { | |
return | |
} | |
go func() { | |
c := chromedp.FromContext(taskCtx) | |
rbp := network.GetResponseBody(evtResp.RequestID) | |
respBody, err := rbp.Do(cdp.WithExecutor(taskCtx, c.Target)) | |
if err != nil { | |
log.Printf("error: %q\n", err) | |
} | |
cRB <- string(respBody) | |
}() | |
}, | |
) | |
err := chromedp.Run(taskCtx, | |
network.Enable(), | |
chromedp.Navigate("http://example.com"), | |
) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(<-cRB) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment