Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a manifest.appcache for a given html file
#!/bin/bash
SWITCH="\e["
NRM="${SWITCH}0m" YLW="${SWITCH}33m" RED="${SWITCH}31m"
GRN="${SWITCH}32m"
BLU="${SWITCH}34m"
dir=$(dirname "${BASH_SOURCE[0]}")
function error {
echo -e $RED$@$NRM
}
function info {
echo -e $BLU$@$NRM
}
function warn {
echo -e $YLW$@$NRM
}
function green {
echo -e $GRN$@$NRM
}
function manifestComment {
local manifest=$1
shift
if [[ ! -f "$manifest" ]]; then
error "The manifest file \"$manifest\" does not exist."
return 1
fi
echo "$@" >> $manifest
}
function manifestPart {
local manifest=$1
local part=$2
shift
shift
local entries="$@"
if [[ ! -f "$manifest" ]]; then
error "The manifest file \"$manifest\" does not exist."
return 1
fi
if [[ "$part" == "" ]]; then
error "The manifest section $BLU must $RED be passed as the second parameter."
return 1
fi
echo "$part" >> $manifest
for entry in $entries; do
local e=$(echo "$entry" | sed 's/^\\//g')
echo "$e" >> $manifest
done
echo -e >> $manifest
return 0
}
function manifestHead {
local manifest=$1
if [[ ! -f "$manifest" ]]; then
error "The manifest file \"$manifest\" does not exist."
return 1
fi
echo "CACHE MANIFEST" > $manifest
echo -n '# ' >> $manifest
echo -n "v1 - " >> $manifest
date +"%Y-%m-%d %k:%M" >> $manifest
echo -e >> $manifest
}
if [[ -f "$1" ]]; then
html=$1
else
html=index.html
if [[ ! -f "$html" ]]; then
error "No $BLU'index.html'$NRM found. An html file to parse must be passed as the first parameter"
exit 1
fi
fi
shift
if [[ "$1" != "" ]]; then
manifest_actual="$1"
else
manifest_actual="manifest.appcache"
fi
tmp_css=$(mktemp -t)
tmp_js=$(mktemp -t)
tmp_manifest=$(mktemp -t)
tmp_err=$(mktemp -t)
xmllint --xpath "//link/@href" --html "$html" 2> $tmp_err > $tmp_css
xmllint --xpath "//script/@src" --html "$html" 2> $tmp_err >> $tmp_js
sed -i 's/ href="//g' $tmp_css
sed -i 's/ src="//g' $tmp_js
css=$(sed 's/"/\n/g' $tmp_css | sort -V)
jss=$(sed 's/"/\n/g' $tmp_js | sort -V)
manifestHead $tmp_manifest
if [[ "$?" != "0" ]]; then
/bin/rm -f $tmp_js
/bin/rm -f $tmp_css
/bin/rm -f $tmp_manifest
exit 1
fi
manifestPart $tmp_manifest "CACHE:" $jss $css
manifestPart $tmp_manifest "NETWORK:" '/api' '/api/* \*'
echo "FALLBACK:" >> $tmp_manifest
manifestComment $tmp_manifest '# /api/ fallback.html'
echo -e >> $tmp_manifest
manifestPart $tmp_manifest "SETTINGS:" "prefer-online"
cp -Rf $tmp_manifest "$manifest_actual"
/bin/rm -f $tmp_js
/bin/rm -f $tmp_css
/bin/rm -f $tmp_manifest
/bin/rm -f $tmp_err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.