-
Notifications
You must be signed in to change notification settings - Fork 3
/
injectGrooveshark.js
87 lines (76 loc) · 3.11 KB
/
injectGrooveshark.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
function fireTrackEvent(data) {
// Context of the page
var hiddenDiv = document.getElementById('LikeFMComm');
hiddenDiv.textContent = JSON.stringify(data);
hiddenDiv.dispatchEvent(trackEvent);
if (data.status == 'playing' && !LikeFM.statusInterval) {
// Song has started playing - start polling
LikeFM.statusInterval = setInterval(function() {
var status = Grooveshark.getCurrentSongStatus();
status.statusUpdate = true;
fireTrackEvent(status);
},500);
} else if (data.status == 'completed') {
clearInterval(LikeFM.statusInterval);
LikeFM.statusInterval = null;
}
}
function LikeFMInject () {
// Comm link with content script
trackEvent = document.createEvent('Event');
trackEvent.initEvent('myTrackEvent', true, true);
function bind() {
try {
window.Grooveshark.setSongStatusCallback("fireTrackEvent");
} catch (e) {
setTimeout(bind,1200);
}
}
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 trackEvent;' + fireTrackEvent + '('+ LikeFMInject +')();'));
(document.body || document.head || document.documentElement).appendChild(script);
}
// Comm link medium div
if (!document.getElementById("LikeFMComm")) {
var comm = document.createElement('div');
comm.setAttribute('id','LikeFMComm');
comm.style.display = "none";
(document.body || document.documentElement).appendChild(comm);
// Comm link with injected script
document.getElementById('LikeFMComm').addEventListener('myTrackEvent', function() {
var track = {};
var data = JSON.parse(document.getElementById('LikeFMComm').textContent);
var percent = data.song.position/data.song.calculatedDuration;
track.lsource = 'Grooveshark';
track.source = 'P';
if (data.song.position == 0
&& (
(LikeFM.currentTrack
&& (data.song.songName != LikeFM.currentTrack.title || data.song.artistName != LikeFM.currentTrack.artist)
) || !LikeFM.currentTrack
)
) {
track.title = data.song.songName;
track.artist = data.song.artistName;
track.album = data.song.albumName;
track.type = 'touch';
// Send message to background process
chrome.extension.sendRequest({messageType:"track",data:track});
LikeFM.currentTrack = track;
} else if (data.status == 'playing' && percent > 0.8 && LikeFM.currentTrack) {
track.title = data.song.songName;
track.artist = data.song.artistName;
track.album = data.song.albumName;
track.type = 'finish';
// Send message to background process
chrome.extension.sendRequest({messageType:"track",data:track});
LikeFM.currentTrack = null;
}
});
}