-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromedp.go
81 lines (78 loc) · 2.51 KB
/
chromedp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package go_chrome
import (
"context"
"fmt"
"github.com/chromedp/chromedp"
"log"
"strings"
)
func getCtX(windowName, url string, opt ...GoChromeOptions) (context.Context, context.CancelFunc, *GoChromeOptions, string) {
ctxInit := context.WithValue(context.Background(), "windowsName", windowName)
runOpt := GoChromeOptions{
chromeRunCommand: ChromeRunCommand,
}
if len(opt) > 0 {
runOpt = opt[0]
runOpt.chromeRunCommand = ChromeRunCommand
chromeExecPath := getChromeExecPath(&runOpt)
runOpt.chromeExecPath = chromeExecPath
runOpt.chromeRunCommand["headless"] = runOpt.CliModule
gh := getHttp(&runOpt)
url = gh.GetUrl(url, runOpt.UseHttpServer)
if runOpt.AppModule || strings.HasPrefix(url, "data:text/html") {
runOpt.chromeRunCommand["app"] = url
}
if runOpt.WindowWidth > 0 && runOpt.WindowHeight > 0 {
runOpt.chromeRunCommand["window-size"] = fmt.Sprintf("%d,%d", runOpt.WindowWidth, runOpt.WindowHeight)
}
if runOpt.WindowPositionWidth > 0 && runOpt.WindowPositionHeight > 0 {
runOpt.chromeRunCommand["window-position"] = fmt.Sprintf("%d,%d", runOpt.WindowPositionWidth, runOpt.WindowPositionHeight)
}
} else {
gh := getHttp(&runOpt)
url = gh.GetUrl(url, runOpt.UseHttpServer)
chromeExecPath := getChromeExecPath(&runOpt)
runOpt.chromeExecPath = chromeExecPath
}
execAllocatorOption := []chromedp.ExecAllocatorOption{}
if runOpt.chromeExecPath != "" {
execAllocatorOption = append(execAllocatorOption, chromedp.ExecPath(runOpt.chromeExecPath))
}
for k, v := range runOpt.chromeRunCommand {
execAllocatorOption = append(execAllocatorOption, chromedp.Flag(k, v))
}
if runOpt.ChromeExecAllocatorOption != nil {
execAllocatorOption = append(execAllocatorOption, runOpt.ChromeExecAllocatorOption...)
}
if runOpt.chromeExecPath != "" {
execAllocatorOption = append(execAllocatorOption, chromedp.ExecPath(runOpt.chromeExecPath))
}
ctx, _ := chromedp.NewExecAllocator(
ctxInit,
append(
chromedp.DefaultExecAllocatorOptions[:],
execAllocatorOption...,
)...,
)
newCtx, cancel := chromedp.NewContext(
ctx,
// 设置日志方法
chromedp.WithLogf(log.Printf),
)
return newCtx, cancel, &runOpt, url
}
func getChromeExecPath(opt *GoChromeOptions) string {
chromeExecPath := ""
packConfig := getPackageConfig()
if packConfig != nil && packConfig.ChromeExecPath != "" {
// test
return packConfig.ChromeExecPath
}
if opt.RestoreAssets != nil {
path, err := GetBrowserPath(opt)
if err == nil {
return path + "\\chrome-win\\chrome.exe"
}
}
return chromeExecPath
}