Skip to content

Instantly share code, notes, and snippets.

@remino
Created April 19, 2018 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remino/5e6562ed2632b76051417cb9ec1d19ba to your computer and use it in GitHub Desktop.
Save remino/5e6562ed2632b76051417cb9ec1d19ba to your computer and use it in GitHub Desktop.
jsbookmarklet – Minify JavaScript file as browser bookmarklet
#!/bin/sh
#
# jsbookmarklet
#
# remi@remino.net
# 2018-04-19
#
# Minify JavaScript file using uglify then save it to a file,
# output it to the screen, or copy the output to clipboard (macOS only),
# for usage as a browser bookmarklet.
#
# No guarantees. Public domain.
jsbookmarklet_main() {
[ $# -lt 1 ] && jsbookmarklet_usage && return 1
OPTIND=
DEFAULT_ECHO=1
while getopts "cehs" OPTION
do
case "$OPTION" in
c) COPY=1 && DEFAULT_ECHO=0 ;;
e) ECHO=1 ;;
h) jsbookmarklet_usage ;;
s) SAVE=1 && DEFAULT_ECHO=0 ;;
esac
done
shift `expr $OPTIND - 1`
INPUT="$1"
OUTPUT="$2"
_true "$DEFAULT_ECHO" && _false "$ECHO" && ECHO=1
[ "$OUTPUT" == "-" ] && OUTPUT="" && ECHO=1
[ -z "$OUTPUT" ] && OUTPUT="` basename "$INPUT" .js `.build.js"
[ ! -f "$INPUT" ] && _error "Cannot find file: $INPUT" && return 2
code="javascript:` uglifyjs -cm -- "$INPUT" `"
_true "$ECHO" && echo "$code"
_true "$SAVE" && echo "$code" > "$OUTPUT"
_true "$COPY" && echo "$code" | pbcopy
}
jsbookmarklet_usage() {
echo "Usage: ` basename "$0" ` [-ces] input.js [output.js]"
echo
echo "Minify JavaScript file using uglify then save it to a file,"
echo "output it to the screen, or copy the output to clipboard (macOS only)."
echo "for usage as a browser bookmarklet."
echo
echo " -c Copy output to clipboard."
echo " -e Echo output. (Default)"
echo " -h This help screen."
echo " -s Save output to file."
echo
}
_error() {
echo $@ 1>&2
}
_false() {
[ -z "$1" ] || [ "$1" == "0" ]
}
_true() {
[ "$1" == "1" ]
}
jsbookmarklet_main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment