Created
February 20, 2011 12:45
-
-
Save kiwanami/835946 to your computer and use it in GitHub Desktop.
transferring capture images to the cacoo editor on Google Chrome
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 | |
# -*- coding: utf-8 -*- | |
# Time-stamp: <2011-02-20 21:44:14 sakurai> | |
# ImageMagick: import, identify | |
# coreutils: base64 | |
# chrome: ChromeRepl extension | |
# ruby: google-chrome-client | |
# Ref: blog.8-p.info: Auto reloading for Google Chrome | |
# http://blog.8-p.info/2010/16-google-chrome | |
require 'rubygems' | |
require 'google/chrome/client' | |
require 'optparse' | |
require 'pp' | |
def print_log_and_error(data) | |
if data['error'] | |
puts data['error']['stack'] | |
elsif data['log'] | |
if data['log'].kind_of?(String) | |
puts data['log'] | |
else | |
pp data['log'] | |
end | |
end | |
end | |
GALLERY_EXTENSION_ID = 'dedppbpeendknnpchklgpdiiiodajgki' | |
extension_id = GALLERY_EXTENSION_ID | |
port = 9222 | |
script = nil | |
import_arguments = [] | |
parser = OptionParser.new | |
parser.on('--port N', Integer) do |n| | |
port = n | |
end | |
parser.on('--extension ID') do |s| | |
extension_id = s | |
end | |
parser.on('--screen') do |v| | |
import_arguments << "-window root" | |
end | |
parser.parse!(ARGV) | |
file = '/tmp/_capture-cacoo.png' | |
`import #{import_arguments.join(" ")} #{file}` | |
if File.exist?(file) then | |
size = `identify -format "%w, %h" #{file}`; | |
data = `base64 #{file}`.split(/\n/).join | |
script = | |
"chrome.tabs.executeScript(null, {code:' | |
var head = document.getElementsByTagName(\\\'head\\\')[0]; | |
var prv = document.getElementById(\\\'chrome-local-capture\\\'); | |
if (prv) head.removeChild(prv); | |
var n = document.createElement(\\\'script\\\'); | |
n.id = \\\'chrome-local-capture\\\'; | |
n.innerHTML = \\\'callPlayerFunction(\"captured\",\"#{data}\",#{size});\\\'; | |
head.appendChild(n); | |
'}, function() {console.log(arguments);});" | |
client = Google::Chrome::Client.new('localhost', port) | |
extension = client.extension(extension_id) | |
extension.connect do |port| | |
extension.post(port, script.gsub(/\n/,"")) | |
client.read_all_response.each do |header, resp| | |
print_log_and_error(resp['data']) | |
end | |
end | |
end # if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment