Created
October 3, 2011 16:09
-
-
Save tantalor/1259477 to your computer and use it in GitHub Desktop.
Perl script to URI-escape JavaScript for bookmarklets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# | |
# Written by John Gruber, taken with permission from: | |
# 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; | |
$bookmarklet =~ s{^\s*//.+\n}{}gm; # Kill comments. | |
$bookmarklet =~ s{\t}{ }gm; # Tabs to spaces | |
$bookmarklet =~ s{ +}{ }gm; # Space runs to one space | |
$bookmarklet =~ s{^\s+}{}gm; # Kill line-leading whitespace | |
$bookmarklet =~ s{\s+$}{}gm; # Kill line-ending whitespace | |
$bookmarklet =~ s{\n}{}gm; # Kill newlines | |
# Escape single- and double-quotes, spaces, control chars, unicode: | |
$bookmarklet = "javascript:" . | |
uri_escape_utf8($bookmarklet); | |
print "// $bookmarklet\n" . $src; | |
# Put bookmarklet on clipboard: | |
`/bin/echo -n '$bookmarklet' | /usr/bin/pbcopy`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made an improved version that tries to not treat "//" in http:// as a comment.