Last active
January 3, 2024 19:16
-
-
Save mfyz/ce4e4c3e000160190246cdd32f0743aa to your computer and use it in GitHub Desktop.
Traffic generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install
npm install puppeteer
Run
node index.js
Runs forever until stopped. Beware of bot detection.