PhantomJS driver: write to stderr on error, fix parsing of arguments, add --quiet option to suppress errors

main
Elbert Alias 9 years ago
parent 1c15f0fbd9
commit 5593dba22a

@ -6,7 +6,8 @@
scriptPath = require('fs').absolute(require('system').args[0]), scriptPath = require('fs').absolute(require('system').args[0]),
resourceTimeout = 9000, resourceTimeout = 9000,
args = [], args = [],
debug = false; debug = false; // Output debug messages
quiet = false; // Don't output errors
try { try {
// Working directory // Working directory
@ -14,15 +15,28 @@
require('fs').changeWorkingDirectory(scriptDir); require('fs').changeWorkingDirectory(scriptDir);
require('system').args.forEach(function(arg) { require('system').args.forEach(function(arg, i) {
var arr = /^(--[^=]+)=(.+)$/.exec(arg);
if ( arr && arr.length === 3 ) {
arg = arr[1];
value = arr[2];
}
switch ( arg ) { switch ( arg ) {
case '-v': case '-v':
case '--verbose': case '--verbose':
debug = true; debug = true;
break;
case '-q':
case '--quiet':
quiet = true;
break; break;
case '--resource-timeout': case '--resource-timeout':
resourceTimeout = arg; resourceTimeout = value;
break; break;
default: default:
url = originalUrl = arg; url = originalUrl = arg;
@ -42,8 +56,12 @@
* Log messages to console * Log messages to console
*/ */
log: function(args) { log: function(args) {
if ( debug || args.type !== 'debug' ) { if ( args.type === 'error' ) {
console.log(args.message); if ( !quiet ) {
require('system').stderr.write(args.message + "\n");
}
} else if ( debug || args.type !== 'debug' ) {
require('system').stdout.write(args.message + "\n");
} }
}, },
@ -74,10 +92,19 @@
}); });
} }
console.log(JSON.stringify({ applications: apps })); wappalyzer.driver.sendResponse(apps);
} }
}, },
/**
* Send response
*/
sendResponse: function(apps) {
apps = apps || [];
require('system').stdout.write(JSON.stringify({ url: url, originalUrl: originalUrl, applications: apps }) + "\n");
},
/** /**
* Initialize * Initialize
*/ */
@ -104,21 +131,21 @@
page.settings.resourceTimeout = resourceTimeout; page.settings.resourceTimeout = resourceTimeout;
page.onConsoleMessage = function(message) { page.onConsoleMessage = function(message) {
wappalyzer.log(message); require('system').stdout.write(message + "\n");
}; };
page.onError = function(message) { page.onError = function(message) {
wappalyzer.log(message); wappalyzer.log(message, 'error');
console.log(JSON.stringify({ applications: [] })); wappalyzer.driver.sendResponse();
phantom.exit(1); phantom.exit(1);
}; };
page.onResourceTimeout = function() { page.onResourceTimeout = function() {
wappalyzer.log('Resource timeout'); wappalyzer.log('Resource timeout', 'error');
console.log(JSON.stringify({ applications: [] })); wappalyzer.driver.sendResponse();
phantom.exit(1); phantom.exit(1);
}; };
@ -172,9 +199,9 @@
phantom.exit(0); phantom.exit(0);
} else { } else {
wappalyzer.log('Failed to fetch page'); wappalyzer.log('Failed to fetch page', 'error');
console.log(JSON.stringify({ applications: [] })); wappalyzer.driver.sendResponse();
phantom.exit(1); phantom.exit(1);
} }
@ -184,9 +211,9 @@
wappalyzer.init(); wappalyzer.init();
} catch ( e ) { } catch ( e ) {
wappalyzer.log(e); wappalyzer.log(e, 'error');
console.log(JSON.stringify({ applications: [] })); wappalyzer.driver.sendResponse();
phantom.exit(1); phantom.exit(1);
} }