1
|
#!/bin/sh
|
2
|
|
3
|
# Copyright (C) 2021 Wojtek Kosior
|
4
|
# Redistribution terms are gathered in the `copyright' file.
|
5
|
|
6
|
ENDL="
|
7
|
"
|
8
|
|
9
|
errcho() {
|
10
|
echo "$@" >&2
|
11
|
}
|
12
|
|
13
|
map_set_instr() {
|
14
|
echo "$1__$2='$3'"
|
15
|
}
|
16
|
|
17
|
map_set() {
|
18
|
eval "$(map_set_instr "$@")"
|
19
|
}
|
20
|
|
21
|
map_get() {
|
22
|
eval "echo \"\$$1__$2\""
|
23
|
}
|
24
|
|
25
|
map_del_instr() {
|
26
|
echo "unset $1__$2"
|
27
|
}
|
28
|
|
29
|
map_del() {
|
30
|
eval "$(map_del_instr "$@")"
|
31
|
}
|
32
|
|
33
|
sanitize() {
|
34
|
echo "$1" | tr /.- _
|
35
|
}
|
36
|
|
37
|
handle_export_line() {
|
38
|
if [ "x$1" = "xEXPORTS_START" ]; then
|
39
|
if [ "$STATE" = "before_block" ]; then
|
40
|
STATE="in_block"
|
41
|
fi
|
42
|
elif [ "x$1" = "xEXPORT" ]; then
|
43
|
if [ "$STATE" != "in_block" ]; then
|
44
|
return
|
45
|
fi
|
46
|
|
47
|
EXPORTCODE="${EXPORTCODE}window.killtheweb.$2 = $2;$ENDL"
|
48
|
|
49
|
PREVIOUS_FILE="$(map_get EXPORTS $2)"
|
50
|
if [ "x$PREVIOUS_FILE" != "x" ]; then
|
51
|
errcho "export $2 present in both $PREVIOUS_FILE and $FILE"
|
52
|
return 1
|
53
|
fi
|
54
|
|
55
|
map_set_instr EXPORTS $2 "$FILE"
|
56
|
|
57
|
elif [ "x$1" = "xEXPORTS_END" ]; then
|
58
|
if [ "$STATE" = "in_block" ]; then
|
59
|
STATE="after_block"
|
60
|
fi
|
61
|
fi
|
62
|
}
|
63
|
|
64
|
translate_exports() {
|
65
|
STATE="before_block"
|
66
|
EXPORTCODE=''
|
67
|
|
68
|
while read EXPORT_LINE; do
|
69
|
handle_export_line $EXPORT_LINE || return 1
|
70
|
done
|
71
|
|
72
|
map_set_instr EXPORTCODES $FILEKEY "$EXPORTCODE"
|
73
|
}
|
74
|
|
75
|
add_exports() {
|
76
|
FILE="$1"
|
77
|
FILEKEY="$(sanitize "$FILE")"
|
78
|
|
79
|
eval "$(grep -o 'EXPORT.\+' "$1" | translate_exports || exit 1)"
|
80
|
}
|
81
|
|
82
|
handle_import_line() {
|
83
|
if [ "x$1" = "xIMPORTS_START" ]; then
|
84
|
if [ "$STATE" = "before_block" ]; then
|
85
|
STATE="in_block"
|
86
|
fi
|
87
|
elif [ "x$1" = "xIMPORT" ]; then
|
88
|
if [ "$STATE" != "in_block" ]; then
|
89
|
return
|
90
|
fi
|
91
|
|
92
|
IMPORTCODE="${IMPORTCODE}const $2 = window.killtheweb.$2;$ENDL"
|
93
|
|
94
|
IMPORTS="$IMPORTS $2"
|
95
|
|
96
|
elif [ "x$1" = "xIMPORTS_END" ]; then
|
97
|
if [ "$STATE" = "in_block" ]; then
|
98
|
STATE="after_block"
|
99
|
fi
|
100
|
fi
|
101
|
}
|
102
|
|
103
|
translate_imports() {
|
104
|
STATE="before_block"
|
105
|
IMPORTCODE=''
|
106
|
IMPORTS=''
|
107
|
|
108
|
while read IMPORT_LINE; do
|
109
|
handle_import_line $IMPORT_LINE || return 1
|
110
|
done
|
111
|
|
112
|
map_set_instr IMPORTCODES $FILEKEY "$IMPORTCODE"
|
113
|
map_set_instr IMPORTS $FILEKEY "$IMPORTS"
|
114
|
}
|
115
|
|
116
|
add_imports() {
|
117
|
FILE="$1"
|
118
|
FILEKEY="$(sanitize "$FILE")"
|
119
|
|
120
|
eval "$(grep -o 'IMPORT.\+' "$1" | translate_imports || exit 1)"
|
121
|
}
|
122
|
|
123
|
compute_scripts_list_rec() {
|
124
|
local FILE="$1"
|
125
|
local FILEKEY=$(sanitize "$1")
|
126
|
|
127
|
local FILESTATE="$(map_get FILESTATES $FILEKEY)"
|
128
|
if [ "xprocessed" = "x$FILESTATE" ]; then
|
129
|
return
|
130
|
fi
|
131
|
if [ "xprocessing" = "x$FILESTATE" ]; then
|
132
|
errcho "import loop on $FILE"
|
133
|
return 1
|
134
|
fi
|
135
|
|
136
|
USED="$USED $FILEKEY"
|
137
|
|
138
|
map_set FILESTATES $FILEKEY "processing"
|
139
|
|
140
|
local IMPORT
|
141
|
for IMPORT in $(map_get IMPORTS $FILEKEY); do
|
142
|
NEXT_FILE="$(map_get EXPORTS $IMPORT)"
|
143
|
if [ "x" = "x$NEXT_FILE" ]; then
|
144
|
errcho "nothing exports $IMPORT, required by $FILE"
|
145
|
return 1
|
146
|
fi
|
147
|
if ! compute_scripts_list_rec "$NEXT_FILE"; then
|
148
|
errcho "when satisfying $IMPORT for $FILE"
|
149
|
return 1
|
150
|
fi
|
151
|
done
|
152
|
|
153
|
echo $FILE
|
154
|
map_set FILESTATES $FILEKEY "processed"
|
155
|
}
|
156
|
|
157
|
compute_scripts_list() {
|
158
|
USED=''
|
159
|
echo COMPUTED_SCRIPTS=\"exports_init.js
|
160
|
compute_scripts_list_rec "$1"
|
161
|
echo \"
|
162
|
|
163
|
for FILEKEY in $USED; do
|
164
|
map_set_instr USED $FILEKEY yes
|
165
|
done
|
166
|
}
|
167
|
|
168
|
as_json_list() {
|
169
|
while true; do
|
170
|
if [ "x" = "x$2" ]; then
|
171
|
echo -n '\\n'"\t\t\"$1\""'\\n\t'
|
172
|
return
|
173
|
fi
|
174
|
echo -n '\\n'"\t\t\"$1\","
|
175
|
shift
|
176
|
done
|
177
|
}
|
178
|
|
179
|
as_html_list() {
|
180
|
while [ "x" != "x$1" ]; do
|
181
|
echo -n '\\n'" <script src=\"/$1\"></script>"
|
182
|
shift
|
183
|
done
|
184
|
}
|
185
|
|
186
|
set_browser() {
|
187
|
if [ "x$1" = "xmozilla" -o "x$1" = "xchromium" ]; then
|
188
|
BROWSER="$1"
|
189
|
else
|
190
|
errcho "usage: $0 mozilla|chromium"
|
191
|
exit 1
|
192
|
fi
|
193
|
}
|
194
|
|
195
|
main() {
|
196
|
set_browser "$1"
|
197
|
|
198
|
SCRIPTDIRS='background html common content'
|
199
|
|
200
|
SCRIPTS=$(find $SCRIPTDIRS -name '[^.#]*.js')
|
201
|
|
202
|
for SCRIPT in $SCRIPTS; do
|
203
|
add_exports $SCRIPT
|
204
|
add_imports $SCRIPT
|
205
|
done
|
206
|
|
207
|
eval "$(compute_scripts_list background/main.js || exit 1)"
|
208
|
BGSCRIPTS="$(as_json_list $COMPUTED_SCRIPTS)"
|
209
|
eval "$(compute_scripts_list content/main.js || exit 1)"
|
210
|
CONTENTSCRIPTS="$(as_json_list $COMPUTED_SCRIPTS)"
|
211
|
eval "$(compute_scripts_list html/display-panel.js || exit 1)"
|
212
|
POPUPSCRIPTS="$(as_html_list $COMPUTED_SCRIPTS)"
|
213
|
eval "$(compute_scripts_list html/options_main.js || exit 1)"
|
214
|
OPTIONSSCRIPTS="$(as_html_list $COMPUTED_SCRIPTS)"
|
215
|
|
216
|
BUILDDIR=build_$BROWSER
|
217
|
rm -rf $BUILDDIR
|
218
|
mkdir $BUILDDIR
|
219
|
for DIR in $(find $SCRIPTDIRS -type d); do
|
220
|
mkdir -p $BUILDDIR/$DIR
|
221
|
done
|
222
|
|
223
|
CHROMIUM_KEY=''
|
224
|
GECKO_APPLICATIONS=''
|
225
|
|
226
|
if [ "$BROWSER" = "chromium" ]; then
|
227
|
CHROMIUM_KEY="\n\
|
228
|
\n\
|
229
|
// WARNING!!!\n\
|
230
|
// EACH USER SHOULD REPLACE \"key\" WITH A UNIQUE VALUE!!!\n\
|
231
|
// OTHERWISE, SECURITY CAN BE TRIVIALLY COMPROMISED!\n\
|
232
|
//\n\
|
233
|
// A unique key can be generated with:\n\
|
234
|
// $ ssh-keygen -f /path/to/new/key.pem -t rsa -b 1024\n\
|
235
|
//\n\
|
236
|
// Only relevant to users of chrome-based browsers.\n\
|
237
|
// Users of Firefox forks are safe.\n\
|
238
|
\n\
|
239
|
\"key\": \"b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAlwAAAAdzc2gtcnNhAAAAAwEAAQAAAIEA+0GT5WNmRRo8e5tL9+BmNtY6aBPwLIgbPnLShYBMSR40iYwLTsccrkwBXb3bs1o4p6q5WJugI8Lsia+GXZc/XHGFkq7D1aWiTxlJLs8z0JC2TQ2/yatYmBMchogYGeeUfP7aI7JJZwpATts+VhIvgga/4FYj+DijMIEpwdckqFEAAAII4Dh7HOA4exwAAAAHc3NoLXJzYQAAAIEA+0GT5WNmRRo8e5tL9+BmNtY6aBPwLIgbPnLShYBMSR40iYwLTsccrkwBXb3bs1o4p6q5WJugI8Lsia+GXZc/XHGFkq7D1aWiTxlJLs8z0JC2TQ2/yatYmBMchogYGeeUfP7aI7JJZwpATts+VhIvgga/4FYj+DijMIEpwdckqFEAAAADAQABAAAAgEHB5/MhEKMFOs8e1cMJ97ZiWubiUPlWpcqyQmauLUj1nspg3JTBh8AWJEVkaxuFgU5gYCHQmRjC6yUdywyziOEkFA4r/WpX4WmbIe+GQHRHhitLN0dgF8N6/fVNOoa5StTdfZqyl23pVXyepoDNjrJFKyupqPMmpwfH5lGr9RwBAAAAQG76HflB/5j8P2YgIYX6dQT4Ei0SqiIjNVy7jFJUQDKSJg/PYkedE02JZJBJPcMYxEJUxXtMgq+upamNILfkmY0AAABBAP4v0O5dqjy16xDDFzb4DPNAcw5Za9KJaXKVkUuKXMNZOKTR0RC/upjNTmttY980RKdIx5zA25dO8cx563bSDIsAAABBAP0MaOpBiai/eRmLqhlthHODa+Mur6W3uc9PyhWhgDBjLNMR/doaYeyfVKxtIiN3a+HkN++G+vbokRweQv++bhMAAAANdXJ6QGxvY2FsaG9zdAECAwQFBg==\","
|
240
|
else
|
241
|
GECKO_APPLICATIONS="\n\
|
242
|
\"applications\": {\n\
|
243
|
\"gecko\": {\n\
|
244
|
\"id\": \"{6fe13369-88e9-440f-b837-5012fb3bedec}\",\n\
|
245
|
\"strict_min_version\": \"60.0\"\n\
|
246
|
}\n\
|
247
|
},"
|
248
|
fi
|
249
|
|
250
|
sed "\
|
251
|
s^_GECKO_APPLICATIONS_^$GECKO_APPLICATIONS^
|
252
|
s^_CHROMIUM_KEY_^$CHROMIUM_KEY^
|
253
|
s^_BGSCRIPTS_^$BGSCRIPTS^
|
254
|
s^_CONTENTSCRIPTS_^$CONTENTSCRIPTS^" \
|
255
|
< manifest.json > $BUILDDIR/manifest.json
|
256
|
|
257
|
sed "s^_POPUPSCRIPTS_^$POPUPSCRIPTS^" \
|
258
|
< html/display-panel.html > $BUILDDIR/html/display-panel.html
|
259
|
|
260
|
sed "s^_OPTIONSSCRIPTS_^$OPTIONSSCRIPTS^" \
|
261
|
< html/options.html > $BUILDDIR/html/options.html
|
262
|
|
263
|
for FILE in $SCRIPTS; do
|
264
|
FILEKEY=$(sanitize "$FILE")
|
265
|
if [ "xyes" != "x$(map_get USED $FILEKEY)" ]; then
|
266
|
errcho "WARNING! $FILE not used"
|
267
|
else
|
268
|
(echo "\
|
269
|
\"use strict\";
|
270
|
|
271
|
(() => {
|
272
|
$(map_get IMPORTCODES $FILEKEY)
|
273
|
|
274
|
";
|
275
|
|
276
|
# A hack to insert the contents of default_settings.json at the appropriate location in background/main.js
|
277
|
if [ "$FILE" = "background/main.js" ]; then
|
278
|
# Uses an internal sed expression to escape and indent the JSON file for use in the external sed expression
|
279
|
sed 's/^ `DEFAULT SETTINGS`$/'"$(sed -E 's/([\\\&\/])/\\\1/g; s/^/ /; s/$/\\/' < default_settings.json) "/g < "$FILE"
|
280
|
else
|
281
|
cat $FILE
|
282
|
fi
|
283
|
|
284
|
echo "
|
285
|
|
286
|
$(map_get EXPORTCODES $FILEKEY)
|
287
|
})();") > $BUILDDIR/$FILE
|
288
|
fi
|
289
|
done
|
290
|
|
291
|
echo "\
|
292
|
window.killtheweb={};
|
293
|
window.browser = this.browser; /* fix for stupid Firefox */
|
294
|
" > $BUILDDIR/exports_init.js
|
295
|
|
296
|
cp -r icons/ copyright licenses/ $BUILDDIR
|
297
|
}
|
298
|
|
299
|
main "$@"
|