Skip to content

Instantly share code, notes, and snippets.

@julik
Created January 11, 2010 16:00
Show Gist options
  • Save julik/274326 to your computer and use it in GitHub Desktop.
Save julik/274326 to your computer and use it in GitHub Desktop.
$KCODE = "u"
=begin
A remote copy-paste responder. Launch this on a specific port,
then open this on a remote machine. This will give you a bookmarklet.
Use this bookmarklet to copy selection from the browser directly to the clipboard
of the machine the server is running on
=end
JS = %<
x = document;
y = window;
Q = null;
if(x.selection) {
Q = x.selection.createRange().text;
} else if (y.getSelection) {
Q = y.getSelection();
} else if (x.getSelection) {
Q = x.getSelection();
};
if(Q.length == 0) {
alert("Nothing selected!")
return false;
}
Q = encodeURIComponent(Q);
var http = new XMLHttpRequest();
var url = "http://localhost:port/";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "text/plain");
http.send(Q);
>
HOST = `hostname`.chomp
PORT = 3333
COPIER = (RUBY_PLATFORM =~ /darwin/i) ? "pbcopy" : "xclip -i -selection c"
APP = lambda do | env |
if env["REQUEST_METHOD"] == "POST"
s = Rack::Utils.unescape(env["rack.input"].read.to_s)
IO.popen(COPIER, "w") {|i| i << s }
[200, {"Content-Type"=>"text/html"}, "Posted"]
else
substitutions = {
"\n" => '',
" = " => '=',
"localhost" => HOST,
"port" => PORT,
}
paster = substitutions.inject(JS) do | s, (k, v) |
s.sub(k, v.to_s)
end
bm = "javascript:(function(){ %s })();" % paster
[200, {"Content-Type"=>"text/html"}, "<a href='%s'>Send selection to the clipborard of #{HOST}</a>" % bm]
end
end
run APP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment