forked from Animorphs/MAL-English-Titles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAL_English_Titles.user.js
672 lines (629 loc) · 23.6 KB
/
MAL_English_Titles.user.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// ==UserScript==
// @name MAL English Titles
// @version 2.0.8
// @description Add English Titles to various MyAnimeList pages, whilst still displaying Japanese Titles
// @author Animorphs
// @grant GM_setValue
// @grant GM_getValue
// @namespace https://github.com/Animorphs/MAL-English-Titles
// @match https://myanimelist.net/*
// @updateURL https://raw.githubusercontent.com/Animorphs/MAL-English-Titles/master/MAL_English_Titles.user.js
// @downloadURL https://raw.githubusercontent.com/Animorphs/MAL-English-Titles/master/MAL_English_Titles.user.js
// ==/UserScript==
// Get Japanese titles from page, and send to be translated (addTranslation)
function translate()
{
const LOCATION_HREF = location.href;
const URL_REGEX = /https:\/\/myanimelist\.net\/(anime|manga)\/([1-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)\/?.*/;
const URL_PHP_REGEX = /https:\/\/myanimelist\.net\/(anime|manga)\.php\?id\=([1-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)\/?.*/;
// Anime/Manga Page (store only, don't display)
if (URL_REGEX.test(LOCATION_HREF) || URL_PHP_REGEX.test(LOCATION_HREF))
{
const TITLE_HTML = document.getElementsByClassName('title-english')[0];
const ID = LOCATION_HREF.includes('.php') ? LOCATION_HREF.split('id=')[1] : LOCATION_HREF.split('/')[4];
const MEDIA_TYPE = LOCATION_HREF.includes('/anime') ? "anime" : "manga";
if (TITLE_HTML)
{
const TITLE = TITLE_HTML.innerText;
console.log('Updated ${MEDIA_TYPE} ${ID}: ${TITLE}');
MEDIA_TYPE == 'anime' ? storeAnime(ID, TITLE) : storeManga(ID, TITLE);
}
else if (storedAnime[ID][0] === '' || !storedAnime.hasOwnProperty(ID))
{
console.log('Updated ${MEDIA_TYPE} ${ID}');
MEDIA_TYPE == 'anime' ? storeAnime(ID, '') : storeManga(ID, '');
}
}
// Anime Top
else if (LOCATION_HREF.includes('https://myanimelist.net/topanime.php'))
{
let results = document.getElementsByClassName('fl-l fs14 fw-b anime_ranking_h3');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = results[i].children[0].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = '.fl-l.fs14.fw-b.anime_ranking_h3 > a[href="' + urlDecoded + '"]';
addTranslation('anime', i, url, id, selector);
}
}
}
// Manga Top
else if (LOCATION_HREF.includes('https://myanimelist.net/topmanga.php'))
{
let results = document.getElementsByClassName('hoverinfo_trigger fs14 fw-b');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('manga' + i))
{
let url = results[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].hoverinfo_trigger.fs14.fw-b';
addTranslation('manga', i, url, id, selector);
}
}
}
// Anime List and Manga List
else if (LOCATION_HREF.includes('https://myanimelist.net/animelist') || LOCATION_HREF.includes('https://myanimelist.net/mangalist'))
{
let type = LOCATION_HREF.substring(24, 29);
let results = document.querySelectorAll('tbody:not([style]) .data.title');
function processResults(tempResults)
{
for (let i = 0; i < tempResults.length; i++)
{
let url = tempResults[i].children[0].href;
let urlShort = url.slice(23);
let urlShortDecoded = decodeURIComponent(urlShort);
let id = url.split('/')[4];
let selector = '.data.title > a[href="' + urlShortDecoded + '"]';
addTranslation(type, i, url, id, selector);
}
}
function attachMutationObserver(listTable)
{
new MutationObserver(function(mutationsList, observer)
{
mutationsList.forEach(function(mutation)
{
processResults(
Array.from(
mutation.addedNodes,
(addedNode) => addedNode.children[0].children[3]
)
);
});
if ((listTable.children.length - 1) % 150 !== 0)
{
observer.disconnect();
}
}).observe(
listTable,
{childList: true}
);
}
let table = document.querySelector('table');
if (results.length)
{
processResults(results);
if (results.length === 150)
{
attachMutationObserver(table);
}
}
else if (table)
{
new MutationObserver(function(mutationsList, observer)
{
mutationsList.some(function(mutation)
{
return Array.from(mutation.addedNodes).some(function(addedNode)
{
if (addedNode.tagName === 'TABLE')
{
let results = addedNode.querySelectorAll('.data.title');
processResults(results);
if (results.length === 150)
{
attachMutationObserver(addedNode);
}
observer.disconnect();
return true;
}
});
});
}).observe(
table.parentElement,
{childList: true}
);
}
}
// Search
else if (LOCATION_HREF.includes('https://myanimelist.net/search/'))
{
// Anime Results
let resultsAnime = document.querySelectorAll('[class="hoverinfo_trigger fw-b fl-l"][href*="/anime/"]');
for (let i = 0; i < resultsAnime.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = resultsAnime[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].hoverinfo_trigger.fw-b.fl-l';
addTranslation('anime', i, url, id, selector, true);
}
}
// Manga Results
let resultsManga = document.querySelectorAll('[class="hoverinfo_trigger fw-b"][href*="/manga/"]');
for (let i = 0; i < resultsManga.length; i++)
{
if (!document.getElementById('manga' + i))
{
let url = resultsManga[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].hoverinfo_trigger.fw-b';
addTranslation('manga', i, url, id, selector);
}
}
}
// Anime Search
else if (LOCATION_HREF.includes('https://myanimelist.net/anime.php?q') || LOCATION_HREF.includes('https://myanimelist.net/anime.php?cat'))
{
let results = document.getElementsByClassName('hoverinfo_trigger fw-b fl-l');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = results[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].hoverinfo_trigger.fw-b.fl-l';
addTranslation('anime', i, url, id, selector, true);
}
}
}
// Manga Search
else if (LOCATION_HREF.includes('https://myanimelist.net/manga.php?q') || LOCATION_HREF.includes('https://myanimelist.net/manga.php?cat'))
{
let results = document.getElementsByClassName('hoverinfo_trigger fw-b');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('manga' + i))
{
let url = results[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].hoverinfo_trigger.fw-b';
addTranslation('manga', i, url, id, selector);
}
}
}
// Anime Seasonal
else if (LOCATION_HREF.includes('https://myanimelist.net/anime/season'))
{
let results = document.getElementsByClassName('link-title');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = results[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].link-title';
addTranslation('anime', i, url, id, selector, false, true);
}
}
}
// Anime Genres
else if (LOCATION_HREF.includes('https://myanimelist.net/anime/genre'))
{
// Seasonal View
if (document.getElementsByClassName('js-btn-view-style seasonal on')[0])
{
let results = document.getElementsByClassName('link-title');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = results[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].link-title';
addTranslation('anime', i, url, id, selector, true, true);
}
}
}
// List View
else if (document.getElementsByClassName('js-btn-view-style list on')[0])
{
let results = document.getElementsByClassName('hoverinfo_trigger fw-b');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = results[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].hoverinfo_trigger.fw-b';
addTranslation('anime', i, url, id, selector);
}
}
}
}
// Manga Genres
else if (LOCATION_HREF.includes('https://myanimelist.net/manga/genre'))
{
// Seasonal View
if (document.getElementsByClassName('js-btn-view-style seasonal on')[0])
{
let results = document.getElementsByClassName('link-title');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('manga' + i))
{
let url = results[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].link-title';
addTranslation('manga', i, url, id, selector, false, true);
}
}
}
// List View
else if (document.getElementsByClassName('js-btn-view-style list on')[0])
{
let results = document.getElementsByClassName('hoverinfo_trigger fw-b');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('manga' + i))
{
let url = results[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].hoverinfo_trigger.fw-b';
addTranslation('manga', i, url, id, selector);
}
}
}
}
// Anime Producers
else if (LOCATION_HREF.includes('https://myanimelist.net/anime/producer'))
{
// Tile View
if (document.getElementsByClassName('js-btn-view-style2 tile on')[0])
{
let results = document.getElementsByClassName('seasonal-anime js-seasonal-anime js-anime-type-all ');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = results[i].children[0].children[0].href
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = '.seasonal-anime.js-seasonal-anime.js-anime-type-all > .title > a[href="' + urlDecoded + '"]';
addTranslation('anime', i, url, id, selector, false, false, true);
}
}
}
//
// Seasonal View
if (document.getElementsByClassName('js-btn-view-style2 seasonal on')[0])
{
let results = document.getElementsByClassName('link-title');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = results[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].link-title';
addTranslation('anime', i, url, id, selector, false, true);
}
}
}
// List View
else if (document.getElementsByClassName('js-btn-view-style2 list on')[0])
{
let results = document.getElementsByClassName('seasonal-anime js-seasonal-anime js-anime-type-all');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = results[i].children[0].children[0].children[0].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = '.spaceit_pad > a[href="' + urlDecoded + '"]';
addTranslation('anime', i, url, id, selector);
}
}
}
}
// Anime Shared
else if (LOCATION_HREF.includes('https://myanimelist.net/shared.php') && !LOCATION_HREF.includes('&type=manga'))
{
let results = document.querySelectorAll('[href*="/anime/"]:not(.Lightbox_AddEdit):not([href*="anime/season"])');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = results[i].href;
let urlShort = url.slice(23);
let urlShortDecoded = decodeURIComponent(urlShort);
let id = url.split('/')[4];
let selector = 'a[href="' + urlShortDecoded + '"]';
addTranslation('anime', i, url, id, selector);
}
}
}
// Manga Shared
else if (LOCATION_HREF.includes('https://myanimelist.net/shared.php') && LOCATION_HREF.includes('&type=manga'))
{
let results = document.querySelectorAll('[href*="/manga/"]:not(.Lightbox_AddEdit)');
for (let i = 0; i < results.length; i++)
{
if (!document.getElementById('manga' + i))
{
let url = results[i].href;
let urlShort = url.slice(23);
let urlShortDecoded = decodeURIComponent(urlShort);
let id = url.split('/')[4];
let selector = 'a[href="' + urlShortDecoded + '"]';
addTranslation('manga', i, url, id, selector);
}
}
}
// History
else if (LOCATION_HREF.includes('https://myanimelist.net/history'))
{
// Anime Results
let resultsAnime = document.querySelectorAll('[href*="/anime.php?id="]');
let animeIds = [];
for (let i = 0; i < resultsAnime.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = resultsAnime[i].href;
let urlShort = url.slice(23);
let urlShortDecoded = decodeURIComponent(urlShort);
let id = url.split('=')[1];
let selector = 'a[href="' + urlShortDecoded + '"]';
if (!animeIds.includes(id))
{
addTranslation('anime', i, url, id, selector);
}
animeIds.push(id);
}
}
// Manga Results
let resultsManga = document.querySelectorAll('[href*="/manga.php?id="]');
let mangaIds = [];
for (let i = 0; i < resultsManga.length-1; i++)
{
if (!document.getElementById('manga' + i))
{
let url = resultsManga[i].href;
let urlShort = url.slice(23);
let urlShortDecoded = decodeURIComponent(urlShort);
let id = url.split('=')[1];
let selector = 'a[href="' + urlShortDecoded + '"]';
if (!mangaIds.includes(id))
{
addTranslation('manga', i, url, id, selector);
}
mangaIds.push(id);
}
}
}
// People
else if (LOCATION_HREF.includes('https://myanimelist.net/people'))
{
// Anime Results
let resultsAnime = document.querySelectorAll('[href*="/anime/"]:not(.Lightbox_AddEdit):not([href*="anime/season"])');
let animeIds = [];
for (let i = 0; i < resultsAnime.length; i++)
{
if (!document.getElementById('anime' + i))
{
let url = resultsAnime[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"]:not(.picSurround > a)';
if (!animeIds.includes(id))
{
addTranslation('anime', i, url, id, selector);
}
animeIds.push(id);
}
}
// Manga Results
let resultsManga = document.querySelectorAll('[href*="/manga/"]:not(.Lightbox_AddEdit)');
let mangaIds = [];
for (let i = 0; i < resultsManga.length; i+=2)
{
if (!document.getElementById('manga' + i))
{
let url = resultsManga[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"]:not(.picSurround > a)';
if (!mangaIds.includes(id))
{
addTranslation('manga', i, url, id, selector);
}
mangaIds.push(id);
}
}
}
}
// Get English title (storedAnime and getEnglishTitle) and add to page
function addTranslation(type, count, url, id, selector, parent=false, tile=false, producer=false)
{
let styleId = ""
let styleIdEnd = ""
if (tile)
{
styleId = '<h3 class="h3_anime_subtitle" id="' + type + count + '">';
styleIdEnd = '</h3>';
}
else
{
styleId = '<div style="font-weight:bold" id="' + type + count + '">';
styleIdEnd = '</div>';
}
if (type === 'anime')
{
if (producer)
{
document.getElementsByClassName('category')[count].style.visibility = 'hidden'
}
if (checkAnime(id))
{
document.querySelectorAll(selector).forEach(function(element)
{
if (parent)
{
element = element.parentElement;
}
element.insertAdjacentHTML('beforebegin', styleId + storedAnime[id][0] + styleIdEnd);
});
}
else
{
getEnglishTitle(type, url, id, selector, parent, styleId, styleIdEnd);
}
}
else if (type === 'manga')
{
if (checkManga(id))
{
document.querySelectorAll(selector).forEach(function(element)
{
if (parent)
{
element = element.parentElement;
}
element.insertAdjacentHTML('beforebegin', styleId + storedManga[id][0] + styleIdEnd);
});
}
else
{
getEnglishTitle(type, url, id, selector, parent, styleId, styleIdEnd);
}
}
}
// Request English title from MAL and send to be stored (storeAnime)
function getEnglishTitle(type, url, id, selector, parent, styleId, styleIdEnd)
{
// Create new request
let xhr = new XMLHttpRequest();
xhr.responseType = 'document';
// Set the callback
xhr.onload = function()
{
if (xhr.readyState === xhr.DONE && xhr.status === 200 && xhr.responseXML !== null)
{
let englishTitleElement = xhr.responseXML.querySelector('.title-english');
let englishTitle;
if (englishTitleElement)
{
englishTitle = englishTitleElement.innerText;
}
else
{
englishTitle = '';
}
if (type === 'anime')
{
storeAnime(id, englishTitle);
}
else if (type === 'manga')
{
storeManga(id, englishTitle);
}
document.querySelectorAll(selector).forEach(function(element)
{
if (parent)
{
element = element.parentElement;
}
element.insertAdjacentHTML('beforebegin', styleId + englishTitle + styleIdEnd);
});
}
};
// Send the request
xhr.open('GET', url);
xhr.send();
}
// Store English titles for anime in cache
function storeAnime(id, engTitle)
{
storedAnime[id] = [engTitle, Date.now()];
GM_setValue('anime', storedAnime);
}
// Store English titles for manga in cache
function storeManga(id, engTitle)
{
storedManga[id] = [engTitle, Date.now()];
GM_setValue('manga', storedManga);
}
// Check if English title for anime is cached, and recheck if empty + last check was >3 weeks
function checkAnime(id)
{
if (storedAnime.hasOwnProperty(id))
{
if (storedAnime[id][0] === '')
{
let dateNow = Date.now();
let dateOld = storedAnime[id][1];
if (dateNow - dateOld > 2628000000)
{
console.log('Updated anime ' + id);
return false;
}
}
return true;
}
console.log('New anime ' + id);
return false;
}
// Check if English title for manga is cached, and recheck if empty + last check was >3 weeks
function checkManga(id)
{
if (storedManga.hasOwnProperty(id))
{
if (storedManga[id][0] === '')
{
let dateNow = Date.now();
let dateOld = storedManga[id][1];
if (dateNow - dateOld > 2628000000)
{
console.log('Updated manga ' + id);
return false;
}
}
return true;
}
console.log('New manga ' + id);
return false;
}
// Get cached English titles if they exist, else create empty dictionary
var storedAnime = GM_getValue('anime');
var storedManga = GM_getValue('manga');
if (!storedAnime)
{
GM_setValue('anime',{});
storedAnime = {};
}
if (!storedManga)
{
GM_setValue('manga',{});
storedManga = {};
}
// Launch actual script
translate();