forked from ylh/tiddlymarker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
395 lines (369 loc) · 11 KB
/
background.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* this is some particularly spicy garbage. tl;dr we can't await anything in the
user input handler if we hope to call openPopup(), which is rather crucial
to the whole “conditional popup” “quick mode” *thing*. so, instead of
accessing the appropriate storage in the input handler, we close over these
globals in the storage update handlers for local.state and sync.quickmode.
we fire off some fake updates to the values to make sure those closures run,
then in the input handler we use these globals and pretend we've accessed
storage.
with any luck once this addon is stable i will never write another line of
this hideous language as long as i live */
let st, qm;
const popup_able = b => browser.browserAction.setPopup({popup: b ? null : ""});
const badge = ({text, fg, bg}) => {
browser.browserAction.setBadgeText({text: text});
browser.browserAction.setBadgeTextColor({color: fg});
browser.browserAction.setBadgeBackgroundColor({color: bg});
};
const union = (a, b) => a.filter(x => b.includes(x));
const catch_bookmark = async () => {
let error = await do_bookmark();
if (error != null) {
browser.storage.local.set({
error: error,
state: "failure"
});
} else {
browser.storage.local.set({state: "done"});
}
}
const do_bookmark = async () => {
const prefs = await browser.storage.sync.get(defaults.sync),
local = await browser.storage.local.get(Object.keys(defaults.local)),
bm = await browser.storage.local.get(Object.keys(tab_reads)),
{rawtitle, title, url, icon} = bm;
let ffo, bfo;
if (icon !== undefined) {
try {
ffo = (eval(`({data, hash, mime, datauri, ext}) => {
${prefs.favicon_fmt}
}`))(icon);
} catch (e) {
return {
errortitle: "FORMAT ERROR",
details: e.toString(),
advice: "Make sure <code>favicon_fmt</code> is well-formed."
};
}
}
try {
bfo = (eval(`(rawtitle, title, url, icon) => {
${prefs.bookmark_fmt}
}`))(rawtitle, title, url, ffo);
} catch (e) {
return {
errortitle: "FORMAT ERROR",
details: e.toString(),
advice: "Make sure <code>bookmark_fmt</code> is well-formed."
};
}
return send_bookmark(prefs, local, bfo, ffo);
};
const send_bookmark = async (prefs, local, bfo, ffo) => {
const sanity = result => {
if (do_you_even(result, "fields")) {
const {fields, ...rest} = result;
if (union(Object.keys(fields), Object.keys(rest)).length !== 0) {
return "Property <code>fields</code> contains properties that "
+ "override others";
}
}
if (!do_you_even(result, "title"))
return "No property <code>title</code> found in "
+ JSON.stringify(result);
return null;
};
const merge = (mode, tiddler) => {
if (mode !== "webserver" && tiddler !== undefined) {
const {fields, ...rest} = tiddler;
return {...fields, ...rest};
}
return tiddler;
};
let bfs = sanity(bfo);
if (bfs !== null) {
return {
errortitle: "FORMAT ERROR",
reason: bfs,
advice: "Correct <code>bookmark_fmt</code> accordingly."
};
}
bfo = merge(prefs.savingmode, bfo);
if (prefs.favicon_separate && ffo !== undefined) {
let ffs = sanity(ffo);
if (ffs !== null) {
return {
errortitle: "FORMAT ERROR",
reason: ffs,
advice: "Correct <code>favicon_fmt</code> accordingly."
};
}
ffo = merge(prefs.savingmode, ffo);
}
return sends[prefs.savingmode](prefs, local, bfo, ffo);
};
const tiddler_blob = tiddlers => new Blob([
new TextEncoder().encode(JSON.stringify(tiddlers, null, "\t")).buffer
], {type: "application/json"});
const addr_of = (prefs, tiddler) =>
`${prefs.address}/recipes/default/tiddlers/` +
`${encodeURIComponent(tiddler.title)}`;
const status_of = xhr => `${xhr.status} ${xhr.statusText}`;
const prefab_xhr = (reject, act) => {
let xhr = new XMLHttpRequest();
xhr.onerror = _e => reject({
errortitle: "NETWORK ERROR",
details: `Could not ${act}`,
advice: "Ensure that the server is running, and you have a "
+ "working connection."
});
xhr.onloadend = _e => reject({
errortitle: "UNKNOWN ERROR",
details: `Request terminated while attempting to ${act}`
});
return xhr;
};
const authopen = (xhr, prefs, method, url) => xhr.open(method, url, true,
prefs.auth ? prefs.username : null,
prefs.auth ? prefs.password : null
);
const put_tiddler = (resolve, reject, prefs, tiddler, desc) => {
const act = `put ${desc} tiddler`;
let put = prefab_xhr(reject, act);
authopen(put, prefs, 'PUT', addr_of(prefs, tiddler));
put.setRequestHeader('X-Requested-With', 'TiddlyWiki');
put.onload = function(_e) {
if (this.status === 204)
return resolve(null);
if (this.status === 401)
return reject({
errortitle: "PERMISSION ERROR",
reason: `Could not ${act}`,
details: status_of(this),
advice: "Ensure your credentials confer write permissions."
});
return reject(status_of(this));
};
put.send(JSON.stringify(tiddler));
};
const check_tiddler = (resolve, reject, prefs, tiddler, desc, ex, ne) => {
const act = `check for ${desc} tiddler`;
let get = prefab_xhr(reject, act);
authopen(get, prefs, 'GET', addr_of(prefs, tiddler));
get.onload = function(_e) {
if (this.status === 200)
return ex();
if (this.status === 404)
return ne();
if (this.status === 401)
return reject({
errortitle: "PERMISSION ERROR",
reason: `Could not ${act}`,
details: status_of(this),
advice: prefs.auth
? "Ensure your credentials are valid."
: "Enable authentication in preferences."
});
return reject(status_of(this));
};
get.send();
};
const sends = {
download: async (prefs, local, bfo, ffo) => {
let tiddlers = (ffo !== undefined) ? [bfo, ffo] : ffo,
url = URL.createObjectURL(tiddler_blob(tiddlers)),
ret = null;
try {
let id = await browser.downloads.download({
url: url,
saveAs: true,
filename: `${bfo.title.replace(/[^A-Za-z0-9._-]/g, "_")}.json`
});
let d2s = delta => {
if (delta.id === id && delta.state.current === "complete") {
browser.downloads.onChanged.removeListener(d2s);
URL.revokeObjectURL(url);
}
};
browser.downloads.onChanged.addListener(d2s);
} catch (e) {
URL.revokeObjectURL(url);
ret = {
errortitle: "DOWNLOAD INTERRUPTED",
details: e.toString(),
advice: "Try again."
};
}
return ret;
},
webserver: (prefs, local, bfo, ffo) => new Promise((resolve, reject) => {
const badurl = (s, a = "Correct the server address accordingly") =>
reject({
errortitle: "CONFIGURATION ERROR",
details: s,
advice: a
});
let u;
try {
u = new URL(prefs.address);
} catch {
return badurl("Could not parse as URL");
}
if (u.hash !== "")
return badurl("Unexpected hash fragment")
if (u.search !== "")
return badurl("Unexpected query string")
if (u.password !== "" || u.password)
return badurl(
"Unexpected username or password in server address",
"Move authentication to its respective fields."
);
if (prefs.safety && u.protocol === "http:"
&& u.hostname !== "localhost" && u.hostname !== "127.0.0.1"
&& u.hostname !== "::1")
return reject({
errortitle: "UNSAFE OPERATION",
details: "Refusing to send plaintext to non-localhost address",
advice: "If this is intentional, uncheck \"Safety\"."
})
if (u.protocol !== "http:" && u.protocol !== "https:")
return badurl("Unexpected or no protocol specified");
return resolve(null);
}).then(_ => new Promise((resolve, reject) => {
let getstatus = prefab_xhr(reject, "get server status");
getstatus.responseType = "json";
authopen(getstatus, prefs, 'GET', `${prefs.address}/status`);
getstatus.onload = function(_e) {
if (this.status === 401 || this.status === 403)
return resolve({
errortitle: "PERMISSION ERROR",
details: status_of(this),
advice: "Ensure that your credentials are correct."
});
if (this.response !== null
&& this.response.hasOwnProperty("tiddlywiki_version")) {
return resolve(null);
}
return reject({
errortitle: "SERVER ERROR",
details: "Received unexpected server status",
advice: "Ensure that the configured address points to a "
+ "TiddlyWiki server."
});
};
getstatus.send();
})).then(_ => new Promise((resolve, reject) => ffo === undefined
? resolve(false)
: check_tiddler(
resolve, reject, prefs, ffo, "favicon",
() => resolve(false),
() => resolve(true)
)
)).then(do_fav => new Promise((resolve, reject) =>
check_tiddler(
resolve, reject, prefs, bfo, "bookmark",
() => reject({
errortitle: "REFUSING TO SAVE",
details: "Bookmark with title already exists"
}),
() => resolve(do_fav)
)
)).then(do_fav => new Promise((resolve, reject) => do_fav
? put_tiddler(resolve, reject, prefs, ffo, "favicon")
: resolve(null)
)).then(_ => new Promise((resolve, reject) =>
put_tiddler(resolve, reject, prefs, bfo, "bookmark")
)).catch(e => e.hasOwnProperty("errortitle") ? e : {
errortitle: "UNKNOWN ERROR",
details: e.toString()
})/*,
tabover: (prefs, local, bfo, ffo) => new Promise((resolve, reject) => {
})*/
};
/* this seemed like it was going to be bigger and justify its scaffolding more
but oh well whatever */
const handler_tree = {
sync: {
quickmode: (changes, change) => {
qm = change.newValue;
}
},
local: {
state: (changes, change) => {
let nv = st = change.newValue;
badge({
ready: {text: "", fg: null, bg: null},
unfinished: {text: "!", fg: "white", bg: "#F80B"},
working: {text: "…", fg: "white", bg: "#888B"},
failure: {text: "✕", fg: "white", bg: "#F00B"},
done: {text: "✓", fg: "white", bg: "#0F08"}
}[nv]);
switch (nv) {
case "done":
browser.alarms.create("done", {when: Date.now() + 3000});
break;
case "ready":
browser.alarms.clear("done");
break;
case "working":
catch_bookmark();
break;
}
}
}
};
browser.storage.onChanged.addListener((changes, area) => {
const a = handler_tree[area];
for (let [k, v] of Object.entries(changes)) {
if (a.hasOwnProperty(k))
a[k](changes, v);
}
});
browser.browserAction.onClicked.addListener(() => {
switch (st) {
case "unfinished":
break;
case "working":
return;
case "failure":
break;
case "done":
st = "ready"; /* so as to avoid waiting on an async */
browser.storage.local.set({state: "ready"});
case "ready":
if (!qm)
break;
(async () => {
await Promise.all(Object.keys(tab_reads).map(
tab_read(await current_tab())
));
st = "working";
browser.storage.local.set({state: "working"});
})();
return;
}
popup_able(true);
browser.browserAction.openPopup();
popup_able(false);
});
browser.alarms.onAlarm.addListener(info => {
if (info.name === "done" && st === "done")
browser.storage.local.set({state: "ready"});
});
popup_able(false);
(async () => {
if (await pref_of("justinstalled")) {
browser.runtime.openOptionsPage();
}
if (await local_of("state") === "working")
browser.storage.local.set({
state: error,
error: {
errortitle: "INTERRUPTED",
details: "Browser quit while saving bookmark"
}
});
else
await fake_update("local", "state");
await fake_update("sync", "quickmode");
})();