-
Notifications
You must be signed in to change notification settings - Fork 1
/
globalInject.js
257 lines (215 loc) · 9.86 KB
/
globalInject.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var LikeFMInject;
var callback;
var LikeFM = {};
// Check if there is a token exchange message
if (document.getElementById('LikeFMTokenAuthenticated')) {
// Request session
safari.self.tab.dispatchMessage("getSession",true);
} else {
// If EXTENSION is not linked, inject a notice bar into the DOM with link
function supremeCommand(msgEvent) {
if (msgEvent.name === "promptLink") {
// No session exists
function goToLinkPage() {
safari.self.tab.dispatchMessage("link",true);
}
if (!document.getElementById("LikeFMNotice")) {
var notice = document.createElement('div');
notice.setAttribute('id','LikeFMNotice');
notice.style.position = "fixed";
notice.style.top = 0;
notice.style.width = "100%";
notice.style.zIndex = "1001";
notice.style.background = "#dfdfdf";
notice.style.borderTop = "1px solid #f2f2f2";
notice.style.borderBottom = "1px solid #8e8e8e";
notice.style.fontSize = "0.9em";
notice.style.fontFamily = "helvetica";
notice.innerHTML = '<div style="float:left;padding:.4em" ><img style="vertical-align:bottom" src="http://like.fm/img/like_small.png" /> Like.fm for Safari (click to dismiss)</div><div style="float:right">You have not linked an account to this extension. Songs you play will not be sent to Like.fm. <input type="button" id="link-account" value="Link my account" /></div>';
document.body.insertBefore(notice,document.body.firstChild);
document.getElementById("link-account").onclick = goToLinkPage;
notice.onclick = function() {
notice.style.display = "none";
};
}
}
}
safari.self.addEventListener("message", supremeCommand, false);
safari.self.tab.dispatchMessage("checkSession",true);
safari.self.tab.dispatchMessage("clearTrack",true);
}
if (window.location.host.indexOf("youtube.com") > -1) {
function determineAndSendTrack(type) {
if (document.getElementById("watch-description")) {
var nodes = document.getElementById("watch-description").childNodes;
var trackEl;
var track = {};
track.lsource = 'YouTube';
track.source = 'P';
for (var i in nodes) {
if (nodes[i].textContent && i == nodes.length - 2) {
if (nodes[i].childNodes && nodes[i].childNodes.length > 1)
trackEl = nodes[i].childNodes[nodes[i].childNodes.length-2];
}
}
if (trackEl && trackEl.childNodes[1].getAttribute("class") == "master-sprite music-note") {
var trackStr = trackEl.childNodes[3].textContent.split(" - ",2);
track.title = trackStr[1];
track.artist = trackStr[0];
track.type = type;
// Send message to background process
safari.self.tab.dispatchMessage("track",track);
} else if (document.getElementById("eow-category").childNodes[0].textContent.match(/Music|Musik|Música|Musika|Musique|Glazba|Musica|Zene|Muziek|Musikk|Muzyka|Музыка|Hudba|Musiikki|Μουσική|Музика|מוסיקה|संगीत|音乐|音樂|音楽|음악/i)) { // English (both), Dansk, Deutsh, Espangnol (both), Filipino, Francais, Hrvatski, Italiano, Magyar, Nederlands, Norsk, Polski, Portugues (both), Pyccĸий, Slovenský, Suomi, Svenska, Čeština, Ελληνικά, Српски, עברית, हिन्द, 中文 (both), 日本語, 한국어
track.query = document.getElementById("eow-title").textContent;
track.type = type;
// Send message to background process
safari.self.tab.dispatchMessage("track",track);
}
}
}
LikeFMInject = function () {
// Comm link with content script
trackEvent = document.createEvent('Event');
trackEvent.initEvent('myTrackEvent', true, true);
window['onYouTubePlayerReady'] = function(){
document.getElementById("movie_player").addEventListener("onStateChange",'fireTrackEvent');
};
};
callback = function(newState) {
if (newState == 1) {
determineAndSendTrack('touch');
} else if (newState == 0) {
determineAndSendTrack('finish');
}
};
injectHooks(LikeFMInject,callback);
} else if (window.location.host.indexOf("pandora.com") > -1) {
LikeFMInject = function () {
// Comm link with content script
trackEvent = document.createEvent('Event');
trackEvent.initEvent('myTrackEvent', true, true);
Pandora.setEventHandler("SongPlayed", function(songData) {
fireTrackEvent({title:songData.songName,artist:songData.artistName,type:'touch'});
});
Pandora.setEventHandler("SongEnded", function(songData) {
fireTrackEvent({title:songData.songName,artist:songData.artistName,type:'finish'});
});
};
// Below is in the context of content script
callback = function(track) {
track.lsource = 'Pandora';
track.source = 'E';
safari.self.tab.dispatchMessage("track",track);
};
injectHooks(LikeFMInject,callback);
} else if (window.location.host.indexOf("meemix.com") > -1) {
LikeFMInject = function () {
// Comm link with content script
trackEvent = document.createEvent('Event');
trackEvent.initEvent('myTrackEvent', true, true);
MeeMixPlayer.setEventHandler("SongPlaying", function(songData){
fireTrackEvent({title:songData.title,artist:songData.artist,type:'touch'});
});
MeeMixPlayer.setEventHandler("SongFinishing", function(songData){
fireTrackEvent({title:songData.title,artist:songData.artist,type:'finish'});
});
};
// Below is in the context of content script
callback = function(track) {
track.lsource = 'Meemix.com';
track.source = 'E';
safari.self.tab.dispatchMessage("track",track);
};
injectHooks(LikeFMInject,callback);
} else if (window.location.host.indexOf("grooveshark.com") > -1) {
function fireTrackEvent (data) {
// Context of the page
var hiddenDiv = document.getElementById('LikeFMComm');
hiddenDiv.innerText = JSON.stringify(data);
hiddenDiv.dispatchEvent(trackEvent);
if (data.status == 'playing' && !data.statusUpdate) {
// 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;
}
}
LikeFMInject = function() {
// 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();
}
callback = function(data) {
var track = {};
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';
safari.self.tab.dispatchMessage("track",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';
safari.self.tab.dispatchMessage("track",track);
LikeFM.currentTrack = null;
}
};
injectHooks(LikeFMInject,callback,fireTrackEvent);
}
function injectHooks(hooks,callback,trackEventCallback) {
var trackEvent;
if ( trackEventCallback ) {
fireTrackEvent = trackEventCallback;
} else {
function fireTrackEvent (data) {
var hiddenDiv = document.getElementById('LikeFMComm');
hiddenDiv.textContent = JSON.stringify(data);
hiddenDiv.dispatchEvent(trackEvent);
}
}
// 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 + '('+ hooks +')();'));
document.documentElement.getElementsByTagName("HEAD")[0].appendChild(script);
}
// Comm link medium div
if (!document.getElementById("LikeFMComm")) {
var comm = document.createElement("div");
comm.setAttribute("id","LikeFMComm");
comm.style.display = 'none';
document.getElementsByTagName('body')[0].appendChild(comm);
// Comm link with injected script
comm.addEventListener('myTrackEvent', function() {
var data = JSON.parse(comm.textContent);
callback(data);
},true);
}
}