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