Last active
December 23, 2015 10:49
-
-
Save crossai-2033/6624076 to your computer and use it in GitHub Desktop.
javascript:screencapture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var moment = require('moment'); | |
var casper = require('casper').create({ | |
verbose: true, | |
logLevel: "debug" | |
}); | |
var viewportSizes = [ | |
// [320,480], | |
// [320,568], | |
// [600,1024], | |
// [1024,768], | |
// [1280,800], | |
[1440,1900] | |
]; | |
var url = casper.cli.args[0]; | |
var saveDir = url.replace(/[^a-zA-Z0-9]/gi, '-').replace(/^https?-+/, ''); | |
casper.start(); | |
casper.each(viewportSizes, function(self, viewportSize, i) { | |
var width = viewportSize[0]; | |
var height = viewportSize[1]; | |
casper.wait(5000, function() { | |
this.viewport(width, height); | |
casper.thenOpen(url, function() { | |
this.echo('Opening at' + width); | |
// 注入脚本模拟操作,让页面请求完整 | |
casper.evaluate(function() { | |
var jsonpUrl = [ | |
"http://hao.h.qhimg.com/index.php?c=localData", | |
"&a=index&v=1.0&key=default&_callback=__jsonp5__&t=2299304" | |
].join(''); | |
qboot.jsonp(jsonpUrl, function(data) { | |
var localCity = document.querySelector('#localcity'); | |
var localTextarea = document.querySelector('#localcity textarea'); | |
localCity.innerHTML = localTextarea.value; | |
var localCityBD = document.querySelector('#localcity .localcity-bd'); | |
localCityBD.innerHTML = data.localdata; | |
}); | |
}); | |
casper.waitForSelector('#localcity .localcity-bd', function() { | |
var date = moment().format('YYYY-MM-DD'); | |
var FPfilename = saveDir + '/fullpage-' + width + '(' + date + ').png'; | |
var ACfilename = saveDir + '/' + width + '-' + height + '(' + date + ').png'; | |
this.captureSelector(FPfilename, 'body'); | |
this.capture(ACfilename, { top: 0, left: 0, width: width, height: height }); | |
this.echo('snapshot taken'); | |
}); | |
}); | |
}); | |
}); | |
casper.run(function() { | |
this.echo('Finished captures for ' + url).exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment