Search code examples
node.jspuppeteerwebautomation

Disable downloads in Puppeteer


Is it possible to disable downloads for specific mime type. For example navigating to an url that by default will download a zip archive, should do nothing.

Edit:

I don't know in advance what url will be visited and what will return that url.


Solution

  • Solution that worked for me:

    const client = await page.target().createCDPSession();
    
    // intercept request when response headers was received
    await client.send('Network.setRequestInterception', {
      patterns: [{
          urlPattern: '*',
          resourceType: 'Document',
          interceptionStage: 'HeadersReceived'
      }],
    });
    
    await client.on('Network.requestIntercepted', async e => {
        let headers = e.responseHeaders || {};
        let contentType = headers['content-type'] || headers['Content-Type'] || '';
        let obj = {interceptionId: e.interceptionId};
        if (contentType.indexOf('application/zip') > -1) {
            obj['errorReason'] = 'BlockedByClient';
        }
    
        await client.send('Network.continueInterceptedRequest', obj);
    });
    

    This piece of code blocks navigation to an url that will download a zip archive and pass the rest of requests.

    Thanks to @hardkoded for suggestion to visit github.com/GoogleChrome/puppeteer/issues/1191.