Ind.ie is now Small Technology Foundation.
Commit e9eb0231 authored by Stanley Shyiko's avatar Stanley Shyiko

Added support for -o (--output) & -m (--max-time)

parent 17577f82
var yargs = require('yargs')
.usage('Usage: electron-har [options...] <url>')
// NOTE: when adding an option - keep it compatible with `curl` (if possible)
.describe('u', 'User [and password] divided by colon').alias('u', 'user').nargs('u', 1)
.describe('u', 'Username and password (divided by colon)').alias('u', 'user').nargs('u', 1)
.describe('o', 'Write to file instead of stdout').alias('o', 'output').nargs('o', 1)
.describe('m', 'Maximum time allowed for HAR generation (in seconds)').alias('m', 'max-time').nargs('m', 1)
.describe('debug', 'Show GUI (useful for debugging)').boolean('debug')
.help('h').alias('h', 'help')
.version(function () { return require('../package').version; })
......@@ -14,6 +16,8 @@ if (argv.u) {
var username = usplit[0];
var password = usplit[1] || '';
}
var outputFile = argv.output;
var timeout = parseInt(argv.m, 10);
var debug = !!argv.debug;
var argvValidationError;
......@@ -28,32 +32,46 @@ if (argvValidationError) {
process.exit(1);
}
var electron = require('electron');
var app = require('app');
var BrowserWindow = require('browser-window');
var stringify = require('json-stable-stringify');
var fs = require('fs');
if (timeout > 0) {
setTimeout(function () {
console.error('Timed out waiting for the HAR');
debug || app.exit(2);
}, timeout * 1000);
}
app.commandLine.appendSwitch('disable-http-cache');
app.dock && app.dock.hide();
app.on('window-all-closed', function () {
app.quit();
});
if (username) {
app.on('login', function (event, webContents, request, authInfo, callback) {
event.preventDefault();
callback(username, password);
app.on('window-all-closed', function () { app.quit(); });
electron.ipcMain
.on('har-generation-succeeded', function (sender, event) {
var har = stringify(event, {space: 2});
if (outputFile) {
fs.writeFile(outputFile, har, function (err) {
if (err) {
console.error(err.message);
debug || app.exit(1);
return;
}
debug || app.quit();
});
} else {
console.log(har);
debug || app.quit();
}
})
.on('har-generation-failed', function (sender, event) {
console.error('An attempt to generate HAR resulted in error code ' + event.errorCode +
(event.errorDescription ? ' (' + event.errorDescription + ')' : '') +
'. \n(error codes defined in http://src.chromium.org/svn/trunk/src/net/base/net_error_list.h)');
debug || app.exit(event.errorCode);
});
}
var ipcMain = require('electron').ipcMain;
ipcMain.on('har-generation-succeeded', function (sender, event) {
console.log(stringify(event, {space: 2}));
debug || app.quit();
});
ipcMain.on('har-generation-failed', function (sender, event) {
console.error(stringify(event, {space: 2}));
debug || app.exit(1);
});
app.on('ready', function () {
......@@ -62,6 +80,13 @@ app.on('ready', function () {
var bw = new BrowserWindow({show: debug});
if (username) {
bw.webContents.on('login', function (event, request, authInfo, cb) {
event.preventDefault(); // default behavior is to cancel auth
cb(username, password);
});
}
bw.webContents.on('devtools-opened', function () {
function notifyDevToolsExtensionOfLoad() {
bw.webContents.executeJavaScript('new Image().src = "https://did-finish-load/"');
......@@ -73,14 +98,13 @@ app.on('ready', function () {
bw.webContents.on('did-fail-load', function (e, errorCode, errorDescription, url) {
if (url !== 'chrome://ensure-electron-resolution/' && url !== 'https://did-finish-load/') {
bw.webContents.removeListener('did-finish-load', notifyDevToolsExtensionOfLoad);
var err = new Error(errorDescription);
err.code = errorCode;
require('electron').ipcRenderer.send('har-generation-failed', err);
bw.webContents.executeJavaScript('require("electron").ipcRenderer.send("har-generation-failed", ' +
JSON.stringify({errorCode: errorCode, errorDescription: errorDescription}) + ')');
}
});
});
ipcMain.on('devtools-loaded', function (event) {
electron.ipcMain.on('devtools-loaded', function (event) {
setTimeout(function () { bw.loadURL(url); }, 0);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment