phantomjs_screenshot.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var page = require('webpage').create()
  2. var system = require('system')
  3. var maxTimeout = 15000
  4. if (system.args.length === 1) {
  5. console.log('required args: <siteURL> <outputFilePath>');
  6. phantom.exit(1)
  7. }
  8. var address = system.args[1]
  9. var outputPath = system.args[2]
  10. page.viewportSize = { width: 1280, height: 960 }
  11. page.clipRect = { top: 0, left: 0, width: 1280, height: 960}
  12. /*
  13. In development, not working yet.
  14. page.settings.scriptTimeout = 1000
  15. page.onLongRunningScript = function() {
  16. page.stopJavaScript()
  17. phantom.exit(4)
  18. }
  19. */
  20. var t = Date.now()
  21. console.log('Loading ' + address)
  22. setTimeout(function() {
  23. console.log('timeout')
  24. phantom.exit(62)
  25. }, maxTimeout)
  26. page.settings.resourceTimeout = maxTimeout
  27. page.onResourceTimeout = function(e) {
  28. console.log(e.errorCode)
  29. console.log(e.errorString)
  30. console.log(e.url)
  31. phantom.exit(3)
  32. }
  33. page.open(address, function(status) {
  34. if(status !== 'success') {
  35. console.log('failed')
  36. phantom.exit(2)
  37. }
  38. page.render(outputPath)
  39. console.log('Loading time ' + (Date.now() - t) + ' msec');
  40. phantom.exit()
  41. })