Chromedp: how to disable images load

Created on 5 Dec 2018  路  4Comments  路  Source: chromedp/chromedp

Hi, need help how to disable images load

Most helpful comment

chromedp.Flag("blink-settings", "imagesEnabled=false")

All 4 comments

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")

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aymericbeaumet picture aymericbeaumet  路  5Comments

t-blake picture t-blake  路  3Comments

orlangure picture orlangure  路  3Comments

shailendrayadav1 picture shailendrayadav1  路  4Comments

mschilli picture mschilli  路  4Comments