diff --git a/README.md b/README.md index 7c993dd..488315b 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,7 @@ reviews in one server. $ npm install -g review $ review --sites='{"google":"http://google.com","facebook":"http://facebook.com"}' \ - --resolutions='["1280x1024", "1900x1600", "800x600"]' \ - --cookie='{"name":"cookie_monster","value":"i_eat_them","domain":"google.com"}' + --resolutions='["1280x1024", "1900x1600", "800x600"]' $ open http://localhost:4000/ $ # and check @@ -41,7 +40,7 @@ Options: --resolutions, -r Resolutions as JSON Array of strings [default: "[\"1200x800\"]"] --wait, -w Time to give the page to finish loading, in milliseconds [default: 0] --cache, -c Cache snapshots for x milliseconds [default: false] - --cookie Adds a cookie to PhatomJS. Can be called multiple times [default: "{}"] + --cookie Add a cookie to PhatomJS --cut Cut snapshots to exact screen size [default: false] --help, -h Print usage instructions @@ -60,15 +59,11 @@ review() dir : __dirname + '/cache/', expires : 60 }) - .cookies([{ + .cookie({ name : 'cookie monster', value : 'i eat them!', domain : 'google.com' - },{ - name : 'universe', - value : '42', - domain : 'google.com' - }]) + }) .listen(4000) ``` @@ -121,11 +116,11 @@ Defaults to `0`. Cache rendered snapshots for `expires` seconds in `dir`. -### review#cookies([cookie, cookie, ...]) +### review#cookie(cookie) -An array of cookies that PhatomJS will use when requesting all pages. +Add a cookie for PhantomJS to use. Can be called multiple times, to set multiple cookies. -The individual cookie format is: +The cookie format is: ```js { diff --git a/bin/review.js b/bin/review.js index acce129..8da24f5 100755 --- a/bin/review.js +++ b/bin/review.js @@ -32,8 +32,7 @@ var argv = optimist .default('cache', false) .alias('c', 'cache') - .describe('cookie', 'Adds a cookie to PhatomJS. Can be called multiple times') - .default('cookie', '{}') + .describe('cookie', 'Add a cookie to PhatomJS') .describe('cut', 'Cut snapshots to exact screen size') .default('cut', false) @@ -44,16 +43,30 @@ var argv = optimist if (argv.help || !argv.sites) return optimist.showHelp() -var cookies = "[" + argv.cookie + "]" // wrap in square brackets, not a valid JSON otherwise - -review() - .title(argv.title) - .sites(JSON.parse(argv.sites)) - .resolutions(JSON.parse(argv.resolutions)) - .wait(argv.wait) - .cookies(JSON.parse(cookies)) - .cut(argv.cut) - .cache(argv.cache? { dir : __dirname + '/cache', expires : argv.cache } : false) - .listen(argv.port, function () { - console.log('-> Review on port ' + argv.port) - }) \ No newline at end of file +var server = review() + +server.title(argv.title) +server.sites(JSON.parse(argv.sites)) +server.resolutions(JSON.parse(argv.resolutions)) +server.wait(argv.wait) +server.cut(argv.cut) + +if (argv.cache) { + server.cache({ + dir : __dirname + '/cache', + expires : argv.cache + }) +} else { + server.cache(false) +} + +var cookies = argv.cookies +if (!Array.isArray(cookies)) cookies = [cookies] +cookies.forEach(function(cookie){ + server.cookie(JSON.parse(cookie)) +}); + +server.listen(argv.port, function () { + console.log('-> Review on port ' + argv.port) +}) + diff --git a/example/cookie/review.js b/example/cookie/review.js index eb25a35..20e2d00 100644 --- a/example/cookie/review.js +++ b/example/cookie/review.js @@ -19,6 +19,11 @@ review() value : 'this is what I do', domain : 'localhost' }) + .cookie({ + name : 'eating this eating that', + value : 'cookies never make me fat', + domain : 'localhost' + }) .listen(5000, function () { console.log('-> Review on port 5000') - }) \ No newline at end of file + }) diff --git a/lib/review.js b/lib/review.js index 0299b35..795583d 100644 --- a/lib/review.js +++ b/lib/review.js @@ -30,7 +30,7 @@ module.exports = function review () { */ var setters = [ - 'title', 'sites', 'resolutions', 'wait', 'cache', 'cookies', 'cut' + 'title', 'sites', 'resolutions', 'wait', 'cache', 'cut' ] setters.forEach(function (key) { app[key] = function (value) { @@ -38,11 +38,9 @@ module.exports = function review () { } }) - // for backward compatibility - app['cookie'] = function(cookie) { - var cookiesJar = app.get('cookies') - cookiesJar.push(cookie) - return app.set('cookies', cookiesJar) + app.cookie = function(cookie){ + app.get('cookies').push(cookie) + return app } /** @@ -108,4 +106,4 @@ module.exports = function review () { }) return app -} \ No newline at end of file +} diff --git a/script/rasterize.js b/script/rasterize.js index 17e1215..70cd766 100644 --- a/script/rasterize.js +++ b/script/rasterize.js @@ -22,7 +22,9 @@ if (cut) page.clipRect = { } cookies = JSON.parse(cookies) -cookies.forEach(function(cookie) { phantom.addCookie(cookie) }) +cookies.forEach(function(cookie) { + phantom.addCookie(cookie) +}) // silence phantomjs page.onConsoleMessage = function () {}