Skip to content

Instantly share code, notes, and snippets.

@slhck
Created July 21, 2022 09:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slhck/547773a8b1bd104e621e2a36b4960ef7 to your computer and use it in GitHub Desktop.
Save slhck/547773a8b1bd104e621e2a36b4960ef7 to your computer and use it in GitHub Desktop.
Download CRX file from Chrome Web Store
#!/usr/bin/env bash
#
# Download a Chrome extension from the webstore.
# Extract it to a specified path.
#
# Author: Werner Robitza
set -e
CHROME_VERSION="101.0.4951.57"
# ==============================================================================
usage() {
echo "Usage: $0 [-h|--help] <extensionId> <outputPath>"
echo
echo "Download and extract a Chrome extension,"
echo "creating a file with the extension ID and a .crx suffix."
echo
echo "Options:"
echo " -h, --help Show this help message and exit"
}
# If there are no options, exit:
if [ $# -lt 1 ]; then
usage
exit 1
fi
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 0
fi
extensionId="$1"
outputPath="$2"
if [ -z "$extensionId" ]; then
echo "Error: Chrome extension ID not specified."
exit 1
fi
if [ -z "$outputPath" ]; then
outputPath="$(pwd)"
fi
[ -n "$platformOs" ] || case "$OSTYPE" in
*darwin*) platformOs="mac" ;;
*openbsd*) platformOs="openbsd" ;;
*linux* | *hurd* | *sua* | *interix* | *bsd* | *sunos* | *solaris* | *indiana* | *illumos* | *smartos*) platformOs="Linux" ;;
*) echo "Platform unsupported!"; exit 1;;
esac
# if platform is Linux and we do not have the 7z binary, we need to install it
if [ "$platformOs" = "Linux" ] && ! [ -x "$(command -v 7z)" ]; then
echo "Error: 7z binary not found. Please install 7z first via:"
echo
echo " sudo apt-get install p7zip-full"
exit 1
fi
arch="x86-64"
ARCH="$(uname -m)"
[ -n "$arch" ] || case "$ARCH" in
*arm*) arch="arm" ;;
"x86_64") arch="x86-64" ;;
esac
# ==============================================================================
crxDownloadUrl='https://clients2.google.com/service/update2/crx?response=redirect'
crxDownloadUrl="${crxDownloadUrl}&os=${platformOs}"
crxDownloadUrl="${crxDownloadUrl}&arch=${arch}"
crxDownloadUrl="${crxDownloadUrl}&os_arch=${arch}"
#crxDownloadUrl="${crxDownloadUrl}&nacl_arch=${arch}"
crxDownloadUrl="${crxDownloadUrl}&prod=chromecrx"
crxDownloadUrl="${crxDownloadUrl}&prodchannel=unknown"
crxDownloadUrl="${crxDownloadUrl}&prodversion=${CHROME_VERSION}"
crxDownloadUrl="${crxDownloadUrl}&acceptformat=crx3"
crxDownloadUrl="${crxDownloadUrl}&x=id%3D${extensionId}%26uc"
downloadFilePath="$outputPath/$extensionId.crx"
mkdir -p "$outputPath"
unzipFolderPath="${downloadFilePath%.*}"
wget \
--quiet \
--referer="https://chrome.google.com/webstore/detail/${extensionId}?hl=en" \
--user-agent="Mozilla/5.0 Chrome/$CHROME_VERSION" \
-O "$downloadFilePath" "$crxDownloadUrl"
if [ ! -s "$downloadFilePath" ]; then
echo "Error: Downloaded file has 0 bytes."
exit 1
fi
unzipExitCode=0
if [ "$platformOs" = "Linux" ]; then
7z x -aoa -o"$unzipFolderPath" "$downloadFilePath" || true
else
unzip -o "$downloadFilePath" -d "$unzipFolderPath" || true
fi
unzipExitCode=$?
if [ $unzipExitCode -eq 1 ]; then
echo "Warning during unzip, but continuing"
fi
# if output folder does not exist
if [ ! -d "$unzipFolderPath" ]; then
echo "Error: Unzipped folder does not exist. Check the output of the unzip command."
exit 1
fi
echo "Extension downloaded to $downloadFilePath"
echo "Extension extracted to $unzipFolderPath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment