Puppeteer Browser Referer List

I would like to load random list of referers from my default location path , for example: 'referers.txt' instead of adding direct "facebook url as referer.

My code:

browser = await puppeteer.getBrowserInstance(port);
const page = await browser.newPage();
page.setDefaultTimeout(PAGE_DEFAULT_TIMEOUT * 1000);
page.on('error', handlePageCrash(page));
page.on('pageerror', handlePageCrash(page));
page.setExtraHTTPHeaders({ referer: ' });
1

1 Answer

Instead of a txt you should choose JSON to store your list of referer values in an array.

referers.json

["", "", ""]

Then you will be able to pick a random element form the array by: array[randomIndex]. To generate a random number for the length of your array you have multiple possibilities, Math.floor(Math.random() * array.length) only one of them.

referers.js

const puppeteer = require('puppeteer')
const referers = require('./referers.json')

async function fn() {
  const randomReferer = referers[Math.floor(Math.random() * referers.length)]
  console.log(referers)
  console.log(randomReferer)
  const browser = await puppeteer.launch({ headless: false, devtools: true })
  const page = await browser.newPage()

  page.setExtraHTTPHeaders({ referer: randomReferer })

  await page.goto(')
}
fn()

output example:

[
  '
  '
  '
]

6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest