@@ -13,6 +13,15 @@ if (!process.env.CLEAN_SESSIONS) {
13
13
$browser = chromium . createSession ( ) ;
14
14
}
15
15
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
+
16
25
exports . handler = ( event , context , callback ) => {
17
26
context . callbackWaitsForEmptyEventLoop = false ;
18
27
@@ -34,27 +43,31 @@ exports.handler = (event, context, callback) => {
34
43
35
44
log . info ( `Received event: ${ JSON . stringify ( event , null , 2 ) } ` ) ;
36
45
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
-
46
46
// Creates a new session on each event (instead of reusing for performance benefits)
47
47
if ( process . env . CLEAN_SESSIONS ) {
48
48
$browser = chromium . createSession ( ) ;
49
49
}
50
50
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 ) {
52
65
if ( process . env . LOG_DEBUG ) {
53
66
log . debug ( child . execSync ( 'ps aux' ) . toString ( ) ) ;
54
67
log . debug ( child . execSync ( 'cat /tmp/chromedriver.log' ) . toString ( ) )
55
68
}
56
69
if ( err ) {
57
- callback ( err , null ) ;
70
+ return callback ( err , null ) ;
58
71
}
59
72
60
73
callback ( null , 'Finished executing script' ) ;
0 commit comments