GithubHelp home page GithubHelp logo

candicandi / puppeteer-page-proxy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cuadrix/puppeteer-page-proxy

0.0 0.0 0.0 61 KB

Additional module to use with 'puppeteer' for setting proxies per page basis.

JavaScript 100.00%

puppeteer-page-proxy's Introduction

puppeteer-page-proxy

Additional Node.js module to use with puppeteer for setting proxies per page basis.

Forwards intercepted requests from the browser to Node.js where it handles the request then returns the response to the browser, changing the proxy as a result.

Features

  • Proxy per page and per request
  • Supports ( http, https, socks4, socks5 ) proxies
  • Authentication
  • Cookie handling internally

Installation

npm i puppeteer-page-proxy

API

PageProxy(pageOrReq, proxy)

  • pageOrReq <object> 'Page' or 'Request' object to set a proxy for.
  • proxy <string> Proxy to use in the current page.
    • Begins with a protocol (e.g. http://, https://, socks://)

PageProxy.lookup(page[, lookupService, isJSON, timeout])

  • page <object> 'Page' object to execute the request on.
  • lookupService <string> External lookup service to request data from.
    • Fetches data from api.ipify.org by default.
  • isJSON <boolean> Whether to JSON.parse the received response.
    • Defaults to true.
  • timeout <number|string> Time in milliseconds after which the request times out.
    • Defaults to 30000.
  • returns: <Promise> Promise which resolves to the response of the lookup request.

NOTE: By default this method expects a response in JSON format and JSON.parse's it to a usable javascript object. To disable this functionality, set isJSON to false.

Examples

Proxy per page:

const puppeteer = require('puppeteer');
const useProxy = require('puppeteer-page-proxy');

(async () => {
    const site = 'https://example.com';
    const proxy = 'http://host:port';
    const proxy2 = 'https://host:port';
    
    const browser = await puppeteer.launch({headless: false});

    const page = await browser.newPage();
    await useProxy(page, proxy);
    await page.goto(site);

    const page2 = await browser.newPage();
    await useProxy(page2, proxy2);
    await page2.goto(site);
})();

To remove a proxy set this way, simply pass a falsy value (e.g null) instead of the proxy;

await useProxy(page, null);

Proxy per request:

const puppeteer = require('puppeteer');
const useProxy = require('puppeteer-page-proxy');

(async () => {
    const site = 'https://example.com';
    const proxy = 'socks://host:port';

    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();

    await page.setRequestInterception(true);
    page.on('request', req => {
        useProxy(req, proxy); // 'req' as argument
    });
    await page.goto(site);
})();

When changing proxies this way, the request object itself is passed as the first argument. Now 'proxy' can be changed every request. Leaving it as is will have the same effect as useProxy(page, proxy), meaning that the same proxy will be used for all requests within the page.

Using it in other request listeners is also straight forward:

await page.setRequestInterception(true);
page.on('request', req => {
    if (req.resourceType() === 'image') {
        req.abort();
    } else {
        useProxy(req, proxy);
    }
});

Since all requests can be handled exactly once, it's not possible to call other interception methods (e.g. request.abort, request.continue) after calling useProxy, without getting a 'Request is already handled!' error message. This is because puppeteer-page-proxy internally calls request.respond which fulfills the request.

NOTE: It is necessary to set page.setRequestInterception to true when setting proxies this way, otherwise the function will fail.

Authentication:

const proxy = 'https://login:pass@host:port';

Lookup IP used by proxy:

const puppeteer = require('puppeteer');
const useProxy = require('puppeteer-page-proxy');

(async () => {
    const site = 'https://example.com';
    const proxy1 = 'http://host:port';
    const proxy2 = 'https://host:port';
    
    const browser = await puppeteer.launch({headless: false});

    /**1*/
    const page1 = await browser.newPage();
    await useProxy(page1, proxy1);
    let data = await useProxy.lookup(page1); // Waits until done, 'then' continues
        console.log(data.ip);
    await page1.goto(site);
    
    /**2*/
    const page2 = await browser.newPage();
    await useProxy(page2, proxy2);
    useProxy.lookup(page2).then(data => {   // Executes and 'comes back' once done
        console.log(data.ip);
    });
    await page2.goto(site);
})();

Dependencies

puppeteer-page-proxy's People

Contributors

cuadrix avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.