Skip to content

Instantly share code, notes, and snippets.

@planbnet
Created March 16, 2010 07:28
Show Gist options
  • Select an option

  • Save planbnet/333729 to your computer and use it in GitHub Desktop.

Select an option

Save planbnet/333729 to your computer and use it in GitHub Desktop.

Revisions

  1. planbnet created this gist Mar 16, 2010.
    32 changes: 32 additions & 0 deletions bookmarklet.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/usr/bin/perl

    #generates a bookmarklet from a javascript file
    #and outputs a html page to drag the link to the bookmark bar
    #based on john grubers bookmarklet script:
    #http://daringfireball.net/2007/03/javascript_bookmarklet_builder

    use strict;
    use warnings;
    use URI::Escape qw(uri_escape_utf8);
    use open IO => ":utf8", # UTF8 by default
    ":std"; # Apply to STDIN/STDOUT/STDERR

    my $src = do { local $/; <> };

    # Zap the first line if there's already a bookmarklet comment:
    $src =~ s{^// ?javascript:.+\n}{};
    my $bookmarklet = $src;

    for ($bookmarklet) {
    s{^\s*//.+\n}{}gm; # Kill comments.
    s{\t}{ }gm; # Tabs to spaces
    s{[ ]{2,}}{ }gm; # Space runs to one space
    s{^\s+}{}gm; # Kill line-leading whitespace
    s{\s+$}{}gm; # Kill line-ending whitespace
    s{\n}{}gm; # Kill newlines
    }

    # Escape single- and double-quotes, spaces, control chars, unicode:
    $bookmarklet = "javascript:" . uri_escape_utf8($bookmarklet, qq('" \x00-\x1f\x7f-\xff));

    print "<html><body><p><a href=\"$bookmarklet\">BOOKMARKLET</a></p><p/><p>Drag the link to you bookmark bar</p></body></html>"