Re: Best / any way to use addBlockedURL?

169 views
Skip to first unread message

Alex Clarke

unread,
Feb 15, 2017, 6:07:25 AM2/15/17
to els...@gmail.com, headless-dev
Hi

The underlying URL blocking mechanism appears to be working with headless chrome. I'm going to land a test https://codereview.chromium.org/2695923006/

Re your example, I wonder if the Network.addBlockedURL sent by the Network.requestWillBeSent handler arrives in time to be effective?

On 15 February 2017 at 01:35, <els...@gmail.com> wrote:
I'm trying to write a script to leverage the screenshot capability in headless_shell.

I tried the following, using a recent build (I think) of headless_shell (https://hub.docker.com/r/yukinying/chrome-headless/) along with a node library (https://github.com/cyrus-and/chrome-remote-interface) - but looking at my logs it doesn't seem that anything is being actually blocked - either by my explicit addBlockedURL line or in the one inside requestWillBeSent.

Any guesses?

```

const CDP = require('chrome-remote-interface');
const fs = require('fs');

const blacklistURLs = [
  'facebook',
  'fullstory',
  'getdrip',
  'olark',
];

CDP(client => {
  // extract domains
  const { Network, Page } = client;
  // setup handlers


  Network.requestWillBeSent(params => {
    const url = params.request.url;
    console.log(`-> ${params.requestId} ${url.substring(0, 150)}`);
    blacklistURLs.some(blacklistedURL => {
      if (url.indexOf(blacklistedURL) !== -1) {
        Network.addBlockedURL(url);
        console.log('BLOCKING', url, '(I hope)');
        return true;
      }
      return false;
    });
  });
  Network.loadingFailed(params => {
    console.log('*** loadingFailed: ', params.requestId);
  })
  Network.loadingFinished(params => {
    console.log('<-', params.requestId, params.encodedDataLength);
  })
  Page.loadEventFired(() => {
    console.log('loadEventFired!');

    Page.captureScreenshot().then(v => {
      let filename = `screenshot-${Date.now()}`;
      fs.writeFileSync(filename + '.png', v.data, 'base64');
      console.log(`Image saved as ${filename}.png`);
      client.close();
    });
  });
  // enable events then start!
  Promise.all([
    Network.enable(),
    Page.enable()
  ]).then(
    () => {
      return Page.navigate({ url: 'https://shift.com/cars/san-francisco' });
    },
    () => {
      console.log('REJECT');
      client.close();
    }
  );
}).on('error', (err) => {
  console.error('Cannot connect to remote endpoint:', err);
});
```

--
You received this message because you are subscribed to the Google Groups "headless-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to headless-dev+unsubscribe@chromium.org.
To post to this group, send email to headle...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/headless-dev/1edffe3b-cf68-4f49-8695-ba51a60f6462%40chromium.org.

Reply all
Reply to author
Forward
0 new messages