-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
89 lines (70 loc) · 2.85 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NML to Tracklist</title>
</head>
<body>
<div>
<input type="file" id="input" onchange="readFile(this.files)">
<button id="copy">Copy to clipboard</button>
</div>
<div>
<pre id="result"></pre>
</div>
<script>
function copyTrackListToClipboard() {
var textArea = document.createElement('textarea');
var textToCopy = document.querySelector('#result');
textArea.value = textToCopy.innerText;
document.body.appendChild(textArea);
textArea.select()
document.execCommand('copy');
document.body.removeChild(textArea);
}
document.querySelector('#copy').addEventListener('click', copyTrackListToClipboard);
function readFile(files) {
var fileReader = new FileReader();
fileReader.onload = function (event) {
parseNML(fileReader.result);
};
fileReader.readAsText(files[0]);
}
function parseNML(string) {
var parser = new DOMParser();
var tracklist = parser.parseFromString(string, 'application/xml');
var nsResolver = tracklist.createNSResolver(tracklist.documentElement);
var collectionEntries = tracklist.evaluate('/NML/COLLECTION/ENTRY', tracklist, nsResolver, XPathResult
.ANY_TYPE, null);
var playlistEntries = tracklist.evaluate(
'/NML/PLAYLISTS//PLAYLIST/ENTRY/PRIMARYKEY', tracklist, nsResolver,
XPathResult.ANY_TYPE, null);
var tracks = {};
while (entry = collectionEntries.iterateNext()) {
tracks[entry.children[0].getAttribute('FILE')] = {
'artist': entry.getAttribute('ARTIST'),
'title': entry.getAttribute('TITLE'),
};
}
var playlist = [];
while (entry = playlistEntries.iterateNext()) {
playlist.push(entry.getAttribute('KEY').split(':').pop());
}
var orderedTracklist = [];
playlist.forEach((filename) => {
orderedTracklist.push(tracks[filename]);
});
var formatted = orderedTracklist.map((track, index) => {
index += 1;
index = index < 10 ? '0' + index : index;
return `${index} - ${track.artist} - ${track.title}`;
});
var formattedText = formatted.reduce((previousValue, currentValue, index) => {
return `${previousValue}\n${currentValue}`;
});
document.getElementById('result').innerText = formattedText;
}
</script>
</body>
</html>