golang+webview获取cookies
package main
import (
"fmt"
"github.com/zserge/webview"
)
func main() {
// 创建Web视图对象
w := webview.New(true) // true表示以无头模式运行(不显示界面)
defer w.Destroy() // 程序退出时关闭WebView
// 设置页面加载完成后的处理函数
w.SetTitle("Get Cookie Example")
w.Navigate("https://example.com")
w.Bind("getCookies", func() string {
cookies := make([]string, len(w.Eval(`document.cookie`).String()))
for i, cookie := range strings.Split(w.Eval(`document.cookie`).String(), "; ") {
if parts := strings.Split(cookie, "="); len(parts) == 2 {
cookies[i] = fmt.Sprintf("%s=%s", parts[0], parts[1])
} else if len(parts) > 2 {
cookies[i] = fmt.Sprintf("%s=%s", parts[0], strings.Join(parts[1:], "="))
}
}
return strings.Join(cookies, "\n")
})
// 等待WebView加载完成并触发事件
select {}
}