mirror of
https://gitee.com/energye/energy.git
synced 2024-11-29 18:28:06 +08:00
U: Demo dev_tools_method
This commit is contained in:
parent
6887fe6b1c
commit
4ed6668c7c
19
examples/common/utils/databin.go
Normal file
19
examples/common/utils/databin.go
Normal file
@ -0,0 +1,19 @@
|
||||
// 示例工具
|
||||
|
||||
package utils
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// ReadData 读取指针数据到 []byte
|
||||
func ReadData(data uintptr, count uint32) []byte {
|
||||
result := make([]byte, count, count)
|
||||
// 表示下一个字节指针
|
||||
var n uint32 = 0
|
||||
for n < count {
|
||||
// 取出值
|
||||
result[n] = *(*byte)(unsafe.Pointer(data + uintptr(n)))
|
||||
// 下一个字节指针
|
||||
n = n + 1
|
||||
}
|
||||
return result
|
||||
}
|
@ -7,7 +7,9 @@ import (
|
||||
"github.com/energye/energy/v2/cef/ipc"
|
||||
"github.com/energye/energy/v2/cef/ipc/context"
|
||||
"github.com/energye/energy/v2/consts"
|
||||
"github.com/energye/energy/v2/examples/common/utils"
|
||||
"github.com/energye/energy/v2/pkgs/assetserve"
|
||||
"github.com/energye/golcl/lcl"
|
||||
)
|
||||
|
||||
// 资源目录,内置到执行程序中
|
||||
@ -24,41 +26,52 @@ func main() {
|
||||
//主窗口的配置
|
||||
//指定一个URL地址,或本地html文件目录
|
||||
cef.BrowserWindow.Config.Url = "http://localhost:22022/execute-dev-tool-method.html"
|
||||
//chromium配置
|
||||
//config := cef.NewChromiumConfig()
|
||||
//config.SetEnableMenu(true) //启用右键菜单
|
||||
//config.SetEnableDevTools(true) //启用开发者工具
|
||||
//cef.BrowserWindow.Config.SetChromiumConfig(config)
|
||||
//这里演示使用ipc通信实现js和go互相调用,在go监听事件中执行开发者工具方法
|
||||
//使用内置http服务和自定义页面
|
||||
//这里执行的方法是仿真移动端
|
||||
//1. js使用ipc.emit触发 go事件
|
||||
//2. go中"execute-dev-method"事件执行,通过context获得browserId
|
||||
//3. 通过browserId获得chromium
|
||||
//4. 使用字典对象传递方法参数
|
||||
//5. 点击Note链接
|
||||
cef.BrowserWindow.Config.ChromiumConfig().SetEnableDevTools(false)
|
||||
|
||||
ipc.On("execute-dev-method", func(context context.IContext) {
|
||||
//获得当前窗口信息
|
||||
info := cef.BrowserWindow.GetWindowInfo(context.BrowserId())
|
||||
//字典对象
|
||||
var dict = cef.DictionaryValueRef.New() // cef.NewCefDictionaryValue()
|
||||
//根据chromium字典设置
|
||||
//dict.SetInt("width", 500)
|
||||
//dict.SetInt("height", 768)
|
||||
//dict.SetInt("x", 100)
|
||||
//dict.SetInt("y", 100)
|
||||
dict.SetBool("mobile", true)
|
||||
dict.SetDouble("deviceScaleFactor", 1)
|
||||
TempDict := cef.DictionaryValueRef.New()
|
||||
TempDict.SetString("type", "portraitPrimary")
|
||||
TempDict.SetInt("angle", 0)
|
||||
dict.SetDictionary("screenOrientation", TempDict)
|
||||
info.Chromium().ExecuteDevToolsMethod(0, "Emulation.setDeviceMetricsOverride", dict)
|
||||
//设置浏览器 userAgent
|
||||
dict = cef.DictionaryValueRef.New()
|
||||
dict.SetString("userAgent", "Mozilla/5.0 (Linux; Android 11; M2102K1G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Mobile Safari/537.36")
|
||||
info.Chromium().ExecuteDevToolsMethod(0, "Emulation.setUserAgentOverride", dict)
|
||||
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
|
||||
//这里演示使用ipc通信实现js和go互相调用,在go监听事件中执行开发者工具方法
|
||||
//使用内置http服务和自定义页面
|
||||
//这里执行的方法是仿真移动端
|
||||
//1. js使用ipc.emit触发 go事件
|
||||
//2. go中"execute-dev-method"事件执行,通过context获得browserId
|
||||
//3. 通过browserId获得chromium
|
||||
//4. 使用字典对象传递方法参数
|
||||
//5. 点击Note链接
|
||||
|
||||
// 全局变量,自动递增的消息ID
|
||||
var gId int32 = 0
|
||||
ipc.On("execute-dev-method", func(context context.IContext) {
|
||||
//获得当前窗口信息
|
||||
info := cef.BrowserWindow.GetWindowInfo(context.BrowserId())
|
||||
//字典对象
|
||||
var dict = cef.DictionaryValueRef.New() // cef.NewCefDictionaryValue()
|
||||
//根据chromium字典设置
|
||||
//dict.SetInt("width", 500)
|
||||
//dict.SetInt("height", 768)
|
||||
//dict.SetInt("x", 100)
|
||||
//dict.SetInt("y", 100)
|
||||
dict.SetBool("mobile", true)
|
||||
dict.SetDouble("deviceScaleFactor", 1)
|
||||
TempDict := cef.DictionaryValueRef.New()
|
||||
TempDict.SetString("type", "portraitPrimary")
|
||||
TempDict.SetInt("angle", 0)
|
||||
dict.SetDictionary("screenOrientation", TempDict)
|
||||
gId = info.Chromium().ExecuteDevToolsMethod(gId, "Emulation.setDeviceMetricsOverride", dict)
|
||||
fmt.Println("ExecuteDevToolsMethod - result messageId:", gId)
|
||||
//设置浏览器 userAgent
|
||||
dict = cef.DictionaryValueRef.New()
|
||||
dict.SetString("userAgent", "Mozilla/5.0 (Linux; Android 11; M2102K1G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Mobile Safari/537.36")
|
||||
gId = info.Chromium().ExecuteDevToolsMethod(gId, "Emulation.setUserAgentOverride", dict)
|
||||
fmt.Println("ExecuteDevToolsMethod - result messageId:", gId)
|
||||
fmt.Println()
|
||||
})
|
||||
// 使用 DevToolsRawMessage 处理,方便些
|
||||
window.Chromium().SetOnDevToolsRawMessage(func(sender lcl.IObject, browser *cef.ICefBrowser, message uintptr, messageSize uint32) (handled bool) {
|
||||
fmt.Println("OnDevToolsRawMessage message:", message, messageSize)
|
||||
data := utils.ReadData(message, messageSize)
|
||||
fmt.Println("data:", string(data))
|
||||
return false
|
||||
})
|
||||
})
|
||||
|
||||
//在主进程启动成功之后执行
|
||||
|
Loading…
Reference in New Issue
Block a user