Skip to content

Instantly share code, notes, and snippets.

@Mon-ius
Forked from jancurn/proxy-chain-example.js
Created February 29, 2024 07:26
Show Gist options
  • Save Mon-ius/058140e68cc216b01e5fb43136a75483 to your computer and use it in GitHub Desktop.
Save Mon-ius/058140e68cc216b01e5fb43136a75483 to your computer and use it in GitHub Desktop.
Example showing how to use the proxy-chain NPM package to let headless Chrome use a proxy server with username and password
const puppeteer = require('puppeteer');
const proxyChain = require('proxy-chain');
(async() => {
const oldProxyUrl = 'http://bob:password123@proxy.example.com:8000';
const newProxyUrl = await proxyChain.anonymizeProxy(oldProxyUrl);
// Prints something like "http://127.0.0.1:45678"
console.log(newProxyUrl);
const browser = await puppeteer.launch({
args: [`--proxy-server=${newProxyUrl}`],
});
// Do your magic here...
const page = await browser.newPage();
await page.goto('https://www.example.com');
await page.screenshot({ path: 'example.png' });
await browser.close();
// Clean up, forcibly close all pending connections
await proxyChain.closeAnonymizedProxy(newProxyUrl, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment