Created
December 11, 2009 06:26
-
-
Save tarao/254029 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// ==UserScript== | |
// @name run scala | |
// @namespace http://orezdnu.org/ | |
// @include http://*.g.hatena.ne.jp/* | |
// ==/UserScript== | |
(function(lang, url) { | |
var list = document.getElementsByTagName('pre'); | |
for (var i=0; i < list.length; i++) { | |
var pre = list[i]; | |
var div = document.createElement('div'); | |
var btn = document.createElement('div'); | |
btn.style.position = 'absolute'; btn.style.zIndex = 1000; | |
btn.style.backgroundColor = '#ffccdd'; | |
btn.style.width = '12px'; btn.style.height = '12px'; | |
btn.addEventListener('click', (function(i, node) { return function() { | |
var wait = document.createElement('img'); | |
wait.src = 'http://b.hatena.ne.jp/images/loading.gif'; | |
node.nextSibling ? | |
node.parentNode.insertBefore(wait, node.nextSibling) : | |
node.parentNode.appendChild(wait); | |
GM_xmlhttpRequest({ | |
method: 'POST', | |
url: url, | |
data: lang + "\n" + node.textContent, | |
headers: { | |
'User-Agent': 'monkey', | |
'Accept': 'text/html,text/plain' | |
}, | |
onload: function(r) { | |
var div = document.createElement('div'); | |
var pre = document.createElement('pre'); | |
pre.appendChild(document.createTextNode(r.responseText)); | |
div.appendChild(pre); | |
wait.parentNode.replaceChild(div, wait); | |
} | |
}); | |
}; })(i, pre), false); | |
pre.parentNode.replaceChild(div, pre); | |
div.appendChild(btn); | |
div.appendChild(pre); | |
} | |
})('scala', 'http://localhost:8080/?'+Date.now()); |
This file contains hidden or 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 'logger' | |
require 'webrick' | |
require 'yaml' | |
class IO | |
def read_interactive | |
sleep(0.3) | |
ret = '' | |
begin | |
ret += read_nonblock(1) while true | |
rescue EOFError | |
rescue Errno::EAGAIN | |
end | |
return ret | |
end | |
end | |
conffile = ARGV[0] | |
conf = { | |
:BindAddress => '0.0.0.0', | |
:Port => 8080, | |
} | |
if conffile && conffile | |
conf.merge!(YAML.load_file(conffile)) | |
if conf[:logfile] | |
loglevel = conf[:loglevel] || Logger::INFO | |
conf[:Logger] = WEBrick::Log.new(conf[:logfile], conf[:loglevel]) | |
end | |
end | |
srv = WEBrick::HTTPServer.new(conf) | |
srv.mount_proc('/') do |req, res| | |
if req.body | |
cmd, body = req.body.split("\n", 2) | |
body = body.chomp + "\n" | |
srv.logger.info(cmd) | |
srv.logger.info(body) | |
res.content_type = 'text/plain; charset=utf-8' | |
res.status = WEBrick::HTTPStatus::RC_OK | |
res.body = '' | |
IO.popen(cmd, 'r+') do |io| | |
sleep(1.5) | |
res.body += io.read_interactive | |
body.each_line do |line| | |
io.puts(line.chomp) | |
res.body += io.read_interactive | |
end | |
end | |
else | |
res.status = WEBrick::HTTPStatus::RC_NOT_FOUND | |
end | |
srv.logger.info(res.body) | |
end | |
srv.mount_proc('/test') do |req, res| | |
res.content_type = 'text/plain; charset=utf-8' | |
res.status = WEBrick::HTTPStatus::RC_OK | |
res.body = 'OK!' | |
end | |
srv.mount_proc('/stop') do |req, res| | |
srv.stop | |
res.content_type = 'text/plain; charset=utf-8' | |
res.status = WEBrick::HTTPStatus::RC_OK | |
res.body = 'stopped' | |
end | |
srv.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment