Advertisement
Guest User

Untitled

a guest
Dec 12th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #!/usr/bin/env macruby
  2. # from tomafro.net
  3.  
  4. framework 'Cocoa'
  5. framework 'WebKit'
  6.  
  7. class Snapper
  8. attr_accessor :options, :view, :window
  9.  
  10. def initialize(options = {})
  11. @options = options
  12. initialize_view
  13. end
  14.  
  15. def save(url, file, js)
  16. view.setFrameLoadDelegate(self)
  17. # Tell the webView what URL to load.
  18. frame = view.mainFrame
  19. req = NSURLRequest.requestWithURL(NSURL.URLWithString(url))
  20. frame.loadRequest req
  21.  
  22. while view.isLoading && !timed_out?
  23. NSRunLoop.currentRunLoop.runUntilDate NSDate.date
  24. end
  25.  
  26. if @failedLoading
  27. puts "Failed to load page at: #{url}"
  28. else
  29. #jquerify_script = File.read "jquerify.js"
  30. jquery = File.read "jquery.js"
  31. test_script = js || "jQuery('a').length"
  32.  
  33. puts "running:"
  34. puts test_script
  35.  
  36. puts ""
  37. puts "result:"
  38. puts view.stringByEvaluatingJavaScriptFromString jquery + test_script
  39. docView = view.mainFrame.frameView.documentView
  40. docView.window.setContentSize(docView.bounds.size)
  41. docView.setFrame(view.bounds)
  42.  
  43. docView.setNeedsDisplay(true)
  44. docView.displayIfNeeded
  45. docView.lockFocus
  46.  
  47. bitmap = NSBitmapImageRep.alloc.initWithFocusedViewRect(docView.bounds)
  48. docView.unlockFocus
  49.  
  50. # Write the bitmap to a file as a PNG
  51. representation = bitmap.representationUsingType(NSPNGFileType, properties:nil)
  52. representation.writeToFile(file, atomically:true)
  53. bitmap.release
  54. end
  55. end
  56.  
  57. private
  58.  
  59. def webView(view, didFailLoadWithError:error, forFrame:frame)
  60. @failedLoading = true
  61. end
  62.  
  63. def webView(view, didFailProvisionalLoadWithError:error, forFrame:frame)
  64. @failedLoading = true
  65. end
  66.  
  67. def initialize_view
  68. NSApplication.sharedApplication
  69.  
  70. self.view = WebView.alloc.initWithFrame([0, 0, 1024, 600])
  71. self.window = NSWindow.alloc.initWithContentRect([0, 0, 1024, 600],
  72. styleMask:NSBorderlessWindowMask, backing:NSBackingStoreBuffered, defer:false)
  73.  
  74. window.setContentView(view)
  75. # Use the screen stylesheet, rather than the print one.
  76. view.setMediaStyle('screen')
  77. # Set the user agent to Safari, to ensure we get back the exactly the same content as
  78. # if we browsed directly to the page
  79. view.setCustomUserAgent 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us)' +
  80. 'AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10'
  81. # Make sure we don't save any of the prefs that we change.
  82. view.preferences.setAutosaves(false)
  83. # Set some useful options.
  84. view.preferences.setShouldPrintBackgrounds(true)
  85. view.preferences.setJavaScriptCanOpenWindowsAutomatically(false)
  86. view.preferences.setAllowsAnimatedImages(false)
  87. # Make sure we don't get a scroll bar.
  88. view.mainFrame.frameView.setAllowsScrolling(false)
  89. end
  90.  
  91. def timed_out?
  92. @start ||= Time.now
  93. (Time.now.to_i - @start.to_i) > (options[:timeout] || 30)
  94. end
  95. end
  96.  
  97. Snapper.new.save(ARGV[0], 'screenshot.png', ARGV[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement