Skip to content

Instantly share code, notes, and snippets.

@rdamen
Created November 20, 2009 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdamen/239866 to your computer and use it in GitHub Desktop.
Save rdamen/239866 to your computer and use it in GitHub Desktop.
sinatra app for bookmarklets that run stuff
#!/usr/bin/env ruby
# Bookmarklets
Commands = {
:vim => 'mvim -f %s',
:vimtidy => 'curl -s %s | tidy -ib -utf8 -w 0 | mvim -f -',
:browse => '$BROWSER %s',
:store => 'echo %s >> ~/URLZ'
}
require 'sinatra'
helpers do
def menu
list link(Commands.map {|c| [script(c.first), c.first, script(c.first)]})
end
def open(cmd, url)
Kernel.spawn cmd % '"%s"' % url
end
def link stuff
stuff.map {|i| '<a href="%s">%s</a> <a href="/%s">?</a>' % i}
end
def list stuff
"<ul>%s</ul>" % stuff.map {|i| "<li>%s</li>" % i}.join
end
def server_name
env['SERVER_NAME']
end
def server_port
env['SERVER_PORT']
end
def server_url
"http://%s:%i/" % [server_name, server_port]
end
def script action
"javascript:window.location='%s%s/'+encodeURIComponent(document.location)" % [server_url, action]
end
end
get '/' do
erb :index
end
get '/favicon.ico' do
end
get '/javascript:window.location=*/*/:action/*' do
@action = params[:action].to_sym
@example = Commands[@action] % '"&laquo;URL&raquo;"'
erb :halp
end
Commands.each do |c|
route = '/%s/*' % c.first
cmd = c.last
instance_eval do
get route do
url = params[:splat].first
open cmd, url
redirect url
end
end
end
enable :inline_templates
__END__
@@ layout
<html>
<head>
<title><%= @action || "bookmarklets"%> on <%=server_name%></title>
</head>
<body>
<%=yield%>
</body>
</html>
@@ index
<%=menu%>
@@ halp
<h1><%=@action%></h1>
<code><%=@example%></code>
<h3>Desktop</h3>
Drag <a href="<%=script @action%>"><%=@action%></a> (yes, the link, drag it) to your bookmarks.
<h3>Mobile</h3>
<ol>
<li>Bookmark this page.</li>
<li>Edit the bookmark.</li>
<li>Remove <code><%=server_url%></code> from the start of the URL</li>
<li>Verify what remains <code><%=script @action%></code></li>
</ol>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment