Comments (5)
Please add a valid Rod Version: v0.0.0
to your issue. Current version is v0.115.0
generated by check-issue
from rod.
Try using browser Connect () initializes InitEvents, but it blocks the UI thread.
There won't be any problem with InitEvents directly
from rod.
How about use the cdp directly? Look at the code client.Event()
:
Lines 14 to 44 in 3557c23
from rod.
How about use the cdp directly? Look at the code
client.Event()
:Lines 14 to 44 in 3557c23
Hello, this method is not suitable.
I have switched to another method, using "go Browser.Connect()" instead of initEvents().
Because it directly passes a []byte, I need to know the id and method, and I don't want to deserialize again to obtain them. I am using the Chromium().ExecuteDevToolsMethod(messageId int32, method string, dictionaryValue *ICefDictionaryValue) function,
so I have replaced it with "go Browser.Connect()". Let's try this approach for now.
func (m *Energy) CreateBrowser() {
if !m.created {
m.created = true
go m.rodBrowser.Connect()
//m.rodBrowser.InitEvents()
if m.window != nil {
// window
if m.window.IsLCL() {
cef.RunOnMainThread(func() {
m.window.Show()
})
} else {
m.window.Show()
}
} else if m.chromiumBrowser != nil {
m.chromiumBrowser.CreateBrowser()
}
}
}
// Call a method and wait for its response.
func (m *Energy) Call(ctx context.Context, sessionID, method string, params interface{}) ([]byte, error) {
req := &cdp.Request{
ID: int(atomic.AddUint64(&m.count, 1)),
SessionID: sessionID,
Method: method,
Params: params,
}
m.logger.Println(req)
data, err := json.Marshal(params)
utils.E(err)
done := make(chan Result)
once := sync.Once{}
m.pending.Store(req.ID, func(res Result) {
once.Do(func() {
select {
case <-ctx.Done():
case done <- res:
}
})
})
defer m.pending.Delete(req.ID)
//m.logger.Println("send-data:", string(data))
//m.chromium.SendDevToolsMessage(string(data))// Linux cannot be used
dict := JSONParse(data)
m.ChromiumBrowser().Chromium().ExecuteDevToolsMethod(int32(req.ID), req.Method, dict)
defer dict.Free()
select {
case <-ctx.Done():
return nil, ctx.Err()
case res := <-done:
return res.Msg, res.Err
}
}
from rod.
You don't have to marshal it manually, you can use the proto lib:
// Package main ...
package main
import (
"fmt"
"github.com/go-rod/rod/lib/cdp"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
)
func main() {
// launch a browser
url := launcher.New().MustLaunch()
// create a controller
client := cdp.New().Start(cdp.MustConnectWS(url))
go func() {
for range client.Event() {
// you must consume the events
utils.Noop()
}
}()
res, err := proto.TargetCreateTarget{URL: "http://test.com"}.Call(client)
if err != nil {
panic(err)
}
fmt.Println(res.TargetID)
_ = proto.BrowserClose{}.Call(client)
}
from rod.
Related Issues (20)
- 并发page pool, context deadline exceeded,page为什么没有释放? HOT 2
- Add support for Linux Arm? for downloading etc. HOT 4
- page.Navigate(url), the page is like blocked HOT 1
- 测试时如何获取当前网页 HOT 5
- Click Button Eval Js Not Working HOT 4
- 获取点击后的response失败 HOT 2
- How to hide chromium command prompt HOT 2
- Element存在,但Element.MustHas却为false,检测不到Element HOT 2
- How to open a new Chrome window while using remote control HOT 4
- Using go-rod with firefox, failing to start new instance of firefox HOT 6
- How to disable the dialog when open External protocols HOT 2
- How to Set or replace response headers HOT 2
- Proxy and Hijack HOT 3
- Just a recipe to run a go-rod program as systemd service HOT 1
- How to Log CDP Logs to a File or Use slog HOT 2
- Failed to deserialize params.body - BINDINGS: binary value expected at position 61 HOT 5
- failed page screenshot in defer HOT 2
- launcher docker 是否支持常驻且直接连接 HOT 4
- Examples fail on Ubuntu 24.04 with "No usable sandbox!" HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rod.