Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
energye
energy
提交
8a3b9564
energy
项目概览
energye
/
energy
通知
13
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
energy
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8a3b9564
编写于
6月 02, 2023
作者:
yanghye
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add: demo proxy
上级
c9ce867d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
145 addition
and
8 deletion
+145
-8
example/browser-proxy/proxy.go
example/browser-proxy/proxy.go
+95
-0
example/browser-proxy/resources/icon.ico
example/browser-proxy/resources/icon.ico
+0
-0
example/browser-proxy/resources/proxy.html
example/browser-proxy/resources/proxy.html
+42
-0
example/dev-test/ipc-event/ipc-event.go
example/dev-test/ipc-event/ipc-event.go
+8
-8
未找到文件。
example/browser-proxy/proxy.go
0 → 100644
浏览文件 @
8a3b9564
package
main
import
(
"embed"
"fmt"
"github.com/energye/energy/v2/cef"
"github.com/energye/energy/v2/consts"
"github.com/energye/golcl/lcl"
"strings"
)
//go:embed resources
var
resources
embed
.
FS
func
main
()
{
//全局初始化 每个应用都必须调用的
cef
.
GlobalInit
(
nil
,
&
resources
)
//创建应用
cefApp
:=
cef
.
NewApplication
()
//主窗口的配置
//指定一个URL地址,或本地html文件目录
cef
.
BrowserWindow
.
Config
.
Url
=
"https://energy.yanghy.cn"
cef
.
BrowserWindow
.
Config
.
IconFS
=
"resources/icon.ico"
cef
.
BrowserWindow
.
SetBrowserInit
(
func
(
event
*
cef
.
BrowserEvent
,
window
cef
.
IBrowserWindow
)
{
// 使用右键菜单切换需要代理的 url s
var
(
loadEnergyUrl
consts
.
MenuId
loadBaiduUrl
consts
.
MenuId
)
event
.
SetOnBeforeContextMenu
(
func
(
sender
lcl
.
IObject
,
browser
*
cef
.
ICefBrowser
,
frame
*
cef
.
ICefFrame
,
params
*
cef
.
ICefContextMenuParams
,
model
*
cef
.
ICefMenuModel
)
{
model
.
AddSeparator
()
loadEnergyUrl
=
model
.
CefMis
.
NextCommandId
()
model
.
AddCheckItem
(
loadEnergyUrl
,
"load-energy"
)
loadBaiduUrl
=
model
.
CefMis
.
NextCommandId
()
model
.
AddCheckItem
(
loadBaiduUrl
,
"load-baidu"
)
})
event
.
SetOnContextMenuCommand
(
func
(
sender
lcl
.
IObject
,
browser
*
cef
.
ICefBrowser
,
frame
*
cef
.
ICefFrame
,
params
*
cef
.
ICefContextMenuParams
,
commandId
consts
.
MenuId
,
eventFlags
uint32
,
result
*
bool
)
{
if
commandId
==
loadEnergyUrl
{
window
.
Chromium
()
.
LoadUrl
(
"https://energy.yanghy.cn"
)
}
else
if
commandId
==
loadBaiduUrl
{
window
.
Chromium
()
.
LoadUrl
(
"https://www.baidu.com"
)
}
})
// 使用 load start 和 load end 回调观察当前加载的url
event
.
SetOnLoadStart
(
func
(
sender
lcl
.
IObject
,
browser
*
cef
.
ICefBrowser
,
frame
*
cef
.
ICefFrame
,
transitionType
consts
.
TCefTransitionType
)
{
fmt
.
Println
(
"load-start:"
,
frame
.
Url
())
})
event
.
SetOnLoadEnd
(
func
(
sender
lcl
.
IObject
,
browser
*
cef
.
ICefBrowser
,
frame
*
cef
.
ICefFrame
,
httpStatusCode
int32
)
{
fmt
.
Println
(
"load-end:"
,
frame
.
Url
())
})
// 在 on before browser 配置代理
event
.
SetOnBeforeBrowser
(
func
(
sender
lcl
.
IObject
,
browser
*
cef
.
ICefBrowser
,
frame
*
cef
.
ICefFrame
,
request
*
cef
.
ICefRequest
,
userGesture
,
isRedirect
bool
)
bool
{
requestContext
:=
browser
.
GetRequestContext
()
if
strings
.
Index
(
request
.
URL
(),
"www.baidu.com"
)
!=
-
1
{
// 需要配置代理的 url
proxyDict
:=
cef
.
DictionaryValueRef
.
New
()
// 配置字典参数参考
//&cef.TCefProxy{
// ProxyType: consts.PtAutodetect,
// ProxyScheme: consts.PsSOCKS4,
// ProxyServer: "127.0.0.1",
// ProxyPort: 8888,
// ProxyUsername: "username",
// ProxyPassword: "password",
// ProxyScriptURL: "scriptURL",
// ProxyByPassList: "aaa,bbb,ddd",
// MaxConnectionsPerProxy: 100,
//}
proxyDict
.
SetString
(
"mode"
,
"fixed_servers"
)
proxyDict
.
SetString
(
"server"
,
"192.168.1.31:8888"
)
// 最后通过 Value 将代理字典值设置到 requestContext.SetPreference("proxy", proxy)
proxy
:=
cef
.
ValueRef
.
New
()
proxy
.
SetDictionary
(
proxyDict
)
errMsg
,
ok
:=
requestContext
.
SetPreference
(
"proxy"
,
proxy
)
fmt
.
Println
(
"
\t
proxy errMsg:"
,
errMsg
,
"ok:"
,
ok
)
}
else
{
// 不需要代理的 url value 设置 nil
errMsg
,
ok
:=
requestContext
.
SetPreference
(
"proxy"
,
nil
)
fmt
.
Println
(
"
\t
proxy errMsg:"
,
errMsg
,
"ok:"
,
ok
)
}
return
false
})
// 如果代理需要用户名和密码
window
.
Chromium
()
.
SetOnGetAuthCredentials
(
func
(
sender
lcl
.
IObject
,
browser
*
cef
.
ICefBrowser
,
originUrl
string
,
isProxy
bool
,
host
string
,
port
int32
,
realm
,
scheme
string
,
callback
*
cef
.
ICefAuthCallback
)
bool
{
fmt
.
Println
(
"AuthCredentials:"
,
originUrl
,
"isProxy:"
,
isProxy
)
if
isProxy
{
//callback.Cont("username","password")
return
true
}
return
false
})
})
//运行应用
cef
.
Run
(
cefApp
)
}
example/browser-proxy/resources/icon.ico
0 → 100644
浏览文件 @
8a3b9564
66.1 KB
example/browser-proxy/resources/proxy.html
0 → 100644
浏览文件 @
8a3b9564
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
browser-zoom
</title>
<style>
button
{
height
:
50px
;
width
:
100px
;
margin
:
10px
;
}
</style>
<script
type=
"application/javascript"
>
function
reset
()
{
ipc
.
emit
(
"
zoom-reset
"
);
}
function
dec
()
{
ipc
.
emit
(
"
zoom-dec
"
);
}
function
inc
()
{
ipc
.
emit
(
"
zoom-inc
"
);
}
</script>
</head>
<body
style=
"overflow: hidden;margin: 0px;padding: 0px;width: 100%;text-align: center;"
>
<button
onclick=
"inc()"
>
放大
</button>
<button
onclick=
"dec()"
>
缩小
</button>
<button
onclick=
"reset()"
>
重置
</button>
<div>
文字文字文字文字文字文字文字文字文字文字文字
</div>
<div>
文字文字文字文字文字文字文字文字文字文字文字
</div>
<div>
文字文字文字文字文字文字文字文字文字文字文字
</div>
<div>
文字文字文字文字文字文字文字文字文字文字文字
</div>
</body>
</html>
\ No newline at end of file
example/dev-test/ipc-event/ipc-event.go
浏览文件 @
8a3b9564
...
@@ -273,14 +273,14 @@ func main() {
...
@@ -273,14 +273,14 @@ func main() {
//fmt.Println("\tGetPageRanges:", pageRanges)
//fmt.Println("\tGetPageRanges:", pageRanges)
fmt
.
Println
(
"OnBeforeBrowser-Identifier:"
,
browser
.
Identifier
(),
"userGesture:"
,
userGesture
,
"isRedirect:"
,
isRedirect
)
fmt
.
Println
(
"OnBeforeBrowser-Identifier:"
,
browser
.
Identifier
(),
"userGesture:"
,
userGesture
,
"isRedirect:"
,
isRedirect
)
//
requestContext := browser.GetRequestContext()
requestContext
:=
browser
.
GetRequestContext
()
//
proxyDict := cef.DictionaryValueRef.New()
proxyDict
:=
cef
.
DictionaryValueRef
.
New
()
//
proxyDict.SetString("mode", "fixed_servers")
proxyDict
.
SetString
(
"mode"
,
"fixed_servers"
)
//
proxyDict.SetString("server", "192.168.1.31")
proxyDict
.
SetString
(
"server"
,
"192.168.1.31"
)
//
proxy := cef.ValueRef.New()
proxy
:=
cef
.
ValueRef
.
New
()
//
proxy.SetDictionary(proxyDict)
proxy
.
SetDictionary
(
proxyDict
)
//
errMsg, ok := requestContext.SetPreference("proxy", proxy)
errMsg
,
ok
:=
requestContext
.
SetPreference
(
"proxy"
,
proxy
)
//
fmt.Println("\tproxy errMsg:", errMsg, "ok:", ok)
fmt
.
Println
(
"
\t
proxy errMsg:"
,
errMsg
,
"ok:"
,
ok
)
//callbackRef := cef.CompletionCallbackRef.New()
//callbackRef := cef.CompletionCallbackRef.New()
//callbackRef.OnComplete(func() {
//callbackRef.OnComplete(func() {
// fmt.Println("callbackRef.OnComplete")
// fmt.Println("callbackRef.OnComplete")
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录