Skip to content

Commit b836d54

Browse files
committedNov 28, 2017
make run function accepts url as parameter and trackResponses, getCookies as optional parameter
1 parent 52351d0 commit b836d54

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed
 

‎README.md

+22
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ If you want to use latest chrome, run chrome/buildChrome.sh on EC2 having at lea
4141
See also [serverless-chrome](https://github.com/adieuadieu/serverless-chrome/blob/master/chrome/README.md).
4242
Once you build it, link to `headless_shell.tar.gz` in `chrome` dir.
4343

44+
## Deploying to AWS
45+
The best way to deploy right now is to upload package.zip file to S3 and set the link to Lambda.
46+
47+
Testing in Lambda:
48+
49+
- Normal request
50+
```
51+
{
52+
"url": "https://google.com",
53+
"trackResponses": false,
54+
"getCookies": false,
55+
}
56+
```
57+
- Request with XHR responses and cookies (shopee.vn is SPA written in React)
58+
```
59+
{
60+
"url": "https://shopee.vn/Astaxanthin-i.25321200.413097508",
61+
"trackResponses": true,
62+
"getCookies": true,
63+
}
64+
```
65+
4466
## Article
4567

4668
[Lambda上でPuppeteer/Headless Chromeを動かすStarter Kitを作った - sambaiz-net](https://www.sambaiz.net/article/132/)

‎src/index.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
const setup = require('./starter-kit/setup');
2+
const urlParser = require('url');
23

34
exports.handler = async (event, context, callback) => {
45
// For keeping the browser launch
56
context.callbackWaitsForEmptyEventLoop = false;
67
const browser = await setup.getBrowser();
7-
exports.run(browser).then(
8+
exports.run(event.url, browser, event.trackResponses, event.getCookies).then(
89
(result) => callback(null, result)
910
).catch(
1011
(err) => callback(err)
1112
);
1213
};
1314

14-
exports.run = async (browser) => {
15+
exports.run = async (url, browser
16+
, trackResponses = false, getCookies = false) => {
1517
const page = await browser.newPage();
16-
await page.goto('https://www.google.co.jp');
18+
let responses = {};
19+
if (trackResponses) {
20+
const parsedUrl = urlParser.parse(url);
21+
page.on('response', async (response) => {
22+
// make sure only tracks response from same host
23+
if (response.url.indexOf(parsedUrl.hostname) !== -1) {
24+
responses[response.url] = await response.text();
25+
}
26+
});
27+
}
28+
await page.goto(url);
29+
const html = await page.content();
30+
const cookies = getCookies ? await page.cookies() : '';
1731
await page.close();
18-
return 'done';
32+
return {html, responses, cookies};
1933
};

‎src/starter-kit/local.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const puppeteer = require('puppeteer');
99
dumpio: !!config.DEBUG,
1010
// use chrome installed by puppeteer
1111
});
12-
await index.run(browser)
12+
await index.run('https://google.com', browser, false, false)
1313
.then((result) => console.log(result))
1414
.catch((err) => console.error(err));
1515
await browser.close();

‎yarn.lock

+2-10
Original file line numberDiff line numberDiff line change
@@ -2039,14 +2039,10 @@ safe-buffer@^5.0.1, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
20392039
version "5.1.1"
20402040
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"
20412041

2042-
sax@1.2.1:
2042+
sax@1.2.1, sax@>=0.6.0:
20432043
version "1.2.1"
20442044
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.1.tgz#7b8e656190b228e81a66aea748480d828cd2d37a"
20452045

2046-
sax@>=0.6.0:
2047-
version "1.2.4"
2048-
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
2049-
20502046
semver@^5.3.0:
20512047
version "5.4.1"
20522048
resolved "https://registry.yarnpkg.com/semver/-/semver-5.4.1.tgz#e059c09d8571f0540823733433505d3a2f00b18e"
@@ -2283,14 +2279,10 @@ util-deprecate@~1.0.1:
22832279
version "1.0.2"
22842280
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
22852281

2286-
uuid@3.0.1:
2282+
uuid@3.0.1, uuid@^3.0.0:
22872283
version "3.0.1"
22882284
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.0.1.tgz#6544bba2dfda8c1cf17e629a3a305e2bb1fee6c1"
22892285

2290-
uuid@^3.0.0:
2291-
version "3.1.0"
2292-
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.1.0.tgz#3dd3d3e790abc24d7b0d3a034ffababe28ebbc04"
2293-
22942286
v8flags@^2.1.1:
22952287
version "2.1.1"
22962288
resolved "https://registry.yarnpkg.com/v8flags/-/v8flags-2.1.1.tgz#aab1a1fa30d45f88dd321148875ac02c0b55e5b4"

0 commit comments

Comments
 (0)
Please sign in to comment.