在JS中通过ICP消息操作cookie:
JS代码:
<script type="application/javascript"> function message() { return document.getElementById("message"); } //显示cookie function VisitCookie() { message().innerHTML = ""; //触发go中监听的事件 ipc.emit("VisitCookie", function (data) { message().innerHTML = data; }); } //显示cookie的结果,从go中返回到html ipc.on("VisitCookieResult", function (data) { message().innerHTML = message().innerHTML + data + "<br><br>"; }); //删除cookie function DeleteCookie() { message().innerHTML = ""; //触发go中监听的事件 ipc.emit("DeleteCookie", function (data) { message().innerHTML = data; }); } //设置cookie function SetCookie() { message().innerHTML = ""; //触发go中监听的事件 ipc.emit("SetCookie", function (data) { message().innerHTML = data; }); } </script> </head> <body style="overflow: hidden;margin: 0px;padding: 0px;"> <button onclick="VisitCookie()">查看cookie</button> <button onclick="DeleteCookie()">删除cookie</button> <button onclick="SetCookie()">设置cookie</button> <br> <div id="message"> </div>
GO代码:
//监听获取cookie事件 ipc.On("VisitCookie", func(context context.IContext) { fmt.Println("VisitCookie") info := cef.BrowserWindow.GetWindowInfo(context.BrowserId()) info.Chromium().VisitURLCookies("https://www.baidu.com", true, 1) info.Chromium().VisitAllCookies(1) context.Result("执行成功,结果将在 SetOnCookiesVisited 事件中获得") }) //监听删除cookie ipc.On("DeleteCookie", func(context context.IContext) { info := cef.BrowserWindow.GetWindowInfo(context.BrowserId()) info.Chromium().DeleteCookies("", "", false) context.Result("执行成功,结果将在 SetOnCookiesDeleted 事件中获得") }) //监听设置cookie ipc.On("SetCookie", func(context context.IContext) { //在ipc响应中获取当前的浏览器对象 info := cef.BrowserWindow.GetWindowInfo(context.BrowserId()) info.Chromium().SetCookie("https://www.example.com", "example_cookie_name", "1234", "", "/", true, true, false, time.Now(), time.Now(), time.Now(), consts.Ccss_CEF_COOKIE_SAME_SITE_UNSPECIFIED, consts.CEF_COOKIE_PRIORITY_MEDIUM, false, 0) info.Chromium().SetCookie("https://www.example.com", "example_cookie_name2", "123422", "", "/", true, true, false, time.Now(), time.Now(), time.Now(), consts.Ccss_CEF_COOKIE_SAME_SITE_UNSPECIFIED, consts.CEF_COOKIE_PRIORITY_MEDIUM, false, 0) info.Chromium().SetCookie("https://www.baidu.com", "demo_name", "4321", "", "/", true, true, false, time.Now(), time.Now(), time.Now(), consts.Ccss_CEF_COOKIE_SAME_SITE_NO_RESTRICTION, consts.CEF_COOKIE_PRIORITY_MEDIUM, false, 1) context.Result("执行成功,结果将在 SetOnCookieSet 事件中获得") }) //在SetBrowserInit中设置cookie事件,这些事件将返回操作后的结果 cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, browserWindow cef.IBrowserWindow) { //获取cookie时触发 event.SetOnCookiesVisited(func(sender lcl.IObject, cookie *cef.ICefCookie) { fmt.Printf("SetOnCookiesVisited: %+v\n", cookie) //将结果返回到html中 data, _ := json.Marshal(cookie) ipc.Emit("VisitCookieResult", string(data)) }) //删除cookie时触发 event.SetOnCookiesDeleted(func(sender lcl.IObject, numDeleted int32) { fmt.Printf("SetOnCookiesDeleted: %+v\n", numDeleted) }) //设置cookie时触发 event.SetOnCookieSet(func(sender lcl.IObject, success bool, ID int32) { fmt.Println("SetOnCookieSet: ", success, ID) }) event.SetOnCookiesFlushed(func(sender lcl.IObject) { fmt.Println("OnCookiesFlushed") }) event.SetOnCookieVisitorDestroyed(func(sender lcl.IObject, ID int32) { fmt.Println("OnCookieVisitorDestroyed") }) })
CookieManager 使用:
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) { window.Chromium().SetOnLoadEnd(func(sender lcl.IObject, browser *cef.ICefBrowser, frame *cef.ICefFrame, httpStatusCode int32) { manager := browser.GetRequestContext().GetCookieManager(nil) url := frame.Url() fmt.Println("manager:", manager, url) visitor := cef.CookieVisitorRef.New() visitor.SetOnVisit(func(cookie *cef.ICefCookie) (deleteCookie, result bool) { fmt.Printf("cookie: %+v\n", cookie) window.Chromium().SetCookie(url, cookie.Name, cookie.Value, cookie.Domain, cookie.Path, cookie.Secure, cookie.Httponly, cookie.HasExpires, cookie.Creation, cookie.LastAccess, cookie.Expires, cookie.SameSite, cookie.Priority, cookie.SetImmediately, cookie.ID) fmt.Println("\tFlushCookieStore:", window.Chromium().FlushCookieStore(true)) return false, true }) manager.VisitAllCookies(visitor) }) })