Skip to content

Instantly share code, notes, and snippets.

@allspiritseve
Created January 21, 2013 17:05
Show Gist options
  • Save allspiritseve/4587490 to your computer and use it in GitHub Desktop.
Save allspiritseve/4587490 to your computer and use it in GitHub Desktop.
Script to turn a readable javascript file with whitespace into a URL-encoded bookmarklet string. It doesn't automatically add a self-executing anonymous function, because you guys are smart and should be doing that already. Usage: $ ./bookmarkletize bookmarklet.js #=> "javascript:..."
#!/usr/bin/env ruby
#
# Bookmarkletize is a lil script to turn a normal javascript file into a
# (virtually unreadable) URL-escaped string that can be used in a
# bookmarklet.
#
# Inspiration: http://daringfireball.net/2007/03/javascript_bookmarklet_builder
#
# I could use his script, but come on. It's perl.
require 'uri'
def announce(message)
puts "\n#{message}\n\n"
end
def say(message)
puts "\n#{message}"
end
bookmarklet = ARGV[0]
unless bookmarklet
puts "Please pass the name of a javascript file" unless bookmarklet
exit
end
js = open(bookmarklet).read
js.gsub!(/^\s*\/\/.+\n/, '')
js.gsub!(/\t/, ' ')
js.gsub!(/\s{2,}/, '')
js.gsub!(/^\s+/, '')
js.gsub!(/\s+$/, '')
js.gsub!(/\n/, '')
say "javascript:#{URI::escape(js)}"
announce "Bookmarkletized!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment