Skip to content

Instantly share code, notes, and snippets.

@alpacas9
Last active November 27, 2019 11:38
Show Gist options
  • Select an option

  • Save alpacas9/2ea0ab79597d9a44a8444defbf5c1514 to your computer and use it in GitHub Desktop.

Select an option

Save alpacas9/2ea0ab79597d9a44a8444defbf5c1514 to your computer and use it in GitHub Desktop.
cdp simple
package main
import (
"context"
"log"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/chromedp"
)
var switches = []chromedp.ExecAllocatorOption{
chromedp.Flag("headless", false),
chromedp.Flag("no-zygote", true),
chromedp.Flag("no-sandbox", true),
chromedp.Flag("disable-features", "site-per-process,TranslateUI,InfiniteSessionRestore,BlinkGenPropertyTrees"),
chromedp.Flag("enable-blink-features", "ShadowDOMV0,CustomElementsV0,HTMLImports"),
chromedp.Flag("enable-experimental-web-platform-features", true),
chromedp.Flag("disable-zero-suggest", true),
chromedp.Flag("disable-sync", true),
chromedp.Flag("disable-sync-app-list", true),
chromedp.Flag("disable-dev-shm-usage", true),
chromedp.Flag("disable-crash-reporter", true),
chromedp.Flag("disable-breakpad", true),
chromedp.Flag("ignore-certificate-errors", true),
chromedp.Flag("no-first-run", true),
chromedp.Flag("disable-background-networking", true),
chromedp.Flag("enable-features", "NetworkService,NetworkServiceInProcess"),
chromedp.Flag("disable-background-timer-throttling", true),
chromedp.Flag("disable-client-side-phishing-detection", true),
chromedp.Flag("disable-default-apps", true),
chromedp.Flag("disable-popup-blocking", true),
chromedp.Flag("disable-prompt-on-repost", true),
chromedp.Flag("disable-renderer-backgrounding", true),
chromedp.Flag("disable-hang-monitor", true),
chromedp.Flag("disable-ipc-flooding-protection", true),
chromedp.Flag("disable-system-timezone-automatic-detection", true),
chromedp.Flag("force-color-profile", "srgb"),
chromedp.Flag("metrics-recording-only", true),
chromedp.Flag("password-store", "basic"),
chromedp.Flag("hide-scrollbars", true),
chromedp.Flag("disable-infobars", true),
chromedp.Flag("disable-backgrounding-occluded-windows", true),
chromedp.Flag("disable-popup-blocking", true),
chromedp.Flag("disable-prompt-on-repost", true),
chromedp.Flag("disable-renderer-backgrounding", true),
chromedp.Flag("disable-sync", true),
chromedp.Flag("no-first-run", true),
chromedp.Flag("password-store", "basic"),
chromedp.Flag("use-mock-keychain", true),
chromedp.Flag("test-type", true),
chromedp.Flag("hide-scrollbars", true),
chromedp.Flag("no-default-browser-check", true),
}
func main() {
log.Println("Starting...")
// TODO: pass dir as flag
var userDataDir string = "/tmp/testdir"
switches = append(switches, chromedp.Flag("user-data-dir", userDataDir))
// create context
ctx, cancel := chromedp.NewExecAllocator(context.Background(), switches...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx, chromedp.WithDebugf(log.Printf))
defer cancel()
// evaluate js script on new document if needed
// test script
err := chromedp.Run(ctx, sendkeys())
if err != nil {
log.Fatal(err)
}
s := make(chan struct{})
<-s
}
func sendkeys() chromedp.Tasks {
var scriptID page.ScriptIdentifier
var injectScript = `console.log("injected")`
return chromedp.Tasks{
chromedp.ActionFunc(func(ctx context.Context) error {
var err error
scriptID, err = page.AddScriptToEvaluateOnNewDocument(injectScript).Do(ctx)
if err != nil {
return err
}
return nil
}),
page.Enable(),
emulation.SetTimezoneOverride("America/Jamaica"),
emulation.SetGeolocationOverride().WithLatitude(5.0).WithLongitude(5.0).WithAccuracy(10.0),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment