/
launcher.sh
executable file
·173 lines (154 loc) · 3.73 KB
/
launcher.sh
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
usage() {
app=$(basename $0)
printf "$app - open URL in a specific container in Firefox.\n\n"
printf "Usage:\n"
printf "\t$app [OPTIONS] URL\n"
printf "\t$app URL [OPTIONS]\n"
printf "\t$app -h|--help\n\n"
printf "Where optional OPTIONS may include any combination of:\n"
printf "\t--COLOR\t\t\tcolor for the container (if does not exist)\n"
printf "\t--ICON\t\t\ticon for the container (if does not exist)\n"
printf " -n,\t--name=NAME\t\tcontainer name (default: domain part of the URL)\n"
printf " -s,\t--signature=SIGNATURE\tcontainer signature to prevent clickjacking; also see (1) below\n"
printf " -p,\t--pin\t\t\tpin tab\n"
printf " -r,\t--reader\t\topen tab in the reader mode\n\n"
printf "Where COLOR is one of:\n\t--blue\n\t--turquoise\n\t--green\n\t--yellow\n\t--orange\n\t--red\n\t--pink\n\t--purple\n\n"
printf "Where ICON is one of:\n\t--fingerprint\n\t--briefcase\n\t--dollar\n\t--cart\n\t--circle\n\t--gift\n\t--vacation\n\t--food\n\t--fruit\n\t--pet\n\t--tree\n\t--chill\n\n"
printf "Environment variables:\n"
printf " (1) \tOPEN_URL_IN_CONTAINER_SIGNING_KEY\tsigning key for the clickjacking prevention mechanism; if set to a non-empty value, SIGNATURE will be generated by automatically\n"
exit 1
}
assertOnlyOne() {
var="$1"
if [ -n "${!var}" ]
then
echo "Error: Only one $var allowed"
usage
fi
}
assertRequired() {
var="$1"
if [ -z "${!var}" ]
then
echo "Error: URL is a required parameter"
usage
fi
}
# code from: https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command
urlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
}
if [ -z "$FIREFOX" ]
then
FIREFOX=firefox
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
usage
;;
-n|--name)
assertOnlyOne NAME
NAME="$2"
shift
shift
;;
--name=*)
assertOnlyOne NAME
_name="$1"
NAME="${_name#--name=}"
shift
;;
-s|--signature)
assertOnlyOne SIGNATURE
SIGNATURE="$2"
shift
shift
;;
--signature=*)
assertOnlyOne SIGNATURE
_signature="$1"
SIGNATURE="${_signature#--signature=}"
shift
shift
;;
--blue|--turquoise|--green|--yellow|--orange|--red|--pink|--purple)
assertOnlyOne COLOR
COLOR="${1#--}"
shift
;;
--fingerprint|--briefcase|--dollar|--cart|--circle|--gift|--vacation|--food|--fruit|--pet|--tree|--chill)
assertOnlyOne ICON
ICON="${1#--}"
shift
;;
-p|--pin)
PIN=true
shift
;;
-r|--reader)
READER_MODE=true
shift
;;
--*|-*)
echo "Error: Unknown parameter: $1"
usage
;;
*)
assertOnlyOne URL
URL="$1"
shift
;;
esac
done
assertRequired URL
if [ -z "$NAME" ]
then
NAME=${URL#*//} # strip [method:]//
NAME=${NAME%%/*} # strip path
fi
if [ -n "$OPEN_URL_IN_CONTAINER_SIGNING_KEY" -a -z "$SIGNATURE" ]
then
SIGNATURE=$(printf "${NAME}${OPEN_URL_IN_CONTAINER_SIGNING_KEY}" | openssl dgst -sha256 | cut -d " " -f 2)
fi
URL=$(urlencode $URL)
FULL_URL="ext+container:url=${URL}&name=${NAME}"
if [ -n "$COLOR" ]
then
FULL_URL="${FULL_URL}&color=${COLOR}"
fi
if [ -n "$ICON" ]
then
FULL_URL="${FULL_URL}&icon=${ICON}"
fi
if [ -n "$PIN" ]
then
FULL_URL="${FULL_URL}&pinned=true"
fi
if [ -n "$READER_MODE" ]
then
FULL_URL="${FULL_URL}&openInReaderMode=true"
fi
if [ -n "$SIGNATURE" ]
then
FULL_URL="${FULL_URL}&signature=${SIGNATURE}"
fi
$FIREFOX $FIREFOX_ARGS "$FULL_URL"