jobs-monorepo/services/scraper-google/utils/network.go
2025-12-16 17:19:44 +05:00

45 lines
1,016 B
Go

package utils
import (
"context"
"time"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
)
// WaitForNetworkIdle waits for the network to be idle (no pending requests)
// for the specified duration. This is useful for pages that load content dynamically.
func WaitForNetworkIdle(timeout time.Duration) chromedp.ActionFunc {
return func(ctx context.Context) error {
// Enable lifecycle events
if err := page.SetLifecycleEventsEnabled(true).Do(ctx); err != nil {
return err
}
ch := make(chan struct{}, 1)
cctx, cancel := context.WithCancel(ctx)
defer cancel()
chromedp.ListenTarget(cctx, func(ev interface{}) {
if e, ok := ev.(*page.EventLifecycleEvent); ok {
if e.Name == "networkIdle" {
select {
case ch <- struct{}{}:
default:
}
}
}
})
select {
case <-ch:
return nil
case <-time.After(timeout):
// Timeout reached, but don't error - page may still be usable
return nil
case <-ctx.Done():
return ctx.Err()
}
}
}