forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iheart.js
99 lines (96 loc) · 3.22 KB
/
iheart.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
/*
Chrome-Last.fm-Scrobbler iHeartRadio Connector
*/
var track, artist, duration
var scrobbleTimeout = null;
var durationTimeout = null;
$(function () {
//setup unload handler
$(window).unload(function () {
//reset the background scrobbler song data
chrome.extension.sendRequest({
type: 'reset'
});
return true;
});
$("#playerPlay,.b-playStation").click(function () {
var c = 0;
//wait for changes in artist name
$("#player h2.artist").bind("DOMSubtreeModified", function (e) {
//avoids being executed twice
if (c == 0) {
c++;
return false;
}
//cancel any previous timeout
if (scrobbleTimeout != null) clearTimeout(scrobbleTimeout);
if (durationTimeout != null) clearTimeout(durationTimeout);
//delay notification slightly
setTimeout(function () {
artist = $("#player h2.artist a").attr("title");
track = $("#player h1.title a").attr("title");
duration = $(".songDuration").text();
}, 1000);
displayMsg()
//song duration on page updates as the music gets loaded by the browser
waitForDuration(updateNowPlaying);
c = 0;
})
})
});
function updateNowPlaying() {
if (artist == "" || track == "" || artist == undefined || track == undefined) {
console.log("Artist/Track was empty or radio station is in a commercial break.");
return;
}
//validate track info
chrome.extension.sendRequest({
type: 'validate',
artist: artist,
track: track
}, function (response) {
if (response != false) {
chrome.extension.sendRequest({
type: 'nowPlaying',
artist: artist,
track: track,
duration: duration
});
console.log({
type: 'nowPlaying',
artist: artist,
track: track,
duration: duration
});
displayMsg("Scrobbling");
} else {
displayMsg("Unable to Scrobble");
console.log("Last.fm was unable to validate the Artist/Track combination.");
}
});
}
function waitForDuration(callback) {
function getDuration() {
var current_track_duration = $(".songDuration").text();
//need a new solution for duration on live stations
if (current_track_duration == "0:00" || current_track_duration == "") {
current_track_duration = "0:00 / 3:00";
}
var total_length = current_track_duration.split(" / ")[1];
return parseInt(total_length.split(":")[0] * 60) + parseInt(total_length.split(":")[1]);
}
var check_1 = getDuration();
durationTimeout = setTimeout(function () {
var check_2 = getDuration();
if (check_1 == check_2 && check_1 != false && check_1 != 0) {
//not loading anymore
duration = check_1;
callback();
} else {
waitForDuration(callback);
}
}, 1000);
}
function displayMsg(msg) {
$("#chrome-scrobbler-status").remove();
}