Created
March 28, 2014 14:44
-
-
Save myokoym/9834466 to your computer and use it in GitHub Desktop.
Ruby/WebKitGTK2でWebページのスクリーンショットを取得する例。引数にURLを指定して、sキーで取得。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "webkit-gtk2" | |
uri = ARGV[0] | |
window = Gtk::Window.new | |
window.signal_connect("destroy") do | |
Gtk.main_quit | |
end | |
web_view = WebKitGtk2::WebView.new | |
web_view.load_uri(uri) | |
window.add(web_view) | |
window.signal_connect("key-press-event") do |widget, event| | |
case event.keyval | |
when Gdk::Keyval::GDK_KEY_s | |
x, y, width, height, depth = window.window.geometry | |
pixbuf = Gdk::Pixbuf.from_drawable(nil, | |
window.window, | |
0, 0, | |
width, height) | |
pixbuf.save("screenshot-#{Time.now.strftime("%Y%m%d%H%M%S")}.png", "png") | |
end | |
end | |
window.show_all | |
Gtk.main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment