-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathamqCustomListExport.user.js
More file actions
355 lines (301 loc) · 10.1 KB
/
Copy pathamqCustomListExport.user.js
File metadata and controls
355 lines (301 loc) · 10.1 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
347
348
349
350
351
352
353
354
355
// ==UserScript==
// @name AMQ Custom List Export
// @namespace http://tampermonkey.net/
// @version 1.0.0
// @description Export Song Library custom lists as AMQ-compatible JSON
// @author 4Lajf
// @match https://animemusicquiz.com/*
// @match https://*.animemusicquiz.com/*
// @require https://github.com/joske2865/AMQ-Scripts/raw/master/common/amqScriptInfo.js
// @grant none
// ==/UserScript==
"use strict";
(function () {
if (typeof Listener === "undefined") return;
const SCRIPT_PREFIX = "[AMQ Custom List Export]";
const BUTTON_ID = "amqCustomListExportButton";
const MAX_SETUP_ATTEMPTS = 240;
const SETUP_INTERVAL_MS = 500;
let setupAttempts = 0;
let setupInterval = null;
let containerObserver = null;
bootstrap();
function bootstrap() {
console.log(`${SCRIPT_PREFIX} Booting...`);
setupInterval = setInterval(() => {
if ($("#loadingScreen").hasClass("hidden")) {
clearInterval(setupInterval);
waitForGameObjects();
}
}, SETUP_INTERVAL_MS);
}
function waitForGameObjects() {
const waitInterval = setInterval(() => {
setupAttempts += 1;
if (isReady()) {
clearInterval(waitInterval);
initialize();
return;
}
if (setupAttempts >= MAX_SETUP_ATTEMPTS) {
clearInterval(waitInterval);
console.warn(`${SCRIPT_PREFIX} Timed out while waiting for AMQ objects.`);
}
}, SETUP_INTERVAL_MS);
}
function isReady() {
return !!(window.customListHandler && window.libraryCacheHandler);
}
function initialize() {
console.log(`${SCRIPT_PREFIX} Initializing...`);
mountExportButton();
startUiWatcher();
}
function startUiWatcher() {
const refreshButton = () => {
mountExportButton();
updateButtonVisibility();
};
const shortWarmup = setInterval(refreshButton, 500);
setTimeout(() => clearInterval(shortWarmup), 60_000);
if (containerObserver) {
containerObserver.disconnect();
}
containerObserver = new MutationObserver(() => {
refreshButton();
});
containerObserver.observe(document.body, {
childList: true,
subtree: true
});
}
function mountExportButton() {
const backButton = $("#elCustomListBackButton");
if (!backButton.length) return;
let exportButton = $(`#${BUTTON_ID}`);
if (!exportButton.length) {
exportButton = backButton.clone();
exportButton.attr("id", BUTTON_ID);
exportButton.removeClass("hide disabled");
exportButton.off("click");
const icon = exportButton.find("i");
if (icon.length) {
icon.attr("class", "fa fa-download");
}
const textContainer = exportButton.find("div, span").first();
if (textContainer.length) {
textContainer.text("Export");
} else {
exportButton.text("Export");
}
exportButton.on("click", (event) => {
event.preventDefault();
event.stopPropagation();
handleExportClick();
});
backButton.after(exportButton);
console.log(`${SCRIPT_PREFIX} Export button mounted.`);
}
}
function updateButtonVisibility() {
const exportButton = $(`#${BUTTON_ID}`);
if (!exportButton.length) return;
const activeList = getActiveList();
exportButton.toggle(!!activeList);
}
function getActiveList() {
if (!window.customListHandler) return null;
return customListHandler.activeCustomList || null;
}
function handleExportClick() {
const activeList = getActiveList();
if (!activeList) {
showMessage("Export Failed", "Open a custom list first, then click Export.");
return;
}
const payload = buildAmqCompatibleExport(activeList);
if (!payload.songs.length) {
showMessage(
"Nothing To Export",
"No songs found in this custom list. Add songs or anime entries first."
);
return;
}
const filename = getOfficialLikeFilename();
downloadJson(payload, filename);
showMessage(
"Export Complete",
`Exported ${payload.songs.length} songs from "${escapeHtmlSafe(activeList.name || "Custom List")}" as AMQ-compatible JSON.`
);
}
function buildAmqCompatibleExport(customList) {
const seenAnnSongIds = new Set();
const songs = [];
const unresolved = [];
const addAnnSongId = (annSongId) => {
if (!annSongId || seenAnnSongIds.has(annSongId)) return;
seenAnnSongIds.add(annSongId);
const annSongEntry = libraryCacheHandler.getCachedAnnSongEntry(annSongId);
if (!annSongEntry) {
unresolved.push(annSongId);
return;
}
const animeEntry = libraryCacheHandler.getCachedAnime(annSongEntry.annId);
songs.push(createFullSongHistoryLikeEntry(songs.length + 1, annSongEntry, animeEntry));
};
if (customList.songMap && typeof customList.songMap.forEach === "function") {
customList.songMap.forEach((_, annSongId) => addAnnSongId(annSongId));
}
if (customList.animeMap && typeof customList.animeMap.forEach === "function") {
customList.animeMap.forEach((_, annId) => {
const anime = libraryCacheHandler.getCachedAnime(annId);
if (!anime || !anime.songs) return;
["OP", "ED", "INS"].forEach((type) => {
const entries = anime.songs[type] || [];
entries.forEach((songEntry) => addAnnSongId(songEntry.annSongId));
});
});
}
unresolved.forEach((annSongId) => {
songs.push(createSimplifiedSongEntry(songs.length + 1, annSongId));
});
return {
roomName: customList.name || "AMQ Custom List",
startTime: new Date().toString(),
songs
};
}
function createFullSongHistoryLikeEntry(songNumber, annSongEntry, animeEntry) {
const song = annSongEntry?.songEntry || {};
const siteIds = extractSiteIds(animeEntry, annSongEntry);
const names = extractAnimeNames(animeEntry);
const altAnimeNames = extractAltAnimeNames(animeEntry, names);
const typeNumber = Number.isFinite(annSongEntry?.number) ? annSongEntry.number : 0;
return {
songNumber,
songInfo: {
animeNames: {
english: names.english,
romaji: names.romaji
},
altAnimeNames,
altAnimeNamesAnswers: [],
artist: extractDisplayName(song.artist),
composerInfo: toPersonInfo(song.composer),
arrangerInfo: toPersonInfo(song.arranger),
songName: song.name || "",
type: annSongEntry?.type || 0,
typeNumber,
annId: annSongEntry?.annId || animeEntry?.annId || null,
annSongId: annSongEntry?.annSongId || null,
animeScore: null,
animeType: "",
vintage: "",
animeDifficulty: 0,
siteIds,
seasonInfo: null,
animeTags: [],
animeGenre: []
},
answer: "",
correctGuess: 0,
wrongGuess: false,
correctCount: 0,
wrongCount: 0,
startPoint: 0,
videoLength: 90,
annSongId: annSongEntry?.annSongId || null
};
}
function createSimplifiedSongEntry(songNumber, annSongId) {
return {
songNumber,
annSongId,
startPoint: 0,
guessTime: null,
extraGuessTime: null
};
}
function extractAnimeNames(animeEntry) {
const mainNames = animeEntry?.mainNames || {};
const english = mainNames.EN || mainNames.JA || animeEntry?.mainName || "";
const romaji = mainNames.JA || mainNames.EN || animeEntry?.mainName || "";
return { english, romaji };
}
function extractAltAnimeNames(animeEntry, names) {
const altNames = [];
if (Array.isArray(animeEntry?.names)) {
animeEntry.names.forEach((entry) => {
if (entry && typeof entry.name === "string" && entry.name.trim()) {
altNames.push(entry.name.trim());
}
});
}
if (!altNames.length) {
if (names.english) altNames.push(names.english);
if (names.romaji && names.romaji !== names.english) altNames.push(names.romaji);
}
return Array.from(new Set(altNames));
}
function extractSiteIds(animeEntry, annSongEntry) {
const annId = annSongEntry?.annId || animeEntry?.annId || null;
return {
annId,
malId: null,
kitsuId: null,
aniListId: null,
annSongId: annSongEntry?.annSongId || null
};
}
function toPersonInfo(personLike) {
if (!personLike) {
return { artistId: null, groupId: null, name: "" };
}
return {
artistId: Number.isFinite(personLike.songArtistId) ? personLike.songArtistId : null,
groupId: Number.isFinite(personLike.songGroupId) ? personLike.songGroupId : null,
name: extractDisplayName(personLike)
};
}
function extractDisplayName(entity) {
if (!entity) return "";
if (typeof entity.name === "string") return entity.name;
if (Array.isArray(entity.names) && entity.names.length) {
const first = entity.names.find((name) => typeof name === "string" && name.trim());
if (first) return first;
}
return "";
}
function getOfficialLikeFilename() {
const now = new Date();
const pad = (n) => String(n).padStart(2, "0");
return `amq_song_expoert-${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}_${pad(now.getHours())}-${pad(now.getMinutes())}-${pad(now.getSeconds())}.json`;
}
function downloadJson(data, filename) {
const blob = new Blob([JSON.stringify(data)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
function showMessage(title, content) {
if (window.messageDisplayer && typeof messageDisplayer.displayMessage === "function") {
messageDisplayer.displayMessage(title, content);
} else {
alert(`${title}\n${content.replace(/<[^>]*>/g, "")}`);
}
}
function escapeHtmlSafe(input) {
const text = String(input ?? "");
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
})();