Skip to content

Instantly share code, notes, and snippets.

@mfyz
Last active January 3, 2024 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfyz/ce4e4c3e000160190246cdd32f0743aa to your computer and use it in GitHub Desktop.
Save mfyz/ce4e4c3e000160190246cdd32f0743aa to your computer and use it in GitHub Desktop.
Traffic generator
const puppeteer = require('puppeteer')
const BASE_URL = 'https://amazingsite.com'
const visitPage = async (browser, url) => {
const page = await browser.newPage()
console.log('--> Visiting', url)
await page.goto(url)
await page.waitForSelector('.site-logo')
const links = await page.evaluate(() => {
const anchors = Array.from(document.querySelectorAll('a[href]'));
return anchors.map(anchor => anchor.href)
// return anchors.map(anchor => anchor.href).filter(a => new URL(a.href).hostname === location.hostname) // only internal links
})
// console.log('--> links', links)
let randomLink = links[Math.floor(Math.random() * links.length)]
// 20% of the time go to homepage
const isHomepage = await page.evaluate(() => (location.pathname == '/'))
if (!isHomepage && Math.random() < 0.2) randomLink = BASE_URL
await page.close()
return randomLink
}
function wait(sec) {
return new Promise(resolve => setTimeout(resolve, sec * 1000));
}
(async () => {
const browser = await puppeteer.launch({
headless: 'new'
})
let nextUrl = BASE_URL
while (true) { // forever
nextUrl = await visitPage(browser, nextUrl)
const randomSecs = Math.round(Math.random() * 5)
console.log(` Random waiting for ${randomSecs} secs.`)
await wait(randomSecs)
}
})()
@mfyz
Copy link
Author

mfyz commented Jan 3, 2024

Install
npm install puppeteer

Run
node index.js

Runs forever until stopped. Beware of bot detection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment