Skip to content

Commit e7d5a86

Browse files
authored
Merge pull request #15 from smithclay/visit-page-option
Add ability to visit a URL without specifying a script
2 parents 032dd1a + 91a9638 commit e7d5a86

File tree

3 files changed

+61
-26
lines changed

3 files changed

+61
-26
lines changed

index.js

Lines changed: 25 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,32 @@ 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+
// Determine script to run: either a 1) base64-encoded 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+
log.error(err);
71+
return callback(err, null);
5872
}
5973

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

lib/sandbox.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
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 && scriptText === undefined) {
11+
scriptText = require('fs').readFileSync(require('path').join(__dirname, 'visit-page.js'), 'utf8').toString();
12+
}
13+
log.info(`Executing script "${scriptText}"`);
14+
15+
if (typeof scriptText !== 'string') {
16+
return cb('Error: no url or script found to execute.');
17+
}
18+
19+
var consoleWrapper = {
20+
log: function(){
21+
var args = Array.prototype.slice.call(arguments);
22+
args.unshift('[lambdium-selenium]');
23+
console.log.apply(console, args);
24+
}
25+
};
26+
527
// Create Sandbox VM
628
const sandbox = {
729
'$browser': browser,
830
'$driver': driver,
9-
'console': console
31+
'$pageUrl': opts.pageUrl,
32+
'console': consoleWrapper
1033
};
1134

1235
const script = new vm.Script(scriptText);
@@ -19,28 +42,19 @@ exports.executeScript = function(scriptText, browser, driver, cb) {
1942
return cb(e, null);
2043
}
2144

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-
*/
3145
// https://github.com/GoogleChrome/puppeteer/issues/1825#issuecomment-372241101
3246
// Reuse existing session, likely some edge cases around this...
3347
if (process.env.CLEAN_SESSIONS) {
34-
$browser.quit().then(function() {
48+
browser.quit().then(function() {
3549
cb(null);
3650
});
3751
} else {
3852
browser.manage().deleteAllCookies().then(function() {
39-
return $browser.get('about:blank').then(function() {
53+
return browser.get('about:blank').then(function() {
4054
cb(null);
41-
}).catch(function(err) {
42-
cb(err);
4355
});
56+
}).catch(function(err) {
57+
cb(err);
4458
});
4559
}
4660
}

lib/visit-page.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This is the default lambdium webdriver script: it just visits a page url.
2+
console.log($pageUrl);
3+
$browser.get($pageUrl).then(function() {
4+
console.log('visited page', $pageUrl);
5+
}).catch(function() {
6+
console.log('error visiting page', $pageUrl);
7+
});

0 commit comments

Comments
 (0)