diff --git a/lib/screencap/raster.js b/lib/screencap/raster.js index b5beac6..57d8033 100644 --- a/lib/screencap/raster.js +++ b/lib/screencap/raster.js @@ -12,6 +12,7 @@ // - maxRenderWait [optional] - default 10000 - the maximum time to wait before taking the screenshot, regardless of whether resources are waiting to be loaded // - cutoffWait [optional] - default null - the maximum time to wait before cutting everything off and failing...this helps if there is a page taking a long time to load // - top, left, width, height [optional] - dimensions to use to screenshot a specific area of the screen +// - auth [optional] - Basic auth (ie. 'username:password') option if required by URL // // == Important notice when providing height == // @@ -28,7 +29,9 @@ var page = new WebPage(), mask = null, forcedRenderTimeout, renderTimeout, - cutoffTimeout; + cutoffTimeout, + userName, + password; // // Functions @@ -58,6 +61,11 @@ function pickupNamedArguments() { if(args.resourceWait) { resourceWait = args.resourceWait; } if(args.maxRenderWait) { maxRenderWait = args.maxRenderWait; } if(args.cutoffWait) { cutoffWait = args.cutoffWait; } + if(args.auth) { + pair = args.auth.split(/:/); + userName = pair[0]; + password = pair[1]; + } } function setupMask() { @@ -108,8 +116,16 @@ function evaluateWithArgs(func) { return page.evaluate(fn); } +function setBasicAuthentication() { + if (args.auth) { + page.settings.userName = userName; + page.settings.password = password; + } +} + function takeScreenshot() { cutoffExecution(); + setBasicAuthentication() page.open(args.url, function(status) { if(status !== 'success') { console.log('Unable to load: ' + args.url); diff --git a/spec/fetcher_spec.rb b/spec/fetcher_spec.rb index ddeb3e7..743c5a0 100644 --- a/spec/fetcher_spec.rb +++ b/spec/fetcher_spec.rb @@ -31,4 +31,10 @@ screenshot = Screencap::Fetcher.new('http://google.com?1=2&3=4').fetch(:output => TMP_DIRECTORY + 'ampersand.jpg', :width => 800) FastImage.size(screenshot)[0].should == 800 end + + it 'should support basic auth' do + url = 'http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?' + "0.#{rand(99999999)}" # hosted example requires fresh seed + screenshot = Screencap::Fetcher.new(url).fetch(:output => TMP_DIRECTORY + 'httpwatch.jpg', :auth => 'httpwatch:screencap', :width => 300, height: 60) + FastImage.size(screenshot)[0].should == 300 + end end