Skip to content

Instantly share code, notes, and snippets.

@aji
Created February 20, 2012 08:41
Show Gist options
  • Save aji/1868454 to your computer and use it in GitHub Desktop.
Save aji/1868454 to your computer and use it in GitHub Desktop.
chromeless -- send files on the filesystem to Chromium as text/plain
#!/bin/sh
# Chromium's default action for files it doesn't understand is to
# download them, apparently. Also, it looks like .c and other similar
# plaintext files are not something Chromium thinks it can read.
# So to save myself from having to constantly copy things to a file
# with a .txt extension so I can read them and to keep from having to
# make Chromium treat .c files and such differently, I've decided to
# write this nifty little script.
# NOTE: this implementation is very sub-par. The first major weakness
# is that it copies the file. This means the entire duplication of a
# file upon each invocation of chromeless. My first approach was to
# use symlinks, but Chromium follows links and nothing changes :(
TMPDIR=/tmp/chromeless
BROWSER=chromium
TIMEOUT=20
if [ ! -d "$TMPDIR" ]; then
echo "$TMPDIR does not exist; creating..."
if ! mkdir -p $TMPDIR > /dev/null 2>&1; then
echo "Could not create $TMPDIR! exiting..."
exit 1
fi
fi
# STEP 1: obtain a temporary file whose name ends in .txt
temp=`mktemp --suffix=.txt $TMPDIR/XXXXX`
# STEP 2: copy the file (or stdin) to temp
target=$1
case $target in
''|'-')
cat /dev/stdin > $temp
;;
*)
if [ ! -f "$target" ]; then
echo "$target is not a regular file :("
rm -f $temp > /dev/null 2>&1
exit 1
fi
if ! cp $target $temp > /dev/null 2>&1; then
echo "copy failed :("
rm -f $temp > /dev/null 2>&1
exit 1
fi
;;
esac
# STEP 3: invoke the browser
$BROWSER $temp
# STEP 4: silently delete the temporary file after a timeout, now that
# the browser has (hopefully) downloaded it
( sleep $TIMEOUT; rm -f $temp >/dev/null 2>&1 ) &
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment