@@ -24,33 +24,80 @@ import (
2424 "time"
2525)
2626
27+ const UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36"
28+
2729// 启动
2830// context.Context部分不能抽离,否则会报 context canceled
29- func Apply (actions ... chromedp.Action ) error {
31+ func Apply (actions chromedp.Action , opts ... chromedp.ExecAllocatorOption ) error {
32+
33+ //dir, err := ioutil.TempDir("", "chromedp-example")
34+ //if err != nil {
35+ // panic(err)
36+ //}
37+ //defer os.RemoveAll(dir)
38+
39+ ctx , cancel := chromedp .NewExecAllocator (context .Background (), opts ... )
40+ // 关闭chrome实例
41+ defer cancel ()
42+
43+ // 自定义记录器
44+ ctx , cancel = chromedp .NewContext (ctx , chromedp .WithLogf (log .Printf ))
45+ // 释放所有资源,并等待释放结束
46+ defer cancel ()
47+
48+ // 设置超时时间
49+ ctx , cancel = context .WithTimeout (ctx , 3 * time .Minute )
50+ // 超时关闭chrome实例
51+ defer cancel ()
52+
53+ // listen network event
54+ //listenForNetworkEvent(ctx)
55+
56+ return chromedp .Run (ctx , actions )
57+ }
58+
59+ func ApplyDebug (actions chromedp.Action ) error {
3060
3161 //dir, err := ioutil.TempDir("", "chromedp-example")
3262 //if err != nil {
3363 // panic(err)
3464 //}
3565 //defer os.RemoveAll(dir)
3666
37- userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36 "
67+ opts := append (chromedp .DefaultExecAllocatorOptions [:],
68+ // 设置UA,防止有些页面识别headless模式
69+ chromedp .UserAgent (UserAgent ),
70+ // 窗口最大化
71+ chromedp .Flag ("start-maximized" , true ),
72+ )
73+ return Apply (actions , opts ... )
74+ }
75+
76+ func ApplyRun (actions chromedp.Action ) error {
77+
78+ //dir, err := ioutil.TempDir("", "chromedp-example")
79+ //if err != nil {
80+ // panic(err)
81+ //}
82+ //defer os.RemoveAll(dir)
3883
3984 opts := append (chromedp .DefaultExecAllocatorOptions [:],
4085 chromedp .NoDefaultBrowserCheck ,
4186 // 无头模式
42- // chromedp.Flag("headless", false),
43- chromedp .Headless ,
87+ chromedp .Flag ("headless" , false ),
88+ // chromedp.Headless,
4489 // 禁用GPU,不显示GUI
45- chromedp .DisableGPU ,
90+ // chromedp.DisableGPU,
4691 // 隐身模式启动
4792 chromedp .Flag ("incognito" , true ),
4893 // 取消沙盒模式
4994 chromedp .NoSandbox ,
5095 // 忽略证书错误
5196 chromedp .Flag ("ignore-certificate-errors" , true ),
5297 // 指定浏览器分辨率
53- chromedp .WindowSize (1600 , 900 ),
98+ //chromedp.WindowSize(1600, 900),
99+ // 窗口最大化
100+ chromedp .Flag ("start-maximized" , true ),
54101 //
55102 chromedp .Flag ("in-process-plugins" , true ),
56103 // 不加载图片, 提升速度
@@ -60,30 +107,12 @@ func Apply(actions ...chromedp.Action) error {
60107 // 隐藏滚动条, 应对一些特殊页面
61108 //chromedp.Flag("hide-scrollbars", true),
62109 // 设置UA,防止有些页面识别headless模式
63- chromedp .UserAgent (userAgent ),
110+ chromedp .UserAgent (UserAgent ),
64111 // 设置用户数据目录
65112 //chromedp.UserDataDir(dir),
66113 //chromedp.ExecPath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"),
67114 )
68-
69- ctx , cancel := chromedp .NewExecAllocator (context .Background (), opts ... )
70- // 关闭chrome实例
71- defer cancel ()
72-
73- // 自定义记录器
74- ctx , cancel = chromedp .NewContext (ctx , chromedp .WithLogf (log .Printf ))
75- // 释放所有资源,并等待释放结束
76- defer cancel ()
77-
78- // 设置超时时间
79- ctx , cancel = context .WithTimeout (ctx , 3 * time .Minute )
80- // 超时关闭chrome实例
81- defer cancel ()
82-
83- // listen network event
84- //listenForNetworkEvent(ctx)
85-
86- return chromedp .Run (ctx , actions ... )
115+ return Apply (actions , opts ... )
87116}
88117
89118//监听
0 commit comments