Convert extension scripts to fish

This commit is contained in:
Manuel Thalmann 2024-09-22 23:04:03 +02:00
parent 492583ffbc
commit 56e56c5cea
5 changed files with 66 additions and 95 deletions

View file

@ -0,0 +1,49 @@
function installExtension -d "Installs a Chromium extension for the browser with the specified information" -a name dir bin policyDir extensionDir
[ -n "$bin" ]
or set -l bin "google-chrome-stable"
[ -n "$policyDir" ]
or set -l policyDir "/opt/google/chrome/extensions"
[ -n "$extensionDir" ]
or set -l extensionDir "/usr/local/share/chromium-extensions"
set -l keyFile "$dir.pem"
set -l extensionFile "$dir.crx"
set -l manifestFile "$dir/manifest.json"
set -l destination "$extensionDir/$name.crx"
rm "$keyFile" &> /dev/null
$bin --pack-extension="$dir"
# Tamper manifest file
begin
set -l manifest (cat "$manifestFile" | KEY=(openssl rsa -in "$keyFile" -pubout -outform DER | openssl base64 -A) jq ". + { key: env.KEY }")
echo "$manifest" > "$manifestFile"
$bin --pack-extension="$dir" --pack-extension-key="$keyFile"
end
set -l id (openssl rsa -in "$keyFile" -pubout -outform DER | sha256sum | head -c32 | tr 0-9a-f a-p)
set -l extVersion (cat "$manifestFile" | jq -r ".version")
# Install and configure extension
begin
set -l property "external_crx"
sudo mkdir -p "$extensionDir"
sudo cp "$extensionFile" "$destination"
sudo chmod -R a+rx "$extensionDir"
sudo chmod -R u+w "$extensionDir"
sudo mkdir -p "$policyDir"
for manifest in (find "$policyDir" -name "*.json" -print0 | string split0)
if [ (cat "$manifest" | jq -r ".$property") = "$destination" ]
sudo rm "$manifest"
end
end
begin
echo "null" | \
FILE="$destination" \
VERSION="$extVersion" \
jq '{ external_crx: env.FILE, external_version: env.VERSION }'
end | sudo tee "$policyDir/$id.json" > /dev/null
end
end

View file

@ -1,59 +0,0 @@
#!/bin/bash
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
sudo \
BROWSER_BIN="${BROWSER_BIN}" \
EXTENSION_NAME="${EXTENSION_NAME}" \
UNPACKED_EXTENSION="${UNPACKED_EXTENSION}" \
EXTENSION_DIR="${EXTENSION_DIR}" \
EXTENSION_POLICY_DIR="${EXTENSION_POLICY_DIR}" \
bash --login "$BASH_SOURCE" "$USER";
else
keyFile="$UNPACKED_EXTENSION.pem";
extensionFile="$UNPACKED_EXTENSION.crx";
manifestFile="$UNPACKED_EXTENSION/manifest.json";
destination="$EXTENSION_DIR/$EXTENSION_NAME.crx";
sudo -u "$1" "${BROWSER_BIN}" --pack-extension="$UNPACKED_EXTENSION";
# 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" > /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')";
sudo -u "$1" "${BROWSER_BIN}" --pack-extension="$UNPACKED_EXTENSION" --pack-extension-key="$keyFile";
# Install extension and preinstall it
mkdir -p "$EXTENSION_DIR";
cp "$extensionFile" "$destination";
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 "}";
} | tee "$EXTENSION_POLICY_DIR/$extensionID.json" > /dev/null;
fi

View file

@ -0,0 +1,16 @@
function installYTMDL
source "$(status dirname)/extensions.fish"
set -l extension (mktemp)
set -l dir (mktemp -d)
set -l manifestFile "$dir/manifest.json"
chmod -R a+rwx "$dir"
wget https://github.com/dougppaz/youtube-music-dl/releases/download/v1.4.0/youtube-music-dl.zip -O "$extension"
mkdir -p "$dir"
unzip "$extension" -d "$dir"
rm "$extension"
installExtension youtube-music-dl "$dir" $argv
rm -rf "$dir"
end
installYTMDL $argv

View file

@ -1,33 +0,0 @@
#!/bin/bash
pushd "${BASH_SOURCE%/*}" > /dev/null;
BROWSER_BIN="${BROWSER_BIN}";
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" > /dev/null 2>&1;
# Prepare extension
mkdir -p "$extensionDir";
unzip "$archiveName" -d "$extensionDir";
rm "$archiveName";
# Tamper manifest file
manifest="$(cat "$manifestFile" | jq ".background.persistent = false")";
echo "$manifest" > "$manifestFile";
BROWSER_BIN="${BROWSER_BIN}" \
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;

View file

@ -21,9 +21,7 @@ begin
end
end
BROWSER_BIN="$bin" \
EXTENSION_POLICY_DIR="$extensionRoot" \
bash "$dir/../Chromium/ytmdl.sh"
fish "$dir/../Chromium/ytmdl.fish" "$bin" "$extensionRoot"
for i in (seq (count $bins))
if [ "$extensionRoots[$i]" != "$extensionRoot" ]