Skip to content

Instantly share code, notes, and snippets.

@sorenlouv
Last active September 6, 2018 12:52
Show Gist options
  • Save sorenlouv/491b9377e3f540ba58ff630de0e829df to your computer and use it in GitHub Desktop.
Save sorenlouv/491b9377e3f540ba58ff630de0e829df to your computer and use it in GitHub Desktop.
Randomly click around a webpage
// Deprecated. Use puppeteer instead: https://gist.github.com/sqren/94ab2b9c5e88c1c119182425b19bcd59
const { Chromeless } = require('chromeless');
const initialUrl = process.env.CHROMELESS_URL || 'https://www.dr.dk';
const chromeless = new Chromeless({
launchChrome: true
});
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function goToUrl(url) {
return chromeless.goto(url).evaluate(() => {
const links = document.querySelectorAll('a[href^="/"]');
const link = links[Math.floor(Math.random() * links.length)];
return link.href;
});
}
async function run(url) {
console.log(`Opening ${url}`);
const nextUrl = await goToUrl(url);
await sleep(6000 + Math.floor(Math.random() * 10000));
return run(nextUrl);
}
run(initialUrl).catch(e => console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment