forked from chrischen/Like.fm-for-Chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
injectRdio.js
100 lines (87 loc) · 3.52 KB
/
injectRdio.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
function fireTrackEvent(data,posUpdate) {
// Context of the page
var track = window.player_model.playingTrack;
if (posUpdate) {
if (__lfm_hold == true)
return false;
track.__lfm_statusUpdate = true;
track.__lfm_pos = data;
} else {
track.__lfm_status = data;
setTimeout(function(){ __lfm_hold = false},1000);
}
var hiddenDiv = document.getElementById('LikeFMComm');
hiddenDiv.textContent = JSON.stringify(track);
hiddenDiv.dispatchEvent(__lfm_trackEvent);
}
function LikeFMInject () {
// Comm link with content script
__lfm_trackEvent = document.createEvent('Event');
__lfm_trackEvent.initEvent('myTrackEvent', true, true);
function bind() {
window.positionChanged = function (a){$("#playerTrackSlider").progressSlider({value:((a>0)?a/player_model.duration:0)});
updateTrackSliderLabelValue(a);
var b=$.Event("positionChanged");
b.position=a;
$(document).trigger(b);
fireTrackEvent(a,true);
}
window.playStateChanged = function (c){if(playingSomewhereElseDialog!==null){playingSomewhereElseDialog.dialog("close");
playingSomewhereElseDialog=null
}var a=["paused","playing","stopped","buffering","offline"];
log("playState: "+a[c]);
$.each(a,function(e,d){$("body").removeClass(d)
});
$("body").addClass(a[c]);
window.player_model.playState=c;
var b=$.Event("playStateChanged");
b.playState=c;
$(document).trigger(b);
__lfm_hold = true;
fireTrackEvent(c,false);
}
}
bind();
}
// Below is in the context of content script
// Injected script
if (!document.getElementById("LikeFMInject")) {
var script = document.createElement('script');
script.setAttribute('id','LikeFMInject');
script.appendChild(document.createTextNode('var LikeFM = {}; var __lfm_trackEvent; var __lfm_hold;' + fireTrackEvent + '('+ LikeFMInject +')();'));
(document.body || document.head || document.documentElement).appendChild(script);
}
// Comm link medium div
if (!document.getElementById("LikeFMComm")) {
var comm = document.createElement('div');
comm.style.display = "none";
comm.setAttribute('id','LikeFMComm');
(document.body || document.documentElement).appendChild(comm);
// Comm link with injected script
document.getElementById('LikeFMComm').addEventListener('myTrackEvent', function() {
var track = {};
var song = JSON.parse(document.getElementById('LikeFMComm').textContent);
track.lsource = 'Rdio';
track.source = 'P';
if (song.__lfm_status == 1 && !song.__lfm_statusUpdate) {
track.title = song.name;
track.artist = song.artist;
track.album = song.album
track.type = 'touch';
// Send message to background process
chrome.extension.sendRequest({messageType:"track",data:track});
LikeFM.currentTrack = track;
} else {
var percent = song.__lfm_pos/song.duration;
if (song.__lfm_status == 1 && percent > 0.8 && LikeFM.currentTrack) {
track.title = song.name;
track.artist = song.artist;
track.album = song.album;
track.type = 'finish';
// Send message to background process
chrome.extension.sendRequest({messageType:"track",data:track});
LikeFM.currentTrack = null;
}
}
});
}