-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdates.js
More file actions
93 lines (90 loc) · 2.89 KB
/
updates.js
File metadata and controls
93 lines (90 loc) · 2.89 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
import style from 'src/utils/style.js';
import wrap from 'src/utils/2.pokemon.js';
import debugToast from 'src/utils/debugToast';
import { toast as Toast } from 'src/utils/2.toasts.js';
import semver from 'src/utils/version.js';
import { buttonCSS, scriptVersion, window } from 'src/utils/1.variables.js';
import createParser from 'src/utils/parser';
import eventManager from 'src/utils/eventManager';
import {
register,
silent,
unregister,
validate,
} from 'src/hooks/updates';
import plugin from 'src/utils/underscript';
import Translation from 'src/structures/constants/translation.ts';
// Check for script updates
wrap(() => {
style.add(`
#AlertToast h2,
#AlertToast h3 {
margin: 0;
font-size: 20px;
}
#AlertToast h3 {
font-size: 17px;
}
`);
const checker = createParser({ updateURL: 'UCProjects/UnderScript' });
const DEBUG = 'underscript.update.debug';
let updateToast;
function isNewer(data) {
const version = scriptVersion;
if (version === 'L' && !localStorage.getItem(DEBUG)) return false;
if (version.includes('-')) return semver(data.newVersion, version); // Allow test scripts to update to release
return data.newVersion !== version; // Always assume that the marked version is better
}
function compareAndToast(data) {
if (!data || !isNewer(data)) {
updateToast?.close('invalid');
return false;
}
eventManager.once(':update:finished :update:force', () => {
updateToast?.close('stale');
if (data.announce) return;
updateToast = Toast({
title: Translation.Toast('update.title'),
text: Translation.Toast('update.text').withArgs(data.newVersion),
className: 'dismissable',
buttons: [{
text: Translation.Toast('update'),
className: 'dismiss',
css: buttonCSS,
onclick() {
const url = data.url || `https://github.com/UCProjects/UnderScript/releases/download/${data.version}/undercards.user.js`;
window.open(url, 'updateUserScript', 'noreferrer');
},
}],
});
});
return true;
}
eventManager.on(':update', async (auto) => {
if (!auto) {
unregister(plugin);
}
try {
const data = await checker.getUpdateData();
const update = {
url: await checker.getDownload(data),
newVersion: await checker.getVersion(data),
time: data.assets.find(({ name }) => name.endsWith('.user.js')).updated_at,
// TODO: separate setting
announce: silent.value(),
version: scriptVersion.includes('.') ? scriptVersion : undefined,
plugin,
};
if (compareAndToast(update)) {
register(update);
}
} catch (error) {
debugToast(error);
}
});
// Toast if update pending
eventManager.on('underscript:ready', () => {
compareAndToast(validate(plugin));
eventManager.emit(':update:force');
});
});