forked from fireattack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvgmdb_helper.user.js
More file actions
82 lines (72 loc) · 2.64 KB
/
vgmdb_helper.user.js
File metadata and controls
82 lines (72 loc) · 2.64 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
// ==UserScript==
// @name VGMdb Liner Note Helper
// @namespace http://tampermonkey.net/
// @version 2.4
// @description Within album notes in VGMdb, link to artists, and reference tracks by name
// @author fireattack
// @original-author btown
// @match https://vgmdb.net/album/*
// @grant none
// @license MIT
// @copyright 2021, btown (https://openuserjs.org/users/btown)
// ==/UserScript==
// modified from https://openuserjs.org/scripts/btown/VGMdb_Liner_Note_Helper as the author no longer updates it
const DEBUGGING = false;
function restoreConsole() {
var i = document.createElement('iframe');
i.style.display = 'none';
document.body.appendChild(i);
window.console = i.contentWindow.console;
}
function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
if (DEBUGGING) restoreConsole();
var notes = document.getElementById('notes');
var html = notes.innerHTML;
var toBeReplaced = [];
var links = document.querySelectorAll('a[href^="/artist/"]');
[...links].map((link) => {
try {
var linkText = link.querySelector('[lang="en"]') || link;
var name = linkText.textContent;
var re = new RegExp(escapeRegExp(name), 'g');
var nameJa = (link.querySelector('[lang="ja"]') || link).textContent;
var repl = `<a href="${link.href}" target="_blank">${nameJa}</a>`;
toBeReplaced.push({
re: re,
repl: repl,
});
}
catch (err) { }
});
// replace to intermediate placeholders firstly, so links won't be broken
for (idx in toBeReplaced) {
DEBUGGING && console.log(re, repl);
var {re, repl} = toBeReplaced[idx];
var placeHolder = `___${idx}___`;
html = html.replace(re, placeHolder);
}
for (idx in toBeReplaced) {
var {re, repl} = toBeReplaced[idx];
var placeHolder = `___${idx}___`;
html = html.replace(new RegExp(placeHolder, 'g'), repl);
}
// Feature: add track name to note.
var toBeReplaced = [];
let languages = {};
[...document.querySelectorAll('#tlnav > li > a')].map(a=>languages[a.textContent]=a.rel);
let tracklist_id = languages['Japanese'] || Object.values(languages)[0];
[...document.querySelectorAll(`#${tracklist_id} > table tr.rolebit`)].map((track) => {
try {
var cols = track.children;
var num = cols[0].textContent.trim();
var title = cols[1].textContent.trim();
DEBUGGING && console.log(num, title);
var re = new RegExp(`(\\w-)?\\b(${num}|${parseInt(num, 10)})\\b`, 'g');
var repl = `$& (${title})`;
html = html.replace(re, repl);
}
catch (err) { }
});
notes.innerHTML = html;