-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcookies.go
55 lines (45 loc) · 1.19 KB
/
cookies.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
package cookies
import (
"context"
"fmt"
"net/http"
"github.com/mafredri/cdp"
)
// GetAllCookies returns all cookies from Chrome
func GetAllCookies(ctx context.Context, network cdp.Network) (cookies []*http.Cookie, err error) {
networkCookies, err := network.GetAllCookies(ctx)
if err != nil {
return cookies, err
}
for _, cookie := range networkCookies.Cookies {
c := http.Cookie{
Name: cookie.Name,
Value: cookie.Value,
Domain: cookie.Domain,
Path: cookie.Path,
// Expires
// RawExpires
// MaxAge
Secure: cookie.Secure,
HttpOnly: cookie.HTTPOnly,
// Do we need SameSite here?
// Raw
// Unparsed
}
cookies = append(cookies, &c)
}
return cookies, nil
}
// GetCookiesForDomain returns the cookies from Chrome for a specific domain name
func GetCookiesForDomain(ctx context.Context, network cdp.Network, domain string) (cookies []*http.Cookie, err error) {
allCookies, err := GetAllCookies(ctx, network)
if err != nil {
return cookies, fmt.Errorf("failed getting cookies from browser: %v", err)
}
for i := 0; i < len(allCookies); i++ {
if allCookies[i].Domain == domain {
cookies = append(cookies, allCookies[i])
}
}
return cookies, nil
}