Skip to content

Instantly share code, notes, and snippets.

@gyuque
Created October 15, 2010 08:33
Show Gist options
  • Save gyuque/627852 to your computer and use it in GitHub Desktop.
Save gyuque/627852 to your computer and use it in GitHub Desktop.
repl.rb
require 'rubygems'
require 'json'
require 'net/telnet'
target_url = "http://www.jewelpet.jp/"
prompt = /^repl> /
INJECT_JS = <<EOS
repl.print( (function(doc){
var _j = function(s) {
if (!s) return '';
return s.replace(/"/g, "\\\\\\\""); //'"
};
var ofs = function(elm) {
var x = 0;
var y = 0;
var w = elm.offsetWidth;
var h = elm.offsetHeight;
for (;elm;) {
x += elm.offsetLeft;
y += elm.offsetTop;
elm = elm.offsetParent;
}
return [x, y, w, h];
};
var makeImageInfo = function(img, nm) {
var pos = ofs(img);
return "{\\\"tagname\\\": \\\""+nm+"\\\", \\\"url\\\": \\\""+_j(img.src)+"\\\", \\\"x\\\":"+pos[0]+", \\\"y\\\":"+pos[1]+", \\\"w\\\":"+pos[2]+", \\\"h\\\":"+pos[3]+"}";
};
var ents = [];
var i;
var ls = doc.getElementsByTagName("img");
var len = ls.length;
for (i = 0;i < len;i++) {
ents.push(makeImageInfo(ls[i], 'img'));
}
ls = doc.getElementsByTagName("object");
len = ls.length;
for (i = 0;i < len;i++) {
ents.push(makeImageInfo(ls[i], 'object'));
}
return ents.join(",");
})(content.document) );
EOS
def wait_load(telnet, pr)
loaded = false
STDERR.puts("Wait for load...")
10.times{|i|
curloc = nil
telnet.cmd("content.location.href") do |c|
curloc = c.gsub(pr,"")
end
if curloc != "about:blank"
loaded = true
end
STDERR.print("\b#{9-i}")
sleep 0.5
}
STDERR.puts("\bdone")
return loaded
end
telnet = Net::Telnet::new(
"Host" => "localhost",
"Port" => 4242,
"Prompt" => prompt
)
telnet.waitfor(prompt)
telnet.cmd("content.location.href = 'about:blank'")
telnet.cmd("content.location.href = '#{target_url}'")
if wait_load(telnet, prompt)
telnet.cmd(INJECT_JS) {|c|
STDOUT.puts '[' + c.gsub(prompt, "") +']'
}
end
telnet.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment