forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
megalyrics.js
88 lines (75 loc) · 2.43 KB
/
megalyrics.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
/*
* Chrome-Last.fm-Scrobbler Megalyrics.ru connector by Dunaeva Natalia
* https://github.com/plushsteel
* http://www.lastfm.ru/user/SlaveOfBeauty
*
* (based on Pandora.com connector by Jordan Perr)
*
*/
/********* Configuration: ***********/
// changes to the DOM in this container will trigger an update.
LFM_WATCHED_CONTAINER = "#ui_title_bar";
// function that returns title of current song
function LFM_TRACK_TITLE() {
link = $("#ui_song_title").html();
res = link.replace(/<b>.*<\/b> - /g,'');
return res;
}
// function that returns artist of current song
function LFM_TRACK_ARTIST() {
res = $("#ui_song_title b").text();
return res;
}
// function that returns duration of current song in seconds
// called at begining of song
function LFM_TRACK_DURATION() {
durationArr = $("#ui_time").text().split('/')[1].split(":");
return parseInt(durationArr[0])*60 + parseInt(durationArr[1]);
}
/********* Connector: ***********/
var LFM_lastTrack = "";
var LFM_isWaiting = 0;
function LFM_updateNowPlaying(){
// Acquire data from page
title = LFM_TRACK_TITLE();
artist = LFM_TRACK_ARTIST();
duration = LFM_TRACK_DURATION();
//console.log(title,artist,duration);
newTrack = title + " " + artist;
// Update scrobbler if necessary
if (newTrack != "" && newTrack != LFM_lastTrack){
if (duration == 0) {
// Nasty workaround for delayed duration visiblity with skipped tracks.
setTimeout(LFM_updateNowPlaying, 5000);
return 0;
}
//console.log("submitting a now playing request. artist: "+artist+", title: "+title+", duration: "+duration);
LFM_lastTrack = newTrack;
chrome.extension.sendRequest({type: 'validate', artist: artist, track: title}, function(response) {
if (response != false) {
chrome.extension.sendRequest({type: 'nowPlaying', artist: artist, track: title, duration: duration});
} else { // on failure send nowPlaying 'unknown song'
chrome.extension.sendRequest({type: 'nowPlaying', duration: duration});
}
});
}
LFM_isWaiting = 0;
}
// Run at startup
$(function(){
//console.log("Megalyrics module starting up");
$(LFM_WATCHED_CONTAINER).live('DOMSubtreeModified', function(e) {
//console.log("Live watcher called");
if ($(LFM_WATCHED_CONTAINER).length > 0) {
if(LFM_isWaiting == 0){
LFM_isWaiting = 1;
setTimeout(LFM_updateNowPlaying, 10000);
}
return;
}
});
$(window).unload(function() {
chrome.extension.sendRequest({type: 'reset'});
return true;
});
});