Created
December 9, 2009 03:42
-
-
Save adacosta/252242 to your computer and use it in GitHub Desktop.
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
module MozRepl | |
require 'base64' | |
require 'net/telnet' | |
PROMPT = /\nrepl>\s/ | |
class Telnet < Net::Telnet | |
alias_method :old_cmd, :cmd | |
def cmd(options, &blk) | |
old_cmd(options, &blk).gsub!(MozRepl::PROMPT, '') | |
end | |
end | |
def self.connect(args={}) | |
session = MozRepl::Telnet.new(args) | |
session.waitfor /Welcome to MozRepl.\s*/im | |
yield session | |
session.close | |
end | |
end | |
connect_args = {"Host" => 'localhost', "Port" => 4242, "Timeout" => 3} | |
MozRepl.connect(connect_args) do |session| | |
canvas_b64 = %Q[ | |
var win = getBrowser().selectedTab.linkedBrowser.contentWindow; | |
var page_height = win.innerHeight + win.scrollMaxY; | |
var page_width = win.document.getElementsByTagName('html')[0].offsetWidth + win.scrollMaxX | |
var canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas'); | |
canvas.mozOpaque = true; | |
canvas.scrollTop = 0; | |
canvas.scrollLeft = 0; | |
canvas.width = page_width; | |
canvas.height = page_height; | |
var ctx = canvas.getContext('2d'); | |
ctx.drawWindow(win, 0, 0, page_width, page_height, 'rgb(255,255,255)'); | |
canvas.toDataURL(); | |
] | |
image_b64 = session.cmd(canvas_b64) | |
# strip html content type and quotes | |
image_b64 = image_b64[23..-2] | |
File.open('/Users/adacosta/Desktop/moztest.png', 'w') {|f| f.write(Base64.decode64(image_b64)) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment