💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ## 概述 ``` package main import ( "bufio" "bytes" "context" "fmt" "os" "os/exec" ) func main() { args := []string{ "--disable-extensions", "--use-mock-keychain", "--disable-background-networking", "--enable-features=NetworkService,NetworkServiceInProcess", "--disable-default-apps", "--disable-dev-shm-usage", "--no-first-run", "--disable-backgrounding-occluded-windows", "--disable-breakpad", "--enable-automation", "--disable-sync", "--force-color-profile=srgb", "--mute-audio", "--disable-background-timer-throttling", "--safebrowsing-disable-auto-update", "--disable-features=site-per-process,Translate,BlinkGenPropertyTrees", "--disable-popup-blocking", "--disable-renderer-backgrounding", "--metrics-recording-only", "--password-store=basic", "--no-default-browser-check", "--hide-scrollbars", "--disable-hang-monitor", "--disable-prompt-on-repost", "--disable-client-side-phishing-detection", "--disable-ipc-flooding-protection", "--remote-debugging-port=0", "about:blank", } var allocTempDir = "" tempDir, err := os.MkdirTemp(allocTempDir, "chromedp-runner") if err != nil { panic(err) } //必须写 --user-data-dir 才能返回 websocket 的地址 args = append(args, "--user-data-dir="+tempDir) cmd := exec.CommandContext(context.Background(), "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", args...) pipe, err := cmd.StdoutPipe() if err != nil { panic(err) } cmd.Stderr = cmd.Stdout bufr := bufio.NewReader(pipe) if err := cmd.Start(); err != nil { panic(err) } prefix := []byte("DevTools listening on") go func() { for { line, err := bufr.ReadBytes('\n') if err != nil { continue } if bytes.HasPrefix(line, prefix) { line = line[len(prefix):] // use TrimSpace, to also remove \r on Windows line = bytes.TrimSpace(line) fmt.Printf("%+v\n", string(line)) // ws://127.0.0.1:61760/devtools/browser/37f9ad26-4632-4b36-aeda-17522454e962 } } }() // select {} } ``` > 注意:必须添加 `--user-data-dir=` 才能返回websocekt d 地址