-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskipper.js
170 lines (160 loc) · 5.5 KB
/
skipper.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var Skipper = {
previousSongStack: [],
currentStationId: '',
currentSongQueue: [],
currentSong: {},
triggered: false,
artistSelector: 'hgroup>h2.artist>a',
titleSelector: 'hgroup>h1.title>a',
artSelector: '.playerArt>a>img',
listTemplate: '<div id="songListWrapper"><ul id="songList"></ul></div>',
keys: {
nowPlaying: 'p-nowPlaying',
currentSkips: 'p-station-'+this.currentStationId,
overallSkips: 'p-station-skips',
songQueue: 'p-tracks'
},
initialize: function() {
this.initCSS();
this.initEvents();
$('.b-playStation, #playerPlay').click(function(){
setTimeout(this.update.bind(this), 1000);
this.durationChecker();
}.bind(this));
},
durationChecker: function() {
setInterval(function(){
try {
var duration = $('.songDuration').html().split('/');
var current_minute = parseInt(duration[0].replace(/\s+/g,'').split(':')[0]);
var current_second = parseInt(duration[0].replace(/\s+/g,'').split(':')[1]);
var total_minute = parseInt(duration[1].replace(/\s+/g,'').split(':')[0]);
var total_second = parseInt(duration[1].replace(/\s+/g,'').split(':')[1]);
var minutesToSeconds = function(m,s) {return ((m*60)+s)};
var current_position = minutesToSeconds(current_minute, current_second);
var total_duration = minutesToSeconds(total_minute, total_second);
var isAlmostEnd = function() { return (total_duration-1)-current_position<5}
if (isAlmostEnd()) {
setTimeout(this.skip.bind(this), 4000);
}
} catch(err) {
}
}.bind(this), 3000);
},
readValue: function(key) {
return $.parseJSON(localStorage.getItem(this.keys[key]));
},
setValue: function(key, value) {
localStorage.setItem(this.keys[key], JSON.stringify(value));
},
update: function(callback) {
if (this.readValue('songQueue')) {
this.currentStationId = this.readValue('nowPlaying').data.id;
this.currentSongQueue = this.readValue('songQueue')[this.currentStationId];
this.keys.currentSkips = 'p-station-'+this.currentStationId;
var currentSkips = this.readValue('currentSkips');
var overallSkips = this.readValue('overallSkips');
overallSkips.count = 0;
currentSkips.count = 0;
this.setValue('currentSkips', currentSkips);
this.setValue('overallSkips', overallSkips);
callback && callback();
}
},
skip: function() {
if (!this.triggered) {
this.previousSongStack.push($.extend(true, {}, this.currentSong));
} else {
this.triggered = false;
}
if (!$.isEmptyObject(this.currentSong)) {
var listEl = $('<li class="songItem">').append(
'<img src="'+this.currentSong.albumArtUrl+'"/>'
).append(
'<span class="artist">'+this.currentSong.artist+'<span>'
).append(
'<span class="title">'+this.currentSong.title+'<span>'
);
$('#songList').prepend(listEl);
}
this.currentSong.id = this.currentSongQueue[0];
setTimeout(function(){
this.update(function() {
if ($(this.artistSelector).html()) this.currentSong.artist = $(this.artistSelector).html();
if ($(this.titleSelector).html()) this.currentSong.title = $(this.titleSelector).html();
if ($(this.artSelector).attr('src')) this.currentSong.albumArtUrl = $(this.artSelector).attr('src');
}.bind(this));
}.bind(this), 3000);
},
previousSong: function() {
if (!$.isEmptyObject(this.currentSong)) {
this.currentSongQueue.unshift(this.currentSong.id);
var target_song = this.previousSongStack.pop();
this.currentSongQueue.unshift(target_song.id);
var queue_obj = {};
queue_obj[this.currentStationId] = this.currentSongQueue;
this.setValue('songQueue', queue_obj);
this.update();
this.triggered = true;
$('#playerNext').click();
} else {
return false;
}
},
initCSS: function() {
var rewindEl = $('<input>').attr({
'id':'playerLast',
'class':'playerLast',
'type':'button'
}).css({
'width':'32px',
'height':'50px',
'top':'8px',
'left':'2px',
'background-position':'-247px 0',
'-moz-transform': 'scaleX\(-1\)',
'-webkit-transform': 'scaleX\(-1\)',
'-o-transform': 'scaleX\(-1\)',
'transform': 'scaleX\(-1\)',
'filter': 'FlipH',
'-ms-filter': '\“FlipH\”',
});
$('#playerPlay').css('left','38px');
$('#playerNext').css('left','98px');
$('.playerBtns').prepend(rewindEl);
$('#ihr-header').after(this.listTemplate);
$('#songListWrapper').css({
'height':'140px',
'background-color':'whitesmoke'
});
$('#songList').css({
'margin-left': '190px',
'height': '98%',
'width': '84%',
'border': '1px dashed rgb(153, 53, 53)'
});
$('#wrap').css('padding-top', '270px');
$('.navLogo').css('left','-100px');
$('.playerBtns').css({
'background-image':'url\('+$('#inject').attr('bgImage')+'\)',
'width':'134px',
'left':'70px'
});
$('head').append(
'<style type="text/css">'+
'li.songItem {display:inline-block; text-align:center; width:80px; vertical-align:top; margin-top:10px;}'+
'li.songItem>img {display:block;margin:0 auto;}'+
'li.songItem>span {display: block;}'+
'</style>'
);
},
initEvents: function() {
$('#playerNext').click(function(e){
this.skip();
}.bind(this));
$('#playerLast').click(function(e){
this.previousSong();
}.bind(this));
}
}
Skipper.initialize();