Skip to content

Instantly share code, notes, and snippets.

@caged
Created March 26, 2009 23:07
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 caged/86435 to your computer and use it in GitHub Desktop.
Save caged/86435 to your computer and use it in GitHub Desktop.
#!/usr/bin/env macruby
#
# DarkRoom
# Takes fullsize screenshots of a web page.
# Copyright (c) 2007 Justin Palmer.
# Rewrote for MacRuby by Laurent Sansonetti.
#
# Released under an MIT LICENSE
#
# Usage
# ====
# ruby ./darkroom.rb http://activereload.net
# ruby ./darkroom.rb --output=google.png http://google.com
# ruby ./darkroom.rb --width=400 --delay=5 http://yahoo.com
#
require 'optparse'
require 'pp'
framework 'WebKit'
module ActiveReload
module DarkRoom
USER_AGENT = "DarkRoom/0.1"
class Photographer
def initialize
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: #$0 [options] URL"
opts.on('-w', '--width=[WIDTH]', Integer, 'Force width of the screenshot') do |v|
options[:width] = v
end
opts.on('-h', '--height=[HEIGHT]', Integer, 'Force height of screenshot') do |v|
options[:height] = v
end
opts.on('-o', '--output=[FILENAME]', String, 'Specify filename for saving') do |v|
options[:output] = v
end
opts.on('-d', '--delay=[DELAY]', Integer, 'Delay in seconds to give web page assets time to load') do |v|
options[:delay] = v
end
opts.on_tail('-h', '--help', 'Display this message and exit') do
puts opts
exit
end
end.parse!
options[:width] ||= 1024
options[:height] ||= 0
options[:website] = ARGV.first || 'http://ruby-lang.org'
Camera.shoot(options)
end
end
class Camera
def self.shoot(options)
app = NSApplication.sharedApplication
delegate = Processor.new
delegate.options = options
app.delegate = delegate
app.run
end
end
class Processor
attr_accessor :options, :web_view
def initialize
rect = [-16000.0, -16000.0, 100, 100]
win = NSWindow.alloc.initWithContentRect rect,
styleMask:NSBorderlessWindowMask,
backing:2,
defer:0
@web_view = WebView.alloc.initWithFrame rect
@web_view.mainFrame.frameView.allowsScrolling = false
@web_view.applicationNameForUserAgent = USER_AGENT
@web_view.frameLoadDelegate = self
NSNotificationCenter.defaultCenter.addObserver(self, selector:'webview_finished_loading:', name:WebViewProgressFinishedNotification, object:@web_view)
win.contentView = @web_view
end
def webview_finished_loading(sender)
puts "WOOT"
end
def applicationDidFinishLaunching(notification)
@options[:output] ||= "#{Time.now.strftime('%m-%d-%y-%H%I%S')}.png"
@web_view.window.contentSize = [@options[:width], @options[:height]]
@web_view.frameSize = [@options[:width], @options[:height]]
@web_view.mainFrame.loadRequest NSURLRequest.requestWithURL NSURL.URLWithString @options[:website]
end
def webView(web_view, didFinishLoadForFrame:frame)
viewport = frame.frameView.documentView
# if viewport.bounds.size.height == 0
# viewport = web_view.mainFrame.childFrames[0].frameView.documentView
# end
viewport.window.orderFront(nil)
viewport.window.display
viewport.window.contentSize = [@options[:width], (@options[:height] > 0 ? @options[:height] : viewport.bounds.size.height)]
viewport.frame = viewport.bounds
sleep(@options[:delay]) if @options[:delay]
capture_and_save viewport
end
def capture_and_save(view)
# bounds = view.bounds
# if bounds.size.height == 0
# bounds = NSMakeRect(0,0,1024,800)
# view.setFrame(bounds)
# end
view.lockFocus
bitmap = NSBitmapImageRep.alloc.initWithFocusedViewRect view.bounds
view.unlockFocus
repr = bitmap.representationUsingType NSPNGFileType, properties:nil
repr.writeToFile @options[:output], atomically:true
NSApplication.sharedApplication.terminate nil
end
end
end
end
ActiveReload::DarkRoom::Photographer.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment