Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tpkn/pptr-spoofer
Replace page requests in Puppeteer
https://github.com/tpkn/pptr-spoofer
Last synced: 6 days ago
JSON representation
Replace page requests in Puppeteer
- Host: GitHub
- URL: https://github.com/tpkn/pptr-spoofer
- Owner: tpkn
- License: mit
- Created: 2018-11-26T20:51:35.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-07-16T13:43:57.000Z (over 5 years ago)
- Last Synced: 2024-12-14T16:09:59.079Z (about 1 month ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Puppeteer Spoofer [![npm Package](https://img.shields.io/npm/v/pptr-spoofer.svg)](https://www.npmjs.org/package/pptr-spoofer)
Replace page requests in PuppeteerLet's say you are testing some page. And that page loads tons of heavy libs from slow remote server each time. Tedious process, huh? With this module you can respond with content of that libraries, loaded from your local drive.
In case you have the CORS error, disable web security (at your own risk):
```javascript
puppeteer.launch({ args: [ '--disable-web-security' ] });
```## Installation
```bash
npm install pptr-spoofer
```## API
### PptrSpoofer(req, rules)
### req
**Type**: _Object_
Page `request` object### rules
**Type**: _Array_
```
[
{
rule: '',
body: '',
status: 200,
contentType: 'text/plane'
}
]
```
- `rule` _String_ or _RegExp_ of the request url that should be replaced
- `body` _String_ or _Buffer_ of the response body content, **Default:** `''`
- `status` _Number_, **Default:** `200`
- `contentType` _String_, **Default:** `'text/plane'`## Usage
```javascript
const puppeteer = require('puppeteer');
const spoofer = require('pptr-spoofer');let browser = await puppeteer.launch({ headless: 0, devtools: 1, args: ['--disable-web-security'] });
let pages = await browser.pages();
let page = pages[0];await page.setRequestInterception(true);
await page.bringToFront();page.on('request', req => {
spoofer(req, [
{
rule: 'https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.8.2/pixi.js',
body: fs.readFileSync('./libs/pixi.js'),
status: 200,
contentType: 'text/plain'
}
]);
});await page.goto('http://localhost:12345/test', { waitUntil: 'networkidle2', timeout: 45000 });
```