From 38b93422c79d6eac1355b6742a466219741503a4 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sun, 2 Apr 2023 03:44:32 +0200 Subject: [PATCH] Add a separate script for installing extensions --- .../Config/Chromium/install-extension.sh | 42 ++++++++++++++++ .../Config/Chromium/install-ytmusic-dl.sh | 32 ++++++++++++ .../Software/Brave/install-extensions.sh | 50 ------------------- scripts/PopOS/Collections/personal.sh | 6 ++- 4 files changed, 79 insertions(+), 51 deletions(-) create mode 100755 scripts/Common/Config/Chromium/install-extension.sh create mode 100755 scripts/Common/Config/Chromium/install-ytmusic-dl.sh delete mode 100755 scripts/Debian/Software/Brave/install-extensions.sh diff --git a/scripts/Common/Config/Chromium/install-extension.sh b/scripts/Common/Config/Chromium/install-extension.sh new file mode 100755 index 00000000..1c4f913a --- /dev/null +++ b/scripts/Common/Config/Chromium/install-extension.sh @@ -0,0 +1,42 @@ +#!/bin/bash +EXTENSION_NAME="${EXTENSION_NAME}"; +UNPACKED_EXTENSION="${UNPACKED_EXTENSION}"; +EXTENSION_DIR="${EXTENSION_DIR}"; +EXTENSION_POLICY_DIR="${EXTENSION_POLICY_DIR}"; + +# Elevate script +if [ ! "$UID" -eq 0 ] +then + EXTENSION_NAME="${EXTENSION_NAME}" \ + UNPACKED_EXTENSION="${UNPACKED_EXTENSION}" \ + EXTENSION_DIR="${EXTENSION_DIR}" \ + EXTENSION_POLICY_DIR="${EXTENSION_POLICY_DIR}" \ + sudo bash "$BASH_SOURCE" "$USER"; +else + keyFile="$UNPACKED_EXTENSION.pem"; + extensionFile="$UNPACKED_EXTENSION.crx"; + manifestFile="$UNPACKED_EXTENSION/manifest.json"; + + destination="$EXTENSION_DIR/$EXTENSION_NAME.crx"; + + # Tamper manifest file + manifest="$(cat "$manifestFile" | jq ". + {key: "'"'"$(openssl rsa -in "$keyFile" -pubout -outform DER | openssl base64 -A)"'"'"}")"; + echo "$manifest" | sudo -u "$1" tee "$manifestFile"; + + # 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')"; + sudo -u "$1" chromium --pack-extension="$extensionDir" --pack-extension-key="$keyFile"; + + # Install extension and preinstall it + cp "$extensionFile" "$EXTENSION_DIR"; + chmod -R a+rx "$extensionRoot"; + chmod -R u+w "$extensionRoot"; + mkdir -p "$EXTENSION_POLICY_DIR"; + { + echo "{"; + echo ' "external_crx": "'"$destination"'",'; + echo ' "external_version": "'"$extensionVersion"'"'; + echo "}"; + } | tee "$EXTENSION_POLICY_DIR/$extensionID.json"; +fi diff --git a/scripts/Common/Config/Chromium/install-ytmusic-dl.sh b/scripts/Common/Config/Chromium/install-ytmusic-dl.sh new file mode 100755 index 00000000..74dfb027 --- /dev/null +++ b/scripts/Common/Config/Chromium/install-ytmusic-dl.sh @@ -0,0 +1,32 @@ +#!/bin/bash +pushd "${BASH_SOURCE%/*}" > /dev/null; + +EXTENSION_DIR="${EXTENSION_DIR}"; +EXTENSION_POLICY_DIR="${EXTENSION_POLICY_DIR}"; + +archiveName="$(mktemp)"; +extensionDir="$(mktemp -d)"; +manifestFile="$extensionDir/manifest.json"; +chmod a+rwx "$extensionDir"; +# Download `youtube-music-dl` +wget https://github.com/dougppaz/youtube-music-dl/releases/download/v1.2.1/youtube-music-dl.zip -O "$archiveName"; + +# Prepare extension +mkdir -p "$extensionDir"; +sudo -u "$1" unzip "$archiveName" -d "$extensionDir"; +sudo -u "$1" chromium --pack-extension="$extensionDir"; +rm "$archiveName"; + +# Tamper manifest file +manifest="$(echo "$manifest" | jq ".background.persistent = false")"; +echo "$manifest" > "$manifestFile"; + +EXTENSION_NAME="youtube-music-dl" \ +UNPACKED_EXTENSION="$extensionDir" \ +EXTENSION_DIR="${EXTENSION_DIR}" \ +EXTENSION_POLICY_DIR="${EXTENSION_POLICY_DIR}" \ + . ./install-extension.sh; + +rm -rf "$extensionDir"; + +popd > /dev/null; diff --git a/scripts/Debian/Software/Brave/install-extensions.sh b/scripts/Debian/Software/Brave/install-extensions.sh deleted file mode 100755 index 3f632a37..00000000 --- a/scripts/Debian/Software/Brave/install-extensions.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash -# Elevate script -if [ ! "$UID" -eq 0 ] -then - sudo bash "$BASH_SOURCE" "$USER"; -else - # Download and extract `youtube-music-dl` - archiveName="$(sudo -u "$1" mktemp)"; - sudo -u "$1" wget https://github.com/dougppaz/youtube-music-dl/releases/download/v1.2.1/youtube-music-dl.zip -O "$archiveName"; - extensionName=youtube-music-dl; - - contextRoot="$(sudo -u "$1" mktemp -d)"; - extensionDir="$contextRoot/$extensionName"; - keyFile="$extensionDir.pem"; - extensionFile="$extensionDir.crx"; - manifestFile="$extensionDir/manifest.json"; - - extensionRoot=/usr/share/brave-extensions; - destination="$extensionRoot/$extensionName.crx"; - - # Prepare extension - mkdir -p "$extensionRoot"; - sudo -u "$1" unzip "$archiveName" -d "$extensionDir"; - sudo -u "$1" brave-browser --pack-extension="$extensionDir"; - rm "$archiveName"; - - # Tamper manifest file - manifest="$(cat "$manifestFile" | jq ". + {key: "'"'"$(openssl rsa -in "$keyFile" -pubout -outform DER | openssl base64 -A)"'"'"}")"; - manifest="$(echo "$manifest" | jq ".background.persistent = false")"; - echo "$manifest" | sudo -u "$1" tee "$manifestFile"; - - # 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')"; - sudo -u "$1" brave-browser --pack-extension="$extensionDir" --pack-extension-key="$keyFile"; - - # Install extension and preinstall it - cp "$extensionFile" "$extensionRoot"; - configRoot=/opt/brave.com/brave/extensions/ - rm -rf "$contextRoot"; - chmod -R a+rx "$extensionRoot"; - chmod -R u+w "$extensionRoot"; - mkdir -p "$configRoot"; - { - echo "{"; - echo ' "external_crx": "'"$destination"'",'; - echo ' "external_version": "'"$extensionVersion"'"'; - echo "}"; - } | tee "$configRoot/$extensionID.json"; -fi diff --git a/scripts/PopOS/Collections/personal.sh b/scripts/PopOS/Collections/personal.sh index 51f1465f..1386c397 100755 --- a/scripts/PopOS/Collections/personal.sh +++ b/scripts/PopOS/Collections/personal.sh @@ -7,7 +7,11 @@ source "../../Debian/Software/rclone/install.sh"; source "../../Debian/Software/Brave/install-stable.sh"; source "../../Debian/Software/Brave/install-beta.sh"; source "../../Debian/Software/Brave/install-nightly.sh"; -source "../../Debian/Software/Brave/install-extensions.sh"; + +EXTENSION_DIR=/usr/share/brave-extensions \ +EXTENSION_POLICY_DIR=/opt/brave.com/brave/extensions \ + source "../../Common/Config/Chromium/install-ytmusic-dl.sh"; + source "../../Debian/Software/Google Chrome/install.sh"; # Install further software