Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
Stop PhantomJS if a site unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrachev committed Aug 10, 2015
1 parent 86a246e commit 956a16b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
6 changes: 5 additions & 1 deletion lib/gastly/screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module Gastly
class Screenshot

FetchError = Class.new(StandardError)

SCRIPT_PATH = File.expand_path('../script.js', __FILE__)
DEFAULT_TIMEOUT = 0
DEFAULT_BROWSER_WIDTH = 1440
Expand Down Expand Up @@ -53,7 +55,9 @@ def capture

Phantomjs.proxy_host = proxy_host if proxy_host
Phantomjs.proxy_port = proxy_port if proxy_port
Phantomjs.run(proxy_options, SCRIPT_PATH.to_s, *prepared_params)
output = Phantomjs.run(proxy_options, SCRIPT_PATH.to_s, *prepared_params)

raise FetchError, output if output.present?

Gastly::Image.new(tempfile)
end
Expand Down
43 changes: 24 additions & 19 deletions lib/gastly/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,29 @@ if (args.cookies !== undefined){
}
}

page.open(args.url, function(){
window.setTimeout(function(){
page.viewportSize = { width: args.width, height: args.height };

if (args.selector !== undefined){
var clipRect = page.evaluate(function(s){
return document.querySelector(s).getBoundingClientRect();
}, args.selector);

page.clipRect = {
top: clipRect.top,
left: clipRect.left,
width: clipRect.width,
height: clipRect.height
};
}

page.render(args.output);
page.open(args.url, function(status){
if(status !== 'success') {
console.log('Unable to load: ' + args.url);
phantom.exit();
}, args.timeout);
} else {
window.setTimeout(function(){
page.viewportSize = { width: args.width, height: args.height };

if (args.selector !== undefined){
var clipRect = page.evaluate(function(s){
return document.querySelector(s).getBoundingClientRect();
}, args.selector);

page.clipRect = {
top: clipRect.top,
left: clipRect.left,
width: clipRect.width,
height: clipRect.height
};
}

page.render(args.output);
phantom.exit();
}, args.timeout);
}
});
2 changes: 1 addition & 1 deletion lib/gastly/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Gastly
VERSION = '0.2.0'
VERSION = '0.2.1'
end

0 comments on commit 956a16b

Please sign in to comment.