-
Notifications
You must be signed in to change notification settings - Fork 6
/
chrome-snippet.js
47 lines (42 loc) · 1.41 KB
/
chrome-snippet.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
(async () => {
const scroll = (top) => window.scrollTo({ top });
const delay = (ms) => new Promise((r) => setTimeout(r, ms));
async function scrollPlaylist() {
const spinner = document.querySelector('.CatalogBlock__autoListLoader');
let pageHeight = 0;
do {
pageHeight = document.body.clientHeight;
scroll(pageHeight);
await delay(400);
} while (
pageHeight < document.body.clientHeight ||
spinner?.style.display === ''
);
}
function parsePlaylist() {
return [...document.querySelectorAll('.audio_row__performer_title')].map(
(row) => {
const [artist, title] = ['.audio_row__performers', '.audio_row__title']
.map(selector => row.querySelector(selector)?.textContent || '')
.map((v) => v.replace(/[\s\n ]+/g, ' ').trim());
return [artist, title].join(' - ');
},
);
}
function saveToFile(filename, content) {
const data = content.replace(/\n/g, '\r\n');
const blob = new Blob([data], { type: 'text/plain' });
const link = document.createElement('a');
link.download = filename;
link.href = URL.createObjectURL(blob);
link.target = '_blank';
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
// Main
await scrollPlaylist();
const list = parsePlaylist();
saveToFile('vk-playlist.txt', list.join('\n'));
})();