aboutsummaryrefslogtreecommitdiffstats
path: root/bin/firefox-profile
blob: 3920e09cb34b659132fd724c8a971d3720bab0e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
# firefox-profile - pick a Firefox profile with rofi, one behind a password
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Pick a Firefox profile with rofi; one profile requires a password.
# Hash file: printf '%s' 'secret' | sha256sum | cut -d' ' -f1 > ~/.config/firefox-profile.hash
# ponytail: wrapper gate only, `firefox -P <name>` bypasses it. Encrypt the profile dir if that matters.

PROTECTED="danix"
HASHFILE="$HOME/.config/firefox-profile.hash"
INI="$HOME/.mozilla/firefox/profiles.ini"

# Opening a link with exactly one profile running: hand it to that instance,
# no picker. The protected profile was already unlocked to get it running.
if [ $# -gt 0 ]; then
    running=$(pgrep -af '^/usr/lib64/firefox/firefox(-bin)? (.* )?-P ' | sed -n 's/.* -P \([^ ]*\).*/\1/p' | sort -u)
    [ -n "$running" ] && [ "$(wc -l <<<"$running")" -eq 1 ] && exec firefox -P "$running" "$@"
fi

profile=$(sed -n 's/^Name=//p' "$INI" | rofi -dmenu -theme ~/.config/rofi/udt/list.rasi -i -p '󰈹  Firefox:') || exit 0
[ -n "$profile" ] || exit 0

if [ "$profile" = "$PROTECTED" ]; then
    pass=$(rofi -dmenu -theme ~/.config/rofi/udt/list.rasi -password -l 0 \
        -mesg "Unlock Firefox profile: $profile" \
        -theme-str 'mainbox { children: [ message, inputbar ]; padding: 16px; spacing: 8px; } message { background-color: transparent; padding: 0 0 4px 2px; } textbox { font: "Noto Sans Bold 13"; }' \
        -theme-str 'entry { placeholder: "Password"; }' </dev/null) || exit 0
    hash=$(printf '%s' "$pass" | sha256sum | cut -d' ' -f1)
    if [ "$hash" != "$(cat "$HASHFILE" 2>/dev/null)" ]; then
        notify-send "Firefox" "Wrong password" 2>/dev/null
        exit 1
    fi
fi

exec firefox -P "$profile" "$@"