Skip to content

Instantly share code, notes, and snippets.

@papoms
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save papoms/1ace84fe3207940f2054 to your computer and use it in GitHub Desktop.
Save papoms/1ace84fe3207940f2054 to your computer and use it in GitHub Desktop.
var casper = require('casper').create();
/* var url = casper.cli.args[0]; */
var url = 'http://kicker.de';
var fn = url.replace(/[^a-z0-9]/gi, '').toLowerCase();
// Settings
var targetWidth = 800; //final Image Size
var targetHeight = 600;
var maxWidth = 2800;
var width = targetWidth;
var height = targetHeight;
var step = 50;
casper.start().viewport(targetWidth, targetHeight).thenOpen(url, function() {
var hasHorizontalScrollbar = true;
while (hasHorizontalScrollbar){
// Remove if you dont want to see the steps
this.capture(fn+'-'+width+'.png', {'top':0, 'left':0, 'width':width, 'height':height});
// Check if a Scrollbar is necessary (as opposed to visibile, which could be changed by site)
hasHorizontalScrollbar = this.evaluate(function(){
var root = document.compatMode == 'BackCompat' ? document.body : document.documentElement;
var hasHorizontalScrollbar = root.scrollWidth > root.clientWidth;
return hasHorizontalScrollbar
});
console.log('hasHorizontalScrollbar: ' + hasHorizontalScrollbar);
if(hasHorizontalScrollbar){
width += step;
if (width > maxWidth) break;
height = parseFloat((width * 0.75).toFixed(0));;
console.log('increasing width to ' + width);
this.viewport(width, height);
}
}
var zoomFactor = targetWidth / width;
this.zoom(zoomFactor).viewport(targetWidth, targetHeight, function() {
// new view port is effective
this.capture(fn+'-fitted.png', {'top':0, 'left':0, 'width':targetWidth, 'height':targetHeight});
});
});
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment