-
Notifications
You must be signed in to change notification settings - Fork 1
/
gchrome
executable file
·93 lines (78 loc) · 2.48 KB
/
gchrome
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# google-chrome profile launcher
#
# Adaptation of various ideas and solutions mentioned in https://superuser.com/a/377195/28775
# This script is meant to be "sourced" into your bash-profile. It provides command 'gchrome'
# and provides auto-completion of the profile-name.
: "${_gchrome_base_dir:="$HOME/.config/google-chrome"}"
_gchrome_get_profiles() {
< "${_gchrome_base_dir}/Local State" \
jq -r '[.profile.info_cache | to_entries[] | {"key": .key, "value": .value.name}] | .[] | "profiles+=([\(.value|@sh)]=\(.key|@sh));"'
}
_gchrome_get_completion() {
declare -A profiles
eval "$(_gchrome_get_profiles)"
for key in "${!profiles[@]}"; do
echo "$key";
done
}
_gchrome_make_completion() {
declare -A profiles
eval "$(_gchrome_get_profiles)"
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
for key in "${!profiles[@]}"; do
if [[ "$key" =~ ^$cur ]]; then
COMPREPLY+=( "$(printf '%q' "$key")" )
fi
done
}
_gchrome_is_valid_profile() {
local profile_path="${_gchrome_base_dir}/$1"
[ -n "$1" ] &&
[ -d "$(readlink -f "$profile_path")" ] &&
[ -f "$(readlink -f "$profile_path/Cookies")" ]
}
_gchrome_is_valid_profile_alias() {
declare -A profiles
eval "$(_gchrome_get_profiles)"
[ -n "${1:-}" ] &&
[ -v "profiles['${1:-}']" ] &&
_gchrome_is_valid_profile "${profiles[$1]}";
}
_gchrome_get_last_used_profile() {
< "${_gchrome_base_dir}/Local State" \
jq -r '.profile.last_used'
}
gchrome() {
local opts=()
declare -A profiles
eval "$(_gchrome_get_profiles)"
if _gchrome_is_valid_profile "${1:-}"; then
opts+=("--profile-directory=$1")
shift
elif _gchrome_is_valid_profile_alias "${1:-}"; then
opts+=("--profile-directory=${profiles[$1]}")
shift
fi
if [ "${_gchrome_exec:-0}" = 1 ]; then
export GTK_THEME=""
exec /usr/bin/google-chrome --disk-cache-size=536870912 "${opts[@]}" "$@" &>/dev/null
else
( GTK_THEME="" \
/usr/bin/google-chrome --disk-cache-size=536870912 "${opts[@]}" "$@" &>/dev/null &
disown
)
fi
}
if [[ "$0" != "${BASH_SOURCE[0]}" ]]; then
# being sourced
complete -F _gchrome_make_completion gchrome
else
export _gchrome_exec=1
if _gchrome_is_valid_profile "$1" || _gchrome_is_valid_profile_alias "$1"; then
gchrome "$@"
else
gchrome "$(_gchrome_get_last_used_profile)" "$@"
fi
fi