/ make-bookmarklet
make-bookmarklet
 1  #!/usr/bin/env perl
 2  #
 3  # http://daringfireball.net/2007/03/javascript_bookmarklet_builder
 4  
 5  use strict;
 6  use warnings;
 7  use URI::Escape qw(uri_escape_utf8);
 8  use open  IO  => ":utf8",       # UTF8 by default
 9            ":std";               # Apply to STDIN/STDOUT/STDERR
10  
11  my $src = do { local $/; <> };
12  
13  # Zap the first line if there's already a bookmarklet comment:
14  $src =~ s{^// ?javascript:.+\n}{};
15  my $bookmarklet = $src;
16  
17  for ($bookmarklet) {
18      s{^\s*//.+\n}{}gm;  # Kill comments.
19      s{\t}{ }gm;         # Tabs to spaces
20      s{[ ]{2,}}{ }gm;    # Space runs to one space
21      s{^\s+}{}gm;        # Kill line-leading whitespace
22      s{\s+$}{}gm;        # Kill line-ending whitespace
23      s{\n}{}gm;          # Kill newlines
24  }
25  
26  # Escape single- and double-quotes, spaces, control chars, unicode:
27  $bookmarklet = "javascript:" .
28      uri_escape_utf8($bookmarklet, qq('" \x00-\x1f\x7f-\xff));
29  
30  print "// $bookmarklet\n" . $src;
31  
32  # Put bookmarklet on clipboard:
33  `/bin/echo -n '$bookmarklet' | /usr/bin/pbcopy`;