Skip to content

Instantly share code, notes, and snippets.

@ysmood
Last active October 30, 2020 06:51
Show Gist options
  • Save ysmood/0d5b2c878ecbdb598776af7d3d305b79 to your computer and use it in GitHub Desktop.
Save ysmood/0d5b2c878ecbdb598776af7d3d305b79 to your computer and use it in GitHub Desktop.
Such as when you want to inject a large js helper to the page, chromedp will crash.
package main
import (
"context"
"fmt"
"log"
"strings"
"github.com/chromedp/chromedp"
)
func main() {
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
var res string
err := chromedp.Run(ctx,
chromedp.Navigate(`https://example.com/`),
chromedp.Evaluate(fmt.Sprintf(`"%s"`, strings.Repeat("t", 2_000_000)), &res),
)
if err != nil {
log.Fatal(err) // will fail at here: context canceled
}
log.Println(len(res))
}
@ysmood
Copy link
Author

ysmood commented Oct 30, 2020

Rod doesn't have this limitation:

package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/go-rod/rod"
)

func main() {
	page := rod.New().MustConnect().MustPage("https://example.com/")
	res := page.MustEval(fmt.Sprintf(`"%s"`, strings.Repeat("t", 2_000_000))).Str()
	log.Println(len(res)) // Output: 2000000
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment