Instantly share code, notes, and snippets.

What would you like to do?
JavaScript Bookmarklet Builder
#!/usr/bin/env perl
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 $source_code = do { local $/; <> };
# Zap the first line if there's already a bookmarklet comment:
$source_code =~ s{^// ?javascript:.+\n}{};
my $bookmarklet = $source_code;
for ($bookmarklet) {
s{(^\s*//.+\n)}{}gm; # Kill commented lines
s{^\s*/\*.+?\*/\n?}{}gms; # Kill block 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 "// $bookmarklet\n" . $source_code;
# Put bookmarklet on clipboard:
my $fh;
open($fh, '|-', '/usr/bin/pbcopy')
or die "Failed to open pipe to /usr/bin/pbcopy - $!";
print $fh $bookmarklet
or die "Failed to write to pbcopy pipe - $!";
or die "Failed to close pipe to pbcopy - $!";
27 Jan 2014
- Switched from backticks to open() for the pipe to pbcopy. Thanks to John Siracusa.
- Added '%' to the list of characters to encode.

This comment has been minimized.

jtsternberg commented Sep 5, 2015

Not familiar with Perl, but I'm curious how you would go about passing a destination path to this script.. so you could output the bookmarklet JS to another location.


This comment has been minimized.

kostasx commented Jan 10, 2016

Really useful script. Thank you for sharing.
I've started working on a CoffeeScript/JavaScript version here for Node.JS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment