Skip to content

Instantly share code, notes, and snippets.

@yihyang
Last active March 19, 2019 06:17
Show Gist options
  • Select an option

  • Save yihyang/5678acbe2df02958094d122f037b0959 to your computer and use it in GitHub Desktop.

Select an option

Save yihyang/5678acbe2df02958094d122f037b0959 to your computer and use it in GitHub Desktop.
Bash to generate critical CSS
# check whether critical has been generated
if ! [ -x "$(command -v critical)" ]; then
echo 'Error: critical CSS is not installed.' >&2
exit 1
fi
# ask for website
echo "Website URL (without http / https):"
read -r url
# read the page
curl -L $url > temp.html
# screensize
echo Screen size:
read -r screen_size
# echo $url
# extract domain
domain=`expr "$url" : '\([\.a-z0-9\-]*\)'`
# echo $domain
# replace the CSS
perl -pi.bak -e "s/href\=\"\//href\=\"http\:\/\/${domain}\//g" temp.html
# generate critical CSS
critical temp.html > output.css --height $screen_size
pbcopy < output.css
rm temp.html temp.html.bak output.css
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment