PortValhalla/scripts/Common/Software/Chromium/install-extension.sh

60 lines
2.1 KiB
Bash
Raw Normal View History

#!/bin/bash
2023-04-04 01:04:42 +00:00
BROWSER_BIN="${BROWSER_BIN}";
EXTENSION_NAME="${EXTENSION_NAME}";
UNPACKED_EXTENSION="${UNPACKED_EXTENSION}";
EXTENSION_DIR="${EXTENSION_DIR:-"/usr/local/share/chromium-extensions"}";
EXTENSION_POLICY_DIR="${EXTENSION_POLICY_DIR}";
# Elevate script
if [ ! "$UID" -eq 0 ]
then
2023-04-02 22:15:09 +00:00
sudo \
2023-04-04 01:12:57 +00:00
BROWSER_BIN="${BROWSER_BIN}" \
2023-04-02 22:15:09 +00:00
EXTENSION_NAME="${EXTENSION_NAME}" \
UNPACKED_EXTENSION="${UNPACKED_EXTENSION}" \
EXTENSION_DIR="${EXTENSION_DIR}" \
EXTENSION_POLICY_DIR="${EXTENSION_POLICY_DIR}" \
bash "$BASH_SOURCE" "$USER";
else
keyFile="$UNPACKED_EXTENSION.pem";
extensionFile="$UNPACKED_EXTENSION.crx";
manifestFile="$UNPACKED_EXTENSION/manifest.json";
destination="$EXTENSION_DIR/$EXTENSION_NAME.crx";
2023-04-04 01:04:42 +00:00
sudo -u "$1" "${BROWSER_BIN}" --pack-extension="$UNPACKED_EXTENSION";
2023-04-02 22:15:27 +00:00
# Tamper manifest file
manifest="$(cat "$manifestFile" | jq ". + {key: "'"'"$(openssl rsa -in "$keyFile" -pubout -outform DER | openssl base64 -A)"'"'"}")";
2024-03-09 20:45:50 +00:00
echo "$manifest" | sudo -u "$1" tee "$manifestFile" > /dev/null;
# Pack extension
extensionID="$(openssl rsa -in "$keyFile" -pubout -outform DER | sha256sum | head -c32 | tr 0-9a-f a-p)";
extensionVersion="$(cat "$manifestFile" | jq -r '.version')";
2023-04-04 01:04:42 +00:00
sudo -u "$1" "${BROWSER_BIN}" --pack-extension="$UNPACKED_EXTENSION" --pack-extension-key="$keyFile";
# Install extension and preinstall it
2023-04-04 01:12:57 +00:00
mkdir -p "$EXTENSION_DIR";
2023-04-02 22:26:47 +00:00
cp "$extensionFile" "$destination";
2023-04-02 22:24:36 +00:00
chmod -R a+rx "$EXTENSION_DIR";
chmod -R u+w "$EXTENSION_DIR";
mkdir -p "$EXTENSION_POLICY_DIR";
crxProperty="external_crx";
find "$EXTENSION_POLICY_DIR" -name "*.json" -print0 |
while IFS= read -r -d "" file
do
if [ "$(cat "$file" | jq -r ".$crxProperty")" == "$destination" ]
then
rm "$file";
fi;
done;
{
echo "{";
echo ' "external_crx": "'"$destination"'",';
echo ' "external_version": "'"$extensionVersion"'"';
echo "}";
2024-03-09 20:45:50 +00:00
} | tee "$EXTENSION_POLICY_DIR/$extensionID.json" > /dev/null;
fi