-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_context.go
More file actions
115 lines (100 loc) · 2.72 KB
/
remote_context.go
File metadata and controls
115 lines (100 loc) · 2.72 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @Time: 2023/11/22 14:55
* @Author: LiuKun
* @File: remote_context.go
* @Description:
*/
package cdputil
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/chromedp/chromedp"
nt "github.com/stdawn/network"
"github.com/stdawn/util"
"os"
"os/exec"
"sync"
"time"
)
var (
remoteContext context.Context
remoteCancel context.CancelFunc
remoteContextMu sync.Mutex
)
// 清除远程上下文
func clearRemoteContext(c context.Context) {
remoteContextMu.Lock()
defer remoteContextMu.Unlock()
if c != remoteContext {
return
}
if remoteCancel != nil {
remoteCancel()
}
remoteContext = nil
remoteCancel = nil
}
// 获取远程上下文
func getRemoteContext() (context.Context, context.CancelFunc, error) {
remoteContextMu.Lock()
defer remoteContextMu.Unlock()
devtoolsWsUrl := getDevtoolsWsUrl()
if len(devtoolsWsUrl) > 0 {
if remoteContext != nil {
return remoteContext, remoteCancel, nil
}
remoteContext, remoteCancel = chromedp.NewRemoteAllocator(context.Background(), devtoolsWsUrl)
return remoteContext, remoteCancel, nil
}
if util.IsExeRunning(GetBrowserInfo().name) {
//关闭浏览器
err0 := util.CloseProgram(GetBrowserInfo().name)
if err0 != nil {
return nil, nil, errors.New(fmt.Sprintf("close original browser (%s) error:%s", GetBrowserInfo().name, err0.Error()))
}
time.Sleep(time.Second)
}
//指定cdp的ws连接端口
agr := []string{
fmt.Sprintf("--remote-debugging-port=%d", GetBrowserInfo().port),
}
if GetBrowserInfo().headless {
//指定浏览器无头模式
agr = append(agr, "--headless")
} else {
//指定浏览器窗口大小
if GetBrowserInfo().windowHeight > 0 && GetBrowserInfo().windowWidth > 0 {
agr = append(agr, fmt.Sprintf("--window-size=%d,%d", GetBrowserInfo().windowWidth, GetBrowserInfo().windowHeight))
}
}
//启动浏览器
cmd := exec.Command(os.ExpandEnv(GetBrowserInfo().url), agr...)
err1 := cmd.Start()
if err1 != nil {
return nil, nil, errors.New(fmt.Sprintf("start browser (%s) error : %s", GetBrowserInfo().url, err1.Error()))
}
//等待浏览器启动完成
for {
time.Sleep(100 * time.Millisecond)
devtoolsWsUrl = getDevtoolsWsUrl()
if len(devtoolsWsUrl) > 0 {
break
}
}
remoteContext, remoteCancel = chromedp.NewRemoteAllocator(context.Background(), devtoolsWsUrl)
return remoteContext, remoteCancel, nil
}
// 获取devtools的ws连接地址
func getDevtoolsWsUrl() string {
res, err := nt.Request(nt.RequestMethodGet, fmt.Sprintf("http://localhost:%d/json/version", GetBrowserInfo().port), "", nil)
if err == nil {
resMap := make(map[string]interface{})
err = json.Unmarshal([]byte(res), &resMap)
if err == nil {
return util.GetStringFromMap(resMap, "webSocketDebuggerUrl")
}
}
return ""
}