-
Notifications
You must be signed in to change notification settings - Fork 2
/
page.go
101 lines (93 loc) · 2.48 KB
/
page.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package chatgpt
import (
"github.com/cherish-chat/chatgpt-firefox/config"
"github.com/playwright-community/playwright-go"
"github.com/sirupsen/logrus"
"log"
)
// Deprecated 请使用GetPage
func (h *Helper) MustGetPage(id string) playwright.Page {
if page, ok := h.pageMap.Load(id); ok {
return page.(playwright.Page)
}
var page playwright.Page
var err error
page, err = h.NewPage(id)
if err != nil {
logrus.Errorf("Error while creating new page: %v", err)
log.Fatalf("Error while creating new page: %v", err)
}
return page
}
func (h *Helper) GetPage(id string) (playwright.Page, error) {
if page, ok := h.pageMap.Load(id); ok {
return page.(playwright.Page), nil
}
return h.NewPage(id)
}
func (h *Helper) GetPlusPage(id string) (playwright.Page, error) {
if page, ok := h.pageMap.Load(id); ok {
return page.(playwright.Page), nil
}
return h.NewPlusPage(id)
}
func (h *Helper) NewPage(id string) (playwright.Page, error) {
page, err := h.browser.NewPage()
if err != nil {
logrus.Errorf("Error while creating new page: %v", err)
return nil, err
}
logrus.Info("New page created successfully")
{
// 设置cookie
cookies, err := config.LoadCookies("cookies.json")
if err != nil {
return nil, err
}
err = h.browser.AddCookies(cookies...)
if err != nil {
logrus.Errorf("Error while adding cookies: %v", err)
return nil, err
}
logrus.Info("Cookies added successfully")
}
_, err = page.Goto("https://chat.openai.com/chat")
if err != nil {
page.Close()
logrus.Errorf("Error while navigating to google: %v", err)
return nil, err
}
logrus.Info("Navigated to openai successfully")
h.pageMap.Store(id, page)
return page, nil
}
func (h *Helper) NewPlusPage(id string) (playwright.Page, error) {
page, err := h.browser.NewPage()
if err != nil {
logrus.Errorf("Error while creating new page: %v", err)
return nil, err
}
logrus.Info("New page created successfully")
{
// 设置cookie
cookies, err := config.LoadCookies("cookies.json")
if err != nil {
return nil, err
}
err = h.browser.AddCookies(cookies...)
if err != nil {
logrus.Errorf("Error while adding cookies: %v", err)
return nil, err
}
logrus.Info("Cookies added successfully")
}
_, err = page.Goto("https://chat.openai.com/chat?model=text-davinci-002-render-paid")
if err != nil {
page.Close()
logrus.Errorf("Error while navigating to google: %v", err)
return nil, err
}
logrus.Info("Navigated to openai successfully")
h.pageMap.Store(id, page)
return page, nil
}