|
6 | 6 | import datetime |
7 | 7 |
|
8 | 8 |
|
| 9 | +class LoadHandler(object): |
| 10 | + def OnLoadEnd(self, browser, frame, http_code, **_): |
| 11 | + if not frame.IsMain(): |
| 12 | + return |
| 13 | + manager = cef.CookieManager.GetGlobalManager() |
| 14 | + cookie = cef.Cookie() |
| 15 | + cookie.Set({ |
| 16 | + "name": "my_cookie", |
| 17 | + "value": "my_value", |
| 18 | + # Make sure domain is a valid value otherwise it crashes |
| 19 | + # app (Issue #459) |
| 20 | + "domain": ".google.com", |
| 21 | + "path": "/", |
| 22 | + "secure": True, |
| 23 | + "httpOnly": False, |
| 24 | + "creation": datetime.datetime(2018, 8, 22), |
| 25 | + "lastAccess": datetime.datetime(2018, 8, 22), |
| 26 | + "hasExpires": True, |
| 27 | + "expires": datetime.datetime(2028, 12, 31, 23, 59, 59), |
| 28 | + }) |
| 29 | + manager.SetCookie("https://www.google.com/", cookie) |
| 30 | + print("Cookie set: my_cookie=my_value") |
| 31 | + |
| 32 | + # Delay so SetCookie (async on IO thread) completes before visiting |
| 33 | + cef.PostDelayedTask(cef.TID_UI, 200, visit_cookies) |
| 34 | + |
| 35 | + |
| 36 | +def visit_cookies(): |
| 37 | + manager = cef.CookieManager.GetGlobalManager() |
| 38 | + manager.VisitUrlCookies( |
| 39 | + "https://www.google.com/", |
| 40 | + False, |
| 41 | + CookieVisitor()) |
| 42 | + |
| 43 | + |
| 44 | +class CookieVisitor(object): |
| 45 | + def Visit(self, cookie, count, total, delete_cookie_out): |
| 46 | + print("Cookie[%d/%d]: %s=%s (domain=%s)" % ( |
| 47 | + count + 1, total, |
| 48 | + cookie.Get("name"), cookie.Get("value"), |
| 49 | + cookie.Get("domain"))) |
| 50 | + return True # continue visiting |
| 51 | + |
| 52 | + |
9 | 53 | def main(): |
10 | 54 | cef.Initialize() |
11 | | - cef.CreateBrowserSync( |
12 | | - url="http://www.html-kit.com/tools/cookietester/", |
| 55 | + browser = cef.CreateBrowserSync( |
| 56 | + url="https://www.google.com/", |
13 | 57 | window_title="Set a cookie") |
14 | | - manager = cef.CookieManager.GetGlobalManager() |
15 | | - cookie = cef.Cookie() |
16 | | - cookie.Set({ |
17 | | - "name": "my_cookie", |
18 | | - "value": "my_value", |
19 | | - # Make sure domain is a valid value otherwise it crashes |
20 | | - # app (Issue #459) |
21 | | - "domain": "www.html-kit.com", |
22 | | - "path": "/", |
23 | | - "secure": False, |
24 | | - "httpOnly": False, |
25 | | - "creation": datetime.datetime(2018, 8, 22), |
26 | | - "lastAccess": datetime.datetime(2018, 8, 22), |
27 | | - "hasExpires": True, |
28 | | - "expires": datetime.datetime(2028, 12, 31, 23, 59, 59), |
29 | | - }) |
30 | | - manager.SetCookie("http://www.html-kit.com/", cookie) |
| 58 | + browser.SetClientHandler(LoadHandler()) |
31 | 59 | cef.MessageLoop() |
32 | 60 | cef.Shutdown() |
33 | 61 |
|
|
0 commit comments