教程七 在Go中使用Energy创建跨平台GUI - Cookies

 教程-示例-文档 


介绍

本文介绍在energy中的cookie操作

在energy中可以对cookie的增加、修改和删除以达到某种目的

对cookie操作时,是以调用功能函数后触发事件的方式返回调用功能函数的结果

运行此示例,需要安装好Go和Energy开发环境:教程一 环境安装

此示例中采用了内置http服务访问内嵌资源: 内置http服务

功能函

  • VisitAllCookies 获取所有cookie
  • ​VisitURLCookies 获取指定URL cookie
  • DeleteCookies 删除cookie
  • SetCookie 设置cookie

事件函数

  • OnCookiesVisited 调用功能函数VisitURLCookies 和 VisitAllCookies 时触发
  • OnCookiesDeleted 调用功能函数 DeleteCookies 时触发
  • OnCookieSet 调用功能函数 SetCookie 时触发
  • OnCookieVisitorDestroyed 调用功能函数 VisitURLCookies 和 VisitAllCookies ,有销毁时触发

使用方式

在主进程中,使用事件机制监听,用于在html javascript中使用事件方式调用Go函数

ipc.IPC.Browser().SetOnEvent

在主进程主窗口初始化时设置事件监听

cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, browserWindow *cef.TCefWindowInfo) 

事件监听在Go中监听的事件, 用于在Go的其它进程或渲染(Render)进程(web html)中调用

event.On

在事件监听中获取主窗口信息

在主窗口对象中获得chromium对象
var info = cef.BrowserWindow.GetWindowInfo(context.BrowserId())

在javascript代码使用ipc.emit触发Go监听的事件

ipc.on 事件监听

ipc.emit 事件触

Go代码示例

package main

import (
	"embed"
	"encoding/json"
	"fmt"
	"github.com/energye/energy/cef"
	"github.com/energye/energy/common/assetserve"
	"github.com/energye/energy/consts"
	"github.com/energye/energy/ipc"
	"github.com/energye/golcl/lcl"
	"time"
)

//资源目录,内置到执行程序中
//go:embed resources
var resources embed.FS

//这个示例使用了几个事件来演示下载文件
func main() {
	//全局初始化 每个应用都必须调用的
	cef.GlobalCEFInit(nil, &resources)
	//创建应用
	cefApp := cef.NewApplication(nil)
	//主窗口的配置
	//指定一个URL地址,或本地html文件目录
	cef.BrowserWindow.Config.DefaultUrl = "http://localhost:22022/cookie.html"

	ipc.IPC.Browser().SetOnEvent(func(event ipc.IEventOn) {
		//监听获取cookie事件
		event.On("VisitCookie", func(context ipc.IIPCContext) {
			fmt.Println("VisitCookie")
			info := cef.BrowserWindow.GetWindowInfo(context.BrowserId())
			info.Chromium().VisitURLCookies("https://www.baidu.com", true, 1)
			info.Chromium().VisitAllCookies(1)
			context.Result().SetString("执行成功,结果将在 SetOnCookiesVisited 事件中获得")
		})
		//监听删除cookie
		event.On("DeleteCookie", func(context ipc.IIPCContext) {
			info := cef.BrowserWindow.GetWindowInfo(context.BrowserId())
			info.Chromium().DeleteCookies("", "", false)
			context.Result().SetString("执行成功,结果将在 SetOnCookiesDeleted 事件中获得")
		})
		//监听设置cookie
		event.On("SetCookie", func(context ipc.IIPCContext) {
			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().SetString("执行成功,结果将在 SetOnCookieSet 事件中获得")
		})
	})
	//在SetBrowserInit中设置cookie事件,这些事件将返回操作后的结果
	cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, browserWindow *cef.TCefWindowInfo) {
		//获取cookie时触发
		event.SetOnCookiesVisited(func(sender lcl.IObject, cookie *cef.ICefCookie) {
			fmt.Printf("SetOnCookiesVisited: %+v\n", cookie)
			//将结果返回到html中
			args := ipc.NewArgumentList()
			data, _ := json.Marshal(cookie)
			args.SetString(0, string(data), true)
			browserWindow.Chromium().Emit("VisitCookieResult", args, nil)
		})
		//删除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")
		})
	})
	//在主进程启动成功之后执行
	//在这里启动内置http服务
	//内置http服务需要使用 go:embed resources 内置资源到执行程序中
	cef.SetBrowserProcessStartAfterCallback(func(b bool) {
		fmt.Println("主进程启动 创建一个内置http服务")
		//通过内置http服务加载资源
		server := assetserve.NewAssetsHttpServer()
		server.PORT = 22022               //服务端口号
		server.AssetsFSName = "resources" //必须设置目录名和资源文件夹同名
		server.Assets = &resources
		go server.StartHttpServer()
	})
	//运行应用
	cef.Run(cefApp)
}

html代码示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>cookie</title>
    <style type="text/css">
        button {
            margin: 10px;
        }
    </style>
    <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>
</body>
</html>

示例效果图 - cookie

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanghye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值