Hi, need help how to disable images load
good question,
1.step
network.enable()
2.step
Network.SetRequestInterception([]*network.RequestPattern{
&network.RequestPattern{
URLPattern:"*",ResourceType:"Image",
},
})
3.step
wait Network.requestIntercepted then do
network.ContinueInterceptedRequest(InterceptionID).WithErrorReason("Aborted")
hope u like it
https://github.com/m4p1e/chromedp
if u are interesting check my fork锛寀 can do like that
func task(ctx context.Context)cdp.Tasks{
return cdp.Tasks{
network.SetRequestInterception([]*network.RequestPattern{
&network.RequestPattern{
URLPattern:"*",ResourceType:"Image",
},
}),
cdp.CallbackFunc("Network.requestIntercepted", func(param interface{},handler *cdp.TargetHandler){
network.ContinueInterceptedRequest(data.InterceptionID).WithErrorReason("Aborted")).Do(ctx,handler)
}),
cdp.Navigate(`https://www.somesite.com`),
cdp.Sleep(10* time.Second),
}
}
Thanks, I just try to tell chrome to disable images, disable images in network level is a good idea.
func DisableImageLoad(ctx context.Context) func(event interface{}) {
return func(event interface{}) {
switch ev := event.(type) {
case *fetch.EventRequestPaused:
go func() {
c := chromedp.FromContext(ctx)
ctx := cdp.WithExecutor(ctx, c.Target)
if ev.ResourceType == network.ResourceTypeImage {
fetch.FailRequest(ev.RequestID, network.ErrorReasonBlockedByClient).Do(ctx)
} else {
fetch.ContinueRequest(ev.RequestID).Do(ctx)
}
}()
}
}
}
.....
ctx, _ = chromedp.NewContext(ctx)
chromedp.ListenTarget(ctx, DisableImageLoad(ctx))
if err := chromedp.Run(
ctx,
fetch.Enable(),
chromedp.Navigate("about:blank"),
); err != nil {
return nil, errors.WithStack(err)
}
...
chromedp.Flag("blink-settings", "imagesEnabled=false")
Most helpful comment
chromedp.Flag("blink-settings", "imagesEnabled=false")