Skip to content

Instantly share code, notes, and snippets.

@nelsonsilva
Created September 17, 2019 21:55
Show Gist options
  • Select an option

  • Save nelsonsilva/8e70ef657d62da36a84ab2d22966cf0a to your computer and use it in GitHub Desktop.

Select an option

Save nelsonsilva/8e70ef657d62da36a84ab2d22966cf0a to your computer and use it in GitHub Desktop.
// Time between pages
const TIMEOUT = 5 * 1000;
// https://chromedevtools.github.io/devtools-protocol/
const BASE = 'http://localhost:9222';
const open = url => fetch(`${BASE}/json/new?${url}`).then(r => r.json());
const activate = target => fetch(`${BASE}/json/activate/${target.id}`);
const ws = target => new Promise(resolve => (new WebSocket(target.webSocketDebuggerUrl).onopen = e => resolve(e.target)));
const reload = target => ws(target).then(c => c.send(JSON.stringify({id: 0, method: 'Page.reload'})));
const timeout = (ms) => new Promise(resolve => setTimeout(() => resolve(), ms));
(async () => {
const TARGETS = await Promise.all(URLs.map(open));
for (let i = 0; ; i = (i + 1) % TARGETS.length) {
// activate next page
await activate(TARGETS[i]);
// refresh upcoming page in the background
reload(TARGETS[(i + 1) % TARGETS.length]);
// wait
await timeout(TIMEOUT);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment