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

Commit 956a16b

Browse files
committed
Stop PhantomJS if a site unavailable
1 parent 86a246e commit 956a16b

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

lib/gastly/screenshot.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
module Gastly
66
class Screenshot
77

8+
FetchError = Class.new(StandardError)
9+
810
SCRIPT_PATH = File.expand_path('../script.js', __FILE__)
911
DEFAULT_TIMEOUT = 0
1012
DEFAULT_BROWSER_WIDTH = 1440
@@ -53,7 +55,9 @@ def capture
5355

5456
Phantomjs.proxy_host = proxy_host if proxy_host
5557
Phantomjs.proxy_port = proxy_port if proxy_port
56-
Phantomjs.run(proxy_options, SCRIPT_PATH.to_s, *prepared_params)
58+
output = Phantomjs.run(proxy_options, SCRIPT_PATH.to_s, *prepared_params)
59+
60+
raise FetchError, output if output.present?
5761

5862
Gastly::Image.new(tempfile)
5963
end

lib/gastly/script.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,29 @@ if (args.cookies !== undefined){
3939
}
4040
}
4141

42-
page.open(args.url, function(){
43-
window.setTimeout(function(){
44-
page.viewportSize = { width: args.width, height: args.height };
45-
46-
if (args.selector !== undefined){
47-
var clipRect = page.evaluate(function(s){
48-
return document.querySelector(s).getBoundingClientRect();
49-
}, args.selector);
50-
51-
page.clipRect = {
52-
top: clipRect.top,
53-
left: clipRect.left,
54-
width: clipRect.width,
55-
height: clipRect.height
56-
};
57-
}
58-
59-
page.render(args.output);
42+
page.open(args.url, function(status){
43+
if(status !== 'success') {
44+
console.log('Unable to load: ' + args.url);
6045
phantom.exit();
61-
}, args.timeout);
46+
} else {
47+
window.setTimeout(function(){
48+
page.viewportSize = { width: args.width, height: args.height };
49+
50+
if (args.selector !== undefined){
51+
var clipRect = page.evaluate(function(s){
52+
return document.querySelector(s).getBoundingClientRect();
53+
}, args.selector);
54+
55+
page.clipRect = {
56+
top: clipRect.top,
57+
left: clipRect.left,
58+
width: clipRect.width,
59+
height: clipRect.height
60+
};
61+
}
62+
63+
page.render(args.output);
64+
phantom.exit();
65+
}, args.timeout);
66+
}
6267
});

lib/gastly/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Gastly
2-
VERSION = '0.2.0'
2+
VERSION = '0.2.1'
33
end

0 commit comments

Comments
 (0)