-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentscript.js
101 lines (94 loc) · 3 KB
/
contentscript.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
/**
* Global object containing the functions used to download
*
* @type {object}
*/
window.PLAYLIST_ = [];
var listParser = {
/**
* Listen for audio change
*
*/
onMessage: function() {
var _this = this;
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if ( message.from === 'background' ) {
_this.listTracker( message.audio_url );
}
if ( message.from === 'popup' ) {
sendResponse(PLAYLIST_);
}
if ( message.from === 'popup' && message.command === 'skip song' ) {
document.querySelector( '.skipButton' ).click();
}
});
},
/**
* Encode STRING
*
*/
utf8_to_b64: function( str ) {
return window.btoa(unescape(encodeURIComponent( str )));
},
/**
* Decode STRING
*
*/
b64_to_utf8: function( str ) {
return decodeURIComponent(escape(window.atob( str )));
},
/**
* setInterval for new song
*
*/
listTracker: function( audio_url ) {
var interval = setInterval(function() {
if( document.getElementsByClassName('trackData').length ) {
var songTitle = document.getElementsByClassName('songTitle'),
artistName = document.getElementsByClassName('artistSummary'),
albumTitle = document.getElementsByClassName('albumTitle');
if( songTitle.length && artistName.length && albumTitle.length ) {
songTitle = songTitle[0].innerHTML;
artistName = artistName[0].innerHTML;
albumTitle = albumTitle[0].innerHTML;
if( songTitle.length && artistName.length && albumTitle.length ) {
albumArt = $('.playerBarArt[src]').attr('src');
albumArt = albumArt.indexOf("no_album_art") === -1 ? albumArt : 'http://www.pandora.com/img/no_album_art.png';
listParser.addSong({
id: listParser.utf8_to_b64( songTitle + artistName ),
song: songTitle,
album: albumTitle,
artist: artistName,
art: albumArt,
url: audio_url
});
clearInterval(interval);
}
}
}
}, 2000);
},
/**
* Add new song to PLAYLIST_
*
*/
addSong: function( songObj ) {
if( !this.finder( songObj.id ) ) {
PLAYLIST_.push(songObj);
}
},
/**
* Finder used to lookup existing songs
*
*/
finder: function( id ) {
found = false;
for(var i = 0; i < PLAYLIST_.length; i++) {
if( PLAYLIST_[i].id === id ) {
found = true;
}
}
return found;
}
};
listParser.onMessage();