-
Notifications
You must be signed in to change notification settings - Fork 2
/
export-google-play.js
50 lines (38 loc) · 1.5 KB
/
export-google-play.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
const _logStyle = 'background: #222; color: #bada55';
function log(message) {
console.log('%c ' + message, _logStyle);
}
log('Please scroll through the playlist so that each album is visible once.\n' +
'Then double-click the page to export a spreadsheet.');
var albums = ['Artist,Title,Album'];
var fileName = 'export-google-play.csv';
var playlistTitleNode = document.querySelector('.info > .title > .title-text');
if (playlistTitleNode) {
fileName = playlistTitleNode.innerText;
}
var addVisibleAlbums = function(){
[].forEach.call(document.querySelectorAll('.song-row'), function(e){
var albumNodes = [e.querySelector('td[data-col=\'artist\']'),
e.querySelector('td[data-col=\'title\'] .column-content'),
e.querySelector('td[data-col=\'album\']')];
var albumString = albumNodes.map(function(s){
return s.innerText.trim().replace(/,/g,'');
}).join(',');
if(albums.indexOf(albumString) === -1){
albums.push(albumString); console.log('Added: ' + albumString)
}
});
}
var createCsv = function(){
var csv = 'data:text/csv;charset=utf-8,';
albums.forEach(function(row){ csv += row + '\n'; });
var uri = encodeURI(csv);
var link = document.createElement('a');
link.setAttribute('href', uri);
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
log('Download beginning!');
}
document.body.addEventListener('DOMNodeInserted', addVisibleAlbums, false);
document.body.addEventListener('dblclick', createCsv, false);