#!/usr/bin/env bash # Launch a Chromium web app (github, whatsapp, deezer, mail, ...). # # usage: webapp.sh [--mullvad] # # --mullvad route outside the VPN via mullvad-exclude # app URL, e.g. https://github.com/ # directory name under the web-app-hub profiles dir # # The window class is derived from the URL host as chrome-__-Default, # which is what Chromium itself would generate — it has to match, since that is # the app-id niri and waybar see. # # Compositor-agnostic: usable from niri binds and Hyprland binds alike. set -euo pipefail BROWSER_ID="io.github.ungoogled_software.ungoogled_chromium" PROFILES="$HOME/.var/app/$BROWSER_ID/data/web-app-hub/profiles" MULLVAD=0 if [ "${1:-}" = "--mullvad" ]; then MULLVAD=1 shift fi URL="${1:?usage: webapp.sh [--mullvad] }" PROFILE="${2:?usage: webapp.sh [--mullvad] }" # https://web.whatsapp.com/foo -> web.whatsapp.com HOST="${URL#*://}" HOST="${HOST%%/*}" CLASS="chrome-${HOST}__-Default" # Chromium names its MPRIS player chromium.instance, and every flatpak # sandbox has its own pid namespace, so each web app's browser would get the # same pid and only the first app to start shows in playerctl. The sandbox # shell forks and reaps PID_SKIP throwaway processes first (pids are handed out # sequentially), which moves chromium to a per-profile pid. Offsets start at 10 # to stay clear of the Chromium flatpak and FreeTube at pid 2. The trailing # `exit` stops bash from exec'ing chromium in place. PID_SKIP=$(( $(printf '%s' "$PROFILE" | cksum | cut -d' ' -f1) % 1000 + 10 )) cmd=(flatpak run --command=bash "$BROWSER_ID" -c 'for ((i = 0; i < $0; i++)); do : & done; wait; chromium "$@"; exit $?' "$PID_SKIP" --no-first-run --app="$URL" --class="$CLASS" --name="$CLASS" --user-data-dir="$PROFILES/$PROFILE") if [ "$MULLVAD" -eq 1 ]; then cmd=(mullvad-exclude "${cmd[@]}") fi # --dry-run prints the command instead of running it, for verification. if [ "${DRY_RUN:-0}" = "1" ]; then printf '%q ' "${cmd[@]}" printf '\n' exit 0 fi exec "${cmd[@]}"