-
Notifications
You must be signed in to change notification settings - Fork 0
/
apidata.js
173 lines (143 loc) · 6.12 KB
/
apidata.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
///////////////////////////////////////////////////////////////////////////////////////
// API DATA
///////////////////////////////////////////////////////////////////////////////////////
function mustRefresh(storageLastUpdateId, dataExpire) {
let lastUpdate = new Date(store.get(storageLastUpdateId, new Date(0)));
let datenow = new Date();
let dateExpireRange = new Date(datenow.setMinutes(datenow.getMinutes() - dataExpire.totalMinutes()));
return lastUpdate <= dateExpireRange;
}
async function queryYoutubeBlacklist() {
let newYoutubeBlacklist = await fetchJson(youtubeBlacklistUrl);
if (!newYoutubeBlacklist?.length) return;
youtubeBlacklistArray = newYoutubeBlacklist.flatMap(yp => yp.videos);
store.set(storage_youtubeBlacklist, JSON.stringify(youtubeBlacklistArray));
store.set(storage_youtubeBlacklistLastUpdate, Date.now());
}
async function queryPreboucles() {
let newPreboucles = await fetchJson(prebouclesDataUrl);
if (!newPreboucles?.length) return;
preBoucleArray = newPreboucles;
store.set(storage_preBouclesData, JSON.stringify(preBoucleArray));
store.set(storage_prebouclesLastUpdate, Date.now());
}
async function queryAiLoops() {
let newAiLoops = await fetchJson(aiLoopsDataUrl);
if (!newAiLoops) return;
aiLoopData = newAiLoops;
store.set(storage_aiLoopsData, JSON.stringify(aiLoopData));
store.set(storage_aiLoopsLastUpdate, Date.now());
}
async function queryHotTopics() {
let newHotTopics = await buildHotTopics();
if (!newHotTopics) return;
hotTopicsData = newHotTopics;
store.set(storage_hotTopicsData, JSON.stringify(hotTopicsData));
store.set(storage_hotTopicsLastUpdate, Date.now());
}
async function checkUpdate() {
if (!mustRefresh(storage_lastUpdateCheck, checkUpdateExpire)) return;
const currentUserPseudo = userPseudo ?? store.get(storage_lastUsedPseudo, userId);
const bodyJson = `{"userid":"${userId}","username":"${currentUserPseudo?.toLowerCase() ?? 'anonymous'}","version":"${getCurrentScriptVersion()}"}`;
let checkRes;
await GM.xmlHttpRequest({
method: 'POST',
url: checkUpdateUrl,
data: bodyJson,
headers: { 'Content-Type': 'application/json' },
onload: (response) => { checkRes = response.responseText; },
onerror: (response) => { console.error("error : %o", response); }
});
store.set(storage_lastUpdateCheck, Date.now());
return checkRes;
}
async function updateUser() {
if (!mustRefresh(storage_lastUpdateUser, updateUserExpire)) return;
const currentUserPseudo = userPseudo ?? store.get(storage_lastUsedPseudo, userId);
const settings = getStorageJson(false, storage_excluded_user_Keys);
const body =
{
userid: userId,
username: currentUserPseudo?.toLowerCase() ?? 'anonymous',
version: getCurrentScriptVersion(),
settings: settings
};
const bodyJson = JSON.stringify(body);
await GM.xmlHttpRequest({
method: 'POST',
url: updateUserUrl,
data: bodyJson,
headers: { 'Content-Type': 'application/json' },
onerror: (response) => { console.error("error : %o", response); }
});
store.set(storage_lastUpdateUser, Date.now());
}
async function sendDiagnostic(elapsed, exception) {
if (!exception && !mustRefresh(storage_DiagnosticLastUpdate, diagnosticExpire)) return;
const currentUserPseudo = userPseudo ?? store.get(storage_lastUsedPseudo, userId);
const settings = getStorageJson(false, storage_excluded_user_Keys);
const body =
{
userid: userId,
username: currentUserPseudo?.toLowerCase() ?? 'anonymous',
version: getCurrentScriptVersion(),
elapsed: elapsed,
location: window.location.href,
settings: settings,
...(exception && { exception: stringifyError(exception) }),
};
const bodyJson = JSON.stringify(body);
if (!bodyJson || bodyJson === '{}') return;
await GM.xmlHttpRequest({
method: 'POST',
url: diagnosticUrl,
data: bodyJson,
headers: { 'Content-Type': 'application/json' },
onerror: (response) => { console.error("error : %o", response); }
});
store.set(storage_DiagnosticLastUpdate, Date.now());
}
async function parseYoutubeBlacklistData(forceUpdate) {
youtubeBlacklistArray = JSON.parse(store.get(storage_youtubeBlacklist, storage_youtubeBlacklist_default));
if (!youtubeBlacklistArray?.length ||
forceUpdate ||
mustRefresh(storage_youtubeBlacklistLastUpdate, youtubeBlacklistRefreshExpire)) {
await queryYoutubeBlacklist();
}
if (youtubeBlacklistArray?.length) youtubeBlacklistReg = buildArrayRegex(youtubeBlacklistArray);
}
async function parsePreboucleData(forceUpdate) {
preBoucleArray = JSON.parse(store.get(storage_preBouclesData, storage_preBouclesData_default));
if (!preBoucleArray?.length ||
forceUpdate ||
mustRefresh(storage_prebouclesLastUpdate, prebouclesRefreshExpire)) {
await queryPreboucles();
}
if (preBoucleArray?.length) loadPreBouclesStatuses();
}
async function parseAiLoopData(forceUpdate) {
aiLoopData = JSON.parse(store.get(storage_aiLoopsData, storage_aiLoopsData_default));
if (!aiLoopData ||
forceUpdate ||
mustRefresh(storage_aiLoopsLastUpdate, aiLoopsRefreshExpire)) {
await queryAiLoops();
}
if (!aiLoopData) return;
const dataVersion = parseInt(aiLoopData.version ?? '1');
if (dataVersion === 1 && Array.isArray(aiLoopData)) {
aiLoopSubjectReg = buildEntityRegex(aiLoopData.map(l => l.title), true);
aiLoopAuthorReg = buildEntityRegex(aiLoopData.map(l => l.author), false);
}
else if (dataVersion === 2) {
aiLoopSubjectReg = buildEntityRegex(aiLoopData.titles, true);
aiLoopAuthorReg = buildEntityRegex(aiLoopData.authors, false);
}
}
async function parseHotTopicsData(forceUpdate) {
hotTopicsData = JSON.parse(store.get(storage_hotTopicsData, storage_hotTopicsData_default));
if (!hotTopicsData ||
forceUpdate ||
mustRefresh(storage_hotTopicsLastUpdate, hotTopicsRefreshExpire)) {
await queryHotTopics();
}
}