-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast_assistant.js
147 lines (121 loc) · 4.57 KB
/
cast_assistant.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
$(document).ready(function() {
"use strict";
var overlayHTML = '<div class="cast-assistant-overlay hidden"><div class="cast-assistant-overlay__info"><div class="cast-assistant-overlay__artist" /><div class="cast-assistant-overlay__title" /><div class="cast-assistant-overlay__progress"><div class="cast-assistant-overlay__progress-fill" /><span class="cast-assistant-overlay__progress-info cast-assistant-overlay__progress-start"/><span class="cast-assistant-overlay__progress-info cast-assistant-overlay__progress-end"/></div></div></div>';
$('body').append(overlayHTML);
var overlay = $('.cast-assistant-overlay');
var pageTitleElement = document.getElementsByTagName('title')[0];
var title = '';
var artist = '';
// where are we?
var isSpotify = (window.location.href.indexOf('https://play.spotify.com/') === 0),
isSoundCloud = (window.location.href.indexOf('https://soundcloud.com/') === 0),
isPandora = (window.location.href.indexOf('http://www.pandora.com/') === 0);
overlay.addClass(isSpotify ? 'spotify' : '');
overlay.addClass(isSoundCloud ? 'soundcloud' : '');
overlay.addClass(isPandora ? 'pandora' : '');
pageTitleElement.addEventListener("DOMSubtreeModified", function() {
if (document.title.indexOf('▶') === 0) {
overlay.removeClass('not-playing');
if (isSpotify) {
// Spotify
title = $('#app-player').contents().find('#track-name a').text();
artist = $('#app-player').contents().find('#track-artist a').text();
} else if (isSoundCloud) {
// SoundCloud
title = $('.playbackTitle').text();
artist = document.title.substr(1 + title.length + 4);
}
} else {
title = '';
artist = '';
overlay.addClass('not-playing');
}
updateOverlay(artist, title);
});
if (isPandora) {
// Pandora
var target = document.querySelector('.pauseButton');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function() {
if (target.style.display === 'block') {
title = $('.playerBarSong').text();
artist = $('.playerBarArtist').text();
overlay.removeClass('not-playing');
} else {
title = '';
artist = '';
overlay.addClass('not-playing');
}
updateOverlay(artist, title);
});
});
observer.observe(target, { attributes: true });
var trackTarget = document.querySelector('.playerBarSong');
var trackObserver = new MutationObserver(function(mutations) {
mutations.forEach(function() {
title = $('.playerBarSong').text();
artist = $('.playerBarArtist').text();
updateOverlay(artist, title);
});
});
trackObserver.observe(trackTarget, { childList: true, subtree: true });
}
function toggleOverlay(isActive) {
if (isActive) {
updateOverlay(artist, title);
overlay.removeClass('hidden');
} else {
overlay.addClass('hidden');
}
}
var timer, endTimeSecs;
function updateOverlay(artist, title) {
$('.cast-assistant-overlay__artist').text(artist);
$('.cast-assistant-overlay__title').text(title);
if ((isSpotify || isPandora) && (artist + title).length > 0) {
var endTime;
if (isSpotify) {
endTime = $('#app-player').contents().find('#track-length').text();
} else if (isPandora) {
endTime = secToHms(hmsToSecs($('.remainingTime').text()) + hmsToSecs($('.elapsedTime').text()));
}
endTimeSecs = hmsToSecs(endTime);
$('.cast-assistant-overlay__progress-end').text(endTime);
clearTimeout(timer);
timer = startTimer();
}
}
function startTimer() {
var currentTime;
if (isSpotify) {
currentTime = $('#app-player').contents().find('#track-current').text();
} else if (isPandora) {
currentTime = $('.elapsedTime').text();
}
var progress = hmsToSecs(currentTime);
var barPosition = progress/endTimeSecs;
$('.cast-assistant-overlay__progress-start').text(currentTime);
$('.cast-assistant-overlay__progress-fill').width(barPosition * 100 + "%");
setTimeout(startTimer, 100);
}
function hmsToSecs(hms) {
var split = hms.split(':');
return Math.abs(split.pop()) * 1 + Math.abs(split.pop()) * 60 + (split.length ? Math.abs(split.pop()) * 60 * 60 : 0);
}
function secToHms(sec) {
var hours = Math.floor(sec / 3600);
var minutes = Math.floor((sec - (hours * 3600)) / 60);
var seconds = sec - (hours * 3600) - (minutes * 60);
return (hours ? hours : '') + (hours ? ':' : '') + (hours ? zeroPad(minutes) : minutes) + ':' + zeroPad(seconds);
}
function zeroPad(num) {
return (num >= 10 ? num : '0' + num);
}
overlay.click(function() {
toggleOverlay();
chrome.runtime.sendMessage({active: false});
});
chrome.runtime.onMessage.addListener(function(request) {
toggleOverlay(request.active);
});
});