-
Notifications
You must be signed in to change notification settings - Fork 3
/
injectLive365.js
163 lines (143 loc) · 4.82 KB
/
injectLive365.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function fireTrackEvent(data) {
// Context of the page
var hiddenDiv = document.getElementById('LikeFMComm');
var myTrack = data ;
hiddenDiv.textContent = JSON.stringify(myTrack);
hiddenDiv.dispatchEvent(trackEvent);
}
function LikeFMInject () {
// Comm link with content script
trackEvent = document.createEvent('Event');
trackEvent.initEvent('myTrackEvent', true, true);
oldPlayOrStop = PlayOrStop ;
oldUpdatePlaylist = UpdatePlaylist ;
var isPlaying = false ;
var tr = {} ;
function newPlayOrStop(action, checkNagRma)
{
var myAction = action ;
if (action == 'resume')
myAction= (gPlayOrStopStatus == 'play') ? 'noop' : 'play';
else if (action == 'toggle')
myAction = (gPlayOrStopStatus == 'play') ? 'stop' : 'play';
isPlaying = (myAction == 'play') ;
tr.isPlaying = isPlaying ;
// console.log("PLAYORSTOP : isPlaying : " + isPlaying ) ;
// Call the orginal function/method
return oldPlayOrStop(action, checkNagRma) ;
}
function newUpdatePlaylist(results)
{
function remove( str, str2)
{
return str.replace( str2, "" );
}
function cleanResult( str )
{
var result = str ;
result = remove(result, "</span>");
result = remove(result, "<span class='title'>");
result = remove(result, "<span class='artist'>");
result = remove(result, "<span class='album'>");
return result ;
}
// Send the previous track as finished
if( tr.title != null && isPlaying && tr.percent != 0.9)
{
tr.position = 2;
tr.percent = 0.9 ;
// console.log( "GETINFO (2) : " + tr.artist + " - " + tr.title + " - isPlaying : " + tr.isPlaying + " - percent : " + tr.percent ) ;
fireTrackEvent(tr) ;
//tr = {} ;
}
if( results != null )
{
var title = cleanResult(results.title);
var artist = cleanResult(results.artist);
if( title == tr.title && artist == tr.artist )
{
tr.position = 2;
tr.percent = 0.9 ;
}
else
{
tr.position = 0;
tr.percent = 0 ;
}
tr.title = title ;
tr.artist = artist ;
tr.album = cleanResult(results.album);
tr.isPlaying = isPlaying ;
}
else
{
tr = {} ;
}
// console.log( "GETINFO : " + tr.artist + " - " + tr.title + " - isPlaying : " + tr.isPlaying + " - percent : " + tr.percent ) ;
fireTrackEvent(tr) ;
return oldUpdatePlaylist(results) ;
}
function bind() {
try {
PlayOrStop = newPlayOrStop ;
UpdatePlaylist = newUpdatePlaylist ;
} 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 myTrack = JSON.parse(document.getElementById('LikeFMComm').textContent);
var percent = myTrack.percent ; // dzPlayer.position/dzPlayer.duration;
track.lsource = 'Live365.com';
track.source = 'P';
if (myTrack.position == 0
&& (
(LikeFM.currentTrack
&& ( myTrack.title != LikeFM.currentTrack.title ||
myTrack.artist != LikeFM.currentTrack.artist)
) || !LikeFM.currentTrack
)
) {
track.title = myTrack.title;
track.artist = myTrack.artist;
track.album = myTrack.album;
track.type = 'touch';
// Send message to background process
// console.log( "TOUCH : " + track.artist + " - " + track.title ) ;
chrome.extension.sendRequest({messageType:"track",data:track});
LikeFM.currentTrack = track;
// Update the position
var hiddenDiv = document.getElementById('LikeFMComm');
myTrack.position = 1 ;
hiddenDiv.textContent = JSON.stringify(myTrack);
}
else if (myTrack.isPlaying && percent > 0.8 && LikeFM.currentTrack ) {
track.title = myTrack.title;
track.artist = myTrack.artist;
track.album = myTrack.album;
track.type = 'finish';
// Send message to background process
// console.log( "FINISH : " + track.artist + " - " + track.title ) ;
chrome.extension.sendRequest({messageType:"track",data:track});
LikeFM.currentTrack = null;
}
});
}