forge
Explore
Log in

Don't you dare go Hollow

dotfiles

Clone
★ 0

0 watching0 forksShellGPL-3.0334.8 KiBb1320e9Tahinli17 days ago105 commits

webapp.sh61 lines, 2.1 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# Launch a Chromium web app (github, whatsapp, deezer, mail, ...).
3#
4# usage: webapp.sh [--mullvad] <url> <profile-id>
5#
6# --mullvad route outside the VPN via mullvad-exclude
7# <url> app URL, e.g. https://github.com/
8# <profile-id> directory name under the web-app-hub profiles dir
9#
10# The window class is derived from the URL host as chrome-<host>__-Default,
11# which is what Chromium itself would generate — it has to match, since that is
12# the app-id niri and waybar see.
13#
14# Compositor-agnostic: usable from niri binds and Hyprland binds alike.
15set -euo pipefail
16
17BROWSER_ID="io.github.ungoogled_software.ungoogled_chromium"
18PROFILES="$HOME/.var/app/$BROWSER_ID/data/web-app-hub/profiles"
19
20MULLVAD=0
21if [ "${1:-}" = "--mullvad" ]; then
22 MULLVAD=1
23 shift
24fi
25
26URL="${1:?usage: webapp.sh [--mullvad] <url> <profile-id>}"
27PROFILE="${2:?usage: webapp.sh [--mullvad] <url> <profile-id>}"
28
29# https://web.whatsapp.com/foo -> web.whatsapp.com
30HOST="${URL#*://}"
31HOST="${HOST%%/*}"
32CLASS="chrome-${HOST}__-Default"
33
34# Chromium names its MPRIS player chromium.instance<pid>, and every flatpak
35# sandbox has its own pid namespace, so each web app's browser would get the
36# same pid and only the first app to start shows in playerctl. The sandbox
37# shell forks and reaps PID_SKIP throwaway processes first (pids are handed out
38# sequentially), which moves chromium to a per-profile pid. Offsets start at 10
39# to stay clear of the Chromium flatpak and FreeTube at pid 2. The trailing
40# `exit` stops bash from exec'ing chromium in place.
41PID_SKIP=$(( $(printf '%s' "$PROFILE" | cksum | cut -d' ' -f1) % 1000 + 10 ))
42cmd=(flatpak run --command=bash "$BROWSER_ID"
43 -c 'for ((i = 0; i < $0; i++)); do : & done; wait; chromium "$@"; exit $?' "$PID_SKIP"
44 --no-first-run
45 --app="$URL"
46 --class="$CLASS"
47 --name="$CLASS"
48 --user-data-dir="$PROFILES/$PROFILE")
49
50if [ "$MULLVAD" -eq 1 ]; then
51 cmd=(mullvad-exclude "${cmd[@]}")
52fi
53
54# --dry-run prints the command instead of running it, for verification.
55if [ "${DRY_RUN:-0}" = "1" ]; then
56 printf '%q ' "${cmd[@]}"
57 printf '\n'
58 exit 0
59fi
60
61exec "${cmd[@]}"