Skip to content

Instantly share code, notes, and snippets.

@eabruzzese
Last active July 28, 2017 17:17
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 eabruzzese/e39f04a3569adc9a203e3ffdf101c03d to your computer and use it in GitHub Desktop.
Save eabruzzese/e39f04a3569adc9a203e3ffdf101c03d to your computer and use it in GitHub Desktop.
A screencap script with viewport support
var URLS = [
'/home',
'/about',
'/about/partners',
'/blog',
'/blog/15-facts-employers-should-know-about-class-15',
'/career-planning-tools',
'/career-planning-tools/career-mentorship',
'/career-planning-tools/career-planning-resources',
'/career-planning-tools/career-resources/7-essential-podcasts-business-students',
'/career-planning-tools/career-wizard',
'/career-planning-tools/college-graduate-timeline',
'/career-planning-tools/college-graduate-timeline/10-worst-parts-applying-internships-only-college',
'/careers-in-insurance',
'/careers-in-insurance/company-profiles',
'/careers-in-insurance/company-profiles/appalachian-state-university',
'/careers-in-insurance/insurance-career-roles',
'/careers-in-insurance/insurance-career-roles/actuarial-careers',
'/careers-in-insurance/insurance-career-stories',
'/careers-in-insurance/why-work-in-insurance',
'/careers-in-insurance/why-work-in-insurance/how-does-insurance-work',
'/contact-us',
'/education-experience',
'/education-experience/business-students',
'/education-experience/data-science-students',
'/education-experience/marketing-students',
'/education-experience/other-majors',
'/interests/enjoy-numbers-stats-and-streamlining',
'/media-center',
'/partner-with-us',
'/search/content?keys=actuary',
'/terms-of-use',
]
var BASEURL = 'http://test-mypath2017.pantheonsite.io'
// How to: save as capture.js and run with "phantomjs capture.js"
// Setup by modifying URLS, PAGE_WIDTH AND PAGE_HEIGHT constants!
// Hint: set PAGE_WIDTH or PAGE_HEIGHT to 0 to capture full page!
// modified version of script at http://www.cameronjtinker.com/post/2011/09/26/Take-Screenshot-of-all-HTML-documents-in-a-folder-using-PhantomJS.aspx
var VIEWPORTS = [
{ width: 1200 },
// { width: 1199, height: 0 },
// { width: 992, height: 0 },
// { width: 991, height: 0 },
// { width: 768, height: 0 },
// { width: 767, height: 0 },
// { width: 480, height: 0 },
// { width: 360, height: 0 }
]
// var DATE = new Date();
// var DATESTRING = DATE.getFullYear() + '-' + DATE.getMonth() + '-' + DATE.getDay();
// var TIMESTRING = DATE.getHours() + '-' + DATE.getMinutes() + '-' + DATE.getSeconds();
// var DIRECTORY = 'screenshots/' + DATESTRING + '--' + TIMESTRING;
// Permute the URLS and VIEWPORTS to create targets
var targets = []
URLS.forEach(function(url) {
VIEWPORTS.forEach(function(viewport) {
targets.push({ url: url, viewport: viewport })
})
})
// phantomjs page object and helper flag
var page = require('webpage').create(),
loadInProgress = false,
pageIndex = 0
// page handlers
page.onLoadStarted = function() {
loadInProgress = true
console.log('page ' + (pageIndex + 1) + ' load started')
}
page.onLoadFinished = function() {
var viewport = targets[pageIndex].viewport
loadInProgress = false
if (!viewport.height) {
var heights = page.evaluate(function() {
return [
document.body.scrollHeight,
document.body.offsetHeight,
document.documentElement.clientHeight,
document.documentElement.scrollHeight,
document.documentElement.offsetHeight
]
})
viewport.height = Math.max.apply(null, heights)
}
page.viewportSize = viewport
var filePath = [
'images/output',
pageIndex + 1,
'_',
viewport.width,
'x',
viewport.height,
'.png',
].join('')
var filePath = pageIndex + 1 + '.png'
console.log('rendering ' + filePath)
page.render(filePath)
console.log('page ' + (pageIndex + 1) + ' load finished')
pageIndex++
}
// try to load or process a new page every 1 sec.
setInterval(function() {
if (!loadInProgress && pageIndex < targets.length) {
console.log('image ' + (pageIndex + 1))
page.open(BASEURL + targets[pageIndex].url)
}
if (pageIndex === targets.length) {
console.log('image render complete!')
phantom.exit()
}
}, 250)
console.log('Number of targets: ' + targets.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment