Comments (1)
I used a less elegant implementation here. Please let me know if there is a better way later.
example.go
func examples(){
// Omit the content of the creation context.
ctx, cancel := .......
chromedp.ListenTarget(ctx, func(ev interface{}) {
switch ev := ev.(type) {
case *runtime.EventExecutionContextCreated:
data := EventExecutionContextAuxData{}
json.Unmarshal(ev.Context.AuxData, &data)
if data.FrameId != "" {
GlobalExecutionContextPool.Push(data.FrameId, ev.Context.UniqueID)
}
case *runtime.EventExecutionContextDestroyed:
GlobalExecutionContextPool.RemoveByUniqueId(ev.ExecutionContextUniqueID)
case *page.EventFrameNavigated:
// This is my method for executing JavaScript after the frame is navigated, and matching UniqueID through frameId.
go onTargetNavigated(ev, ctx)
})
}
func onTargetNavigated(ev *page.EventFrameNavigated, ctx context.Context){
if strings.HasPrefix(ev.Frame.URL, "http") {
var err error
if ev.Frame.ParentID == "" {
// Parent Page/Non-iframe
} else {
uniqueId := ""
deadline := time.Now().Add(5 * time.Second)
for uniqueId == "" {
time.Sleep(10 * time.Millisecond)
uniqueId = GlobalExecutionContextPool.Find(ev.Frame.ID.String())
if time.Now().UnixMilli() > deadline.UnixMilli() {
break
}
}
if uniqueId == "" {
log.Println("cannot match frame execution context")
return
}
err = chromedp.Run(
ctx,
chromedp.EvaluateAsDevTools(
"alert('current page is ' + location.href)", // this is the script
nil,
func(ep *runtime.EvaluateParams) *runtime.EvaluateParams {
return ep.WithUniqueContextID(uniqueId)
},
),
)
}
}
}
type ExecutionContextPool struct {
pool map[string]string
locker sync.RWMutex
}
var GlobalExecutionContextPool ExecutionContextPool = ExecutionContextPool{
pool: map[string]string{},
}
func (s *ExecutionContextPool) Push(frameId string, uniqueId string) {
log.Println("ExecutionContextPool.Push:", frameId, uniqueId)
// s.pool.Store(frameId, uniqueId)
s.locker.Lock()
defer s.locker.Unlock()
s.pool[frameId] = uniqueId
}
func (s *ExecutionContextPool) Find(frameId string) string {
s.locker.RLock()
defer s.locker.RUnlock()
if val, ok := s.pool[frameId]; ok {
return val
}
return ""
}
func (s *ExecutionContextPool) RemoveByFrameId(frameId string) {
s.locker.Lock()
defer s.locker.Unlock()
delete(s.pool, frameId)
}
func (s *ExecutionContextPool) RemoveByUniqueId(uniqueId string) {
s.locker.Lock()
defer s.locker.Unlock()
for k, v := range s.pool {
if v == uniqueId {
log.Println("ExecutionContextPool.RemoveByUniqueId", k, v)
delete(s.pool, k)
return
}
}
}
from chromedp.
Related Issues (20)
- when use chromedp.Evaluate, how can I get the promise error info ?
- Navigate Hangup with custom url scheme HOT 1
- page.StopLoading() cannot stop navigate
- chrome failed to start with no detail error
- Screenshot from remote browser
- context canceled even with new context HOT 1
- Download events being omitted on the page level but chromedp listens for it on the Browser level HOT 1
- Image not showing up in header
- Can't use proxy and open multiple tabs ?
- Target.targetCrashed > errorCode 11 with chromedp.Navigate() in Docker container environnement HOT 7
- Is it possible to use the net/http client in chromedp ? HOT 1
- GetOuterHTML().WithPierce(true) not returning <iframe> contents
- How to set the state of ShadowDOM from closed to open?
- Question: condition for set FooterTemplate
- Is it possible to capture error messages related to CORS, CSP violations, mixed-content violations, etc.?
- Can I Capture Raw HTTP Data?
- How to start chrome in arm environment?Are there any other plans?
- Why can't I listen to my iframe's network requests
- How i get dpi of chrome
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 chromedp.