screenshot.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var page = require('webpage').create()
  2. var system = require('system')
  3. var signals = {
  4. 'success': 0,
  5. 'missingargs': 1,
  6. 'slowjs': 2,
  7. 'openfailed': 3,
  8. 'resourcetimeout': 4,
  9. 'maxtimeout': 5
  10. }
  11. var maxTimeout = 20000
  12. if (system.args.length !== 4) {
  13. console.log('required args: <siteURL> <filePath> <waitTime=secs>');
  14. phantom.exit(signals['missingargs'])
  15. }
  16. var address = system.args[1]
  17. var filePath = system.args[2]
  18. var waitTime = system.args[3] * 1000
  19. page.viewportSize = { width: 1295, height: 960 }
  20. page.clipRect = { top: 0, left: 0, width: 1280, height: 960}
  21. phantom.outputEncoding = 'binary'
  22. page.settings.scriptTimeout = 1000
  23. page.onLongRunningScript = function() {
  24. page.stopJavaScript()
  25. phantom.exit(signals['slowjs'])
  26. }
  27. var t = Date.now()
  28. //console.log('Loading ' + address)
  29. setTimeout(function() {
  30. console.log('timeout')
  31. phantom.exit(signals['maxtimeout'])
  32. }, maxTimeout)
  33. page.settings.resourceTimeout = maxTimeout
  34. page.onResourceTimeout = function(e) {
  35. console.log(e.errorCode)
  36. console.log(e.errorString)
  37. console.log(e.url)
  38. phantom.exit(signals['resourcetimeout'])
  39. }
  40. page.open(address, function(status) {
  41. if(status !== 'success') {
  42. console.log('failed')
  43. phantom.exit(signals['openfailed'])
  44. }
  45. setTimeout(function() {
  46. page.render(filePath, {format: 'jpg', quality: '97'})
  47. // console.log('Loading time ' + (Date.now() - t) + ' msec');
  48. phantom.exit(signals['success'])
  49. }, waitTime)
  50. })