PortValhalla/scripts/Common/Software/chromium/extensions.fish

50 lines
1.8 KiB
Fish
Raw Normal View History

2024-09-22 21:04:03 +00:00
function installExtension -d "Installs a Chromium extension for the browser with the specified information" -a name dir bin policyDir extensionDir
[ -n "$bin" ]
2024-10-06 19:25:34 +00:00
or set -l bin google-chrome-stable
2024-09-22 21:04:03 +00:00
[ -n "$policyDir" ]
2024-10-06 19:25:34 +00:00
or set -l policyDir /opt/google/chrome/extensions
2024-09-22 21:04:03 +00:00
[ -n "$extensionDir" ]
2024-10-06 19:25:34 +00:00
or set -l extensionDir /usr/local/share/chromium-extensions
2024-09-22 21:04:03 +00:00
set -l keyFile "$dir.pem"
set -l extensionFile "$dir.crx"
set -l manifestFile "$dir/manifest.json"
set -l destination "$extensionDir/$name.crx"
2024-10-06 19:25:34 +00:00
rm "$keyFile" &>/dev/null
2024-09-22 21:04:03 +00:00
$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 }")
2024-10-06 19:25:34 +00:00
echo "$manifest" >"$manifestFile"
2024-09-22 21:04:03 +00:00
$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
2024-10-06 19:25:34 +00:00
set -l property external_crx
2024-09-22 21:04:03 +00:00
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
2024-10-06 19:25:34 +00:00
echo null | \
2024-09-22 21:04:03 +00:00
FILE="$destination" \
VERSION="$extVersion" \
2024-10-06 19:25:34 +00:00
jq '{ external_crx: env.FILE, external_version: env.VERSION }'
end | sudo tee "$policyDir/$id.json" >/dev/null
2024-09-22 21:04:03 +00:00
end
end