I would like to load random list of user-agents from my default location path , for example: \'agents.json\' instead of adding direct and only 1 user-agent.
agent
You seem to redeclare browser when you set the UA, which you shouldn't. Also you don't need to set it as an object: as page.setUserAgent accept a string: page.setUserAgent(userAgent)
.
await page.setUserAgent(randomAgent);
will be just fine.
const browser = await puppeteer.getBrowserInstance(port);
const randomReferer = referers[Math.floor(Math.random() * referers.length)];
const randomAgent = agents[Math.floor(Math.random() * agents.length)]; // made it singular instead of plural
const page = await browser.newPage();
await page.setUserAgent(randomAgent); // like this
page.setDefaultTimeout(PAGE_DEFAULT_TIMEOUT * 1000);
page.on('error', handlePageCrash(page));
page.on('pageerror', handlePageCrash(page));
page.setExtraHTTPHeaders({ referer: randomReferer });
Note: I think it is only a mistake in your post, but you also have two const named referers
, this one should be the agents
:
const referers = require('../core/agents.json');