-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdates.js
More file actions
346 lines (314 loc) · 8.96 KB
/
updates.js
File metadata and controls
346 lines (314 loc) · 8.96 KB
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
// Detect pending updates, display message, open window... allow updating.
import luxon from 'luxon';
import eventManager from 'src/utils/eventManager';
import { toast as Toast } from 'src/utils/2.toasts';
import * as menu from 'src/utils/menu';
import * as settings from 'src/utils/settings';
import each from 'src/utils/each';
import wrap from 'src/utils/2.pokemon';
import Translation from 'src/structures/constants/translation.ts';
import { buttonCSS, DAY, HOUR, scriptVersion, UNDERSCRIPT } from 'src/utils/1.variables';
import sleep from 'src/utils/sleep';
import createParser from 'src/utils/parser';
import DialogHelper from 'src/utils/DialogHelper';
import compound from 'src/utils/compoundEvent';
import { getTranslationArray } from 'src/base/underscript/translation';
import style from 'src/utils/style';
import css from './updates.css';
const CHECKING = 'underscript.update.checking'; // TODO: change to setting?
const LAST = 'underscript.update.last'; // TODO: change to setting?
const PREFIX = 'underscript.pending.';
let autoTimeout;
let toast;
style.add(css);
export const disabled = settings.register({
name: Translation.Setting('update'),
key: 'underscript.disable.updates',
category: Translation.CATEGORY_UPDATES,
});
export const silent = settings.register({
name: Translation.Setting('update.silent'),
key: 'underscript.updates.silent',
category: Translation.CATEGORY_UPDATES,
});
const keys = {
frequency: Translation.Setting('update.frequency.option').key,
toast: Translation.Toast('updater'),
button: Translation.OPEN,
checking: Translation.Toast('update.checking'),
skip: Translation.General('update.skip'),
updateNote: Translation.Menu('update.note', 1),
available: Translation.Toast('update.available', 1),
title: Translation.General('updates'),
updateCurrent: Translation.General('update.current', 1),
updateNew: Translation.General('update.new', 1),
};
const frequency = settings.register({
name: Translation.Setting('update.frequency'),
key: 'underscript.updates.frequency',
data: () => {
const tls = getTranslationArray(keys.frequency);
return [0, HOUR, DAY].map((val, i) => (tls ? [
tls[i],
val,
] : val));
},
default: HOUR,
type: 'select',
category: Translation.CATEGORY_UPDATES,
transform(value) {
return Number(value) || 0;
},
});
const dialog = new DialogHelper();
const pendingUpdates = new Map();
export function register({
plugin,
version,
...rest
}) {
const key = plugin.name || plugin;
const data = { version, ...rest };
localStorage.setItem(`${PREFIX}${key}`, JSON.stringify(data));
pendingUpdates.set(key, data);
}
// TODO: keep registry of update URL's being used? only allow one of any url
export function registerPlugin(plugin, data = {}) {
validate(plugin);
/** @type {FileParser} */
const parser = createParser(data);
const { version } = plugin;
let finished = false;
plugin.events.until(':update', async () => {
if (finished || !plugin.canUpdate) return finished;
const info = await parser.getUpdateData();
const newVersion = await parser.getVersion(info);
if (finished || !plugin.canUpdate) return finished;
if (newVersion !== version) {
const cached = pendingUpdates.get(plugin.name);
if (cached?.newVersion === newVersion) {
return false;
}
register({
plugin,
newVersion,
version,
url: await parser.getDownload(info),
});
} else {
unregister(plugin);
}
return false;
});
return () => {
finished = true;
};
}
export function validate(plugin) {
const key = plugin.name || plugin;
const existing = pendingUpdates.get(key);
if (existing) {
const version = key === UNDERSCRIPT ? scriptVersion : plugin.version;
const isValid = existing.version === undefined || existing.version === version;
const isNotUpdated = existing.newVersion !== version;
if (isNotUpdated && isValid) {
return existing;
}
unregister(plugin);
}
return false;
}
export function unregister(plugin) {
const key = plugin.name || plugin;
localStorage.removeItem(`${PREFIX}${key}`);
return pendingUpdates.delete(key);
}
function notify(text, addButton = false) {
if (toast?.exists()) {
toast.setText(`${text}`);
} else {
toast = Toast({
title: keys.toast,
text,
className: 'dismissable',
buttons: addButton ? {
text: keys.button,
className: 'dismiss',
css: buttonCSS,
onclick: open,
} : undefined,
});
}
}
async function check(auto = true) {
if (sessionStorage.getItem(CHECKING)) return;
sessionStorage.setItem(CHECKING, true);
if (autoTimeout) {
clearTimeout(autoTimeout);
autoTimeout = 0;
}
const previousState = !pendingUpdates.size;
if (!auto || !disabled.value()) {
await eventManager.async.emit(':update', auto);
}
if (!pendingUpdates.size !== previousState) {
menu.dirty();
}
function finish() {
toast?.close();
eventManager.emit(':update:finished', auto);
}
const updateFound = [...pendingUpdates.values()].filter(({ announce = true }) => announce).length;
if (updateFound) {
finish();
notify(keys.available.withArgs(updateFound), true);
} else {
notify(keys.available.withArgs(0));
const delay = !auto ? 3000 : 1000;
sleep(delay).then(finish);
}
// Setup next check, defaulting to an hour if "Page Load" is set
const timeout = frequency.value() || HOUR;
autoTimeout = setTimeout(check, timeout);
localStorage.setItem(LAST, Date.now());
sessionStorage.removeItem(CHECKING);
}
function setup() {
const last = Number(localStorage.getItem(LAST));
const now = Date.now();
const timeout = last - now + frequency.value();
if (!last || timeout <= 0) {
check();
} else {
autoTimeout = setTimeout(check, timeout);
}
const updateFound = [...pendingUpdates.values()].filter(({ announce = true }) => announce).length;
if (updateFound) {
notify(keys.available.translate(updateFound), true);
}
}
function open() {
dialog.open({
title: `${keys.title}`,
cssClass: 'underscript-dialog updates',
message: build,
});
}
menu.addButton({
text: Translation.Menu('update'),
action() {
check(false);
},
note() {
const last = Number(localStorage.getItem(LAST));
const when = last ? luxon.DateTime.fromMillis(last).toLocaleString(luxon.DateTime.DATETIME_FULL) : 'never';
return keys.updateNote.translate(when);
},
});
menu.addButton({
text: Translation.Menu('update.pending'),
action: open,
// note() {},
hidden() {
return !pendingUpdates.size;
},
});
eventManager.on(':update', (auto) => {
toast?.close();
if (auto && silent.value()) return;
notify(keys.checking);
});
// Load pending updates
each(localStorage, (data, key) => wrap(() => {
if (!key.startsWith(PREFIX)) return;
if (disabled.value()) {
localStorage.removeItem(key);
return;
}
const name = key.substring(key.lastIndexOf('.') + 1);
pendingUpdates.set(name, JSON.parse(data));
}, key));
sessionStorage.removeItem(CHECKING);
compound('underscript:ready', ':load', setup);
function build() {
let addedRefresh = false;
const container = $('<div>');
function refreshButton() {
if (addedRefresh) return;
dialog.prependButton({
label: `${Translation.General('refresh')}`,
cssClass: 'btn-success',
action() {
location.reload();
},
});
addedRefresh = true;
}
function add(data) {
const {
announce = true,
name,
newVersion,
url,
version,
} = data;
const isPlugin = name !== UNDERSCRIPT;
const skip = isPlugin && !announce;
const wrapper = $('<fieldset>')
.toggleClass('silent', skip);
const button = $('<a>')
.text(keys.updateNew.translate(newVersion))
.attr({
href: url,
rel: 'noreferrer',
target: 'updateUserScript',
})
.addClass('btn')
.toggleClass('btn-success', !skip)
.toggleClass('btn-skip', skip)
.on('click auxclick', () => {
refreshButton();
button
.removeClass()
.addClass('btn btn-primary');
})
.prepend(
$('<span class="glyphicon glyphicon-arrow-up"></span>'),
' ',
);
const silence = $('<button>')
.text(keys.skip)
.addClass('btn btn-skip')
.on('click', () => {
register({
...data,
plugin: name,
announce: false,
});
wrapper.remove();
});
container.append(wrapper.append(
$('<legend>').text(name),
$('<div>').text(keys.updateCurrent.translate(version || Translation.UNKNOWN)),
button,
announce && isPlugin && silence,
));
}
const underscript = pendingUpdates.get(UNDERSCRIPT);
if (underscript) {
add({
...underscript,
name: UNDERSCRIPT,
});
}
[...pendingUpdates.entries()].forEach(
([name, data]) => {
if (name === UNDERSCRIPT) return;
add({
...data,
name,
});
},
);
return container.children();
}