Skip to content

Commit 1aa1ec2

Browse files
committed
add ability to visit a URL without specifying a script
1 parent 032dd1a commit 1aa1ec2

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

index.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ if (!process.env.CLEAN_SESSIONS) {
1313
$browser = chromium.createSession();
1414
}
1515

16+
const parseScriptInput = (event) => {
17+
const inputParam = event.Base64Script || process.env.BASE64_SCRIPT;
18+
if (typeof inputParam !== 'string') {
19+
return null
20+
}
21+
22+
return Buffer.from(inputParam, 'base64').toString('utf8');
23+
}
24+
1625
exports.handler = (event, context, callback) => {
1726
context.callbackWaitsForEmptyEventLoop = false;
1827

@@ -34,27 +43,31 @@ exports.handler = (event, context, callback) => {
3443

3544
log.info(`Received event: ${JSON.stringify(event, null, 2)}`);
3645

37-
// Read input
38-
const inputParam = event.Base64Script || process.env.BASE64_SCRIPT;
39-
if (typeof inputParam !== 'string') {
40-
return callback('Expected Base64Script string as input.');
41-
}
42-
43-
const inputBuffer = Buffer.from(inputParam, 'base64').toString('utf8');
44-
log.debug(`Executing script "${inputBuffer}"`);
45-
4646
// Creates a new session on each event (instead of reusing for performance benefits)
4747
if (process.env.CLEAN_SESSIONS) {
4848
$browser = chromium.createSession();
4949
}
5050

51-
sandbox.executeScript(inputBuffer, $browser, webdriver, function(err) {
51+
var opts = {
52+
browser: $browser,
53+
driver: webdriver
54+
};
55+
56+
// Provide script: either a 1) selenium script or 2) a URL to visit
57+
var inputBuffer = parseScriptInput(event);
58+
if (inputBuffer !== null) {
59+
opts.scriptText = inputBuffer;
60+
} else if (event.pageUrl || process.env.PAGE_URL) {
61+
opts.pageUrl = event.pageUrl || process.env.PAGE_URL;
62+
}
63+
64+
sandbox.executeScript(opts, function(err) {
5265
if (process.env.LOG_DEBUG) {
5366
log.debug(child.execSync('ps aux').toString());
5467
log.debug(child.execSync('cat /tmp/chromedriver.log').toString())
5568
}
5669
if (err) {
57-
callback(err, null);
70+
return callback(err, null);
5871
}
5972

6073
callback(null, 'Finished executing script');

lib/sandbox.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
const vm = require('vm');
22
const log = require('lambda-log');
33

4-
exports.executeScript = function(scriptText, browser, driver, cb) {
4+
exports.executeScript = function(opts = {}, cb) {
5+
const browser = opts.browser;
6+
const driver = opts.driver;
7+
var scriptText = opts.scriptText;
8+
9+
// Just visit a web page if a script isn't specified
10+
if (opts.pageUrl && (typeof scriptText !== 'string')) {
11+
scriptText = `$browser.get('${opts.pageUrl}').then(function(){ $browser.close() });`;
12+
}
13+
log.debug(`Executing script "${scriptText}"`);
14+
15+
if (typeof scriptText !== 'string') {
16+
return cb('Error: no url or script found to execute.');
17+
}
18+
519
// Create Sandbox VM
620
const sandbox = {
721
'$browser': browser,
822
'$driver': driver,
923
'console': console
1024
};
1125

12-
const script = new vm.Script(scriptText);
26+
const script = new vm.Script(opts.scriptText);
1327
// TODO: Set timeout options for VM context
1428
const scriptContext = new vm.createContext(sandbox);
1529
try {
@@ -19,28 +33,19 @@ exports.executeScript = function(scriptText, browser, driver, cb) {
1933
return cb(e, null);
2034
}
2135

22-
/*
23-
// Print performance logs
24-
$browser.manage().logs().get('performance').then(function (log) {
25-
for (var index in log) {
26-
var entry = log[index];
27-
console.log("[script-log] [" + entry.level.name + "] " + entry.message);
28-
}
29-
});
30-
*/
3136
// https://github.com/GoogleChrome/puppeteer/issues/1825#issuecomment-372241101
3237
// Reuse existing session, likely some edge cases around this...
3338
if (process.env.CLEAN_SESSIONS) {
34-
$browser.quit().then(function() {
39+
browser.quit().then(function() {
3540
cb(null);
3641
});
3742
} else {
3843
browser.manage().deleteAllCookies().then(function() {
39-
return $browser.get('about:blank').then(function() {
44+
return browser.get('about:blank').then(function() {
4045
cb(null);
41-
}).catch(function(err) {
42-
cb(err);
4346
});
47+
}).catch(function(err) {
48+
cb(err);
4449
});
4550
}
4651
}

0 commit comments

Comments
 (0)