-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.html
239 lines (218 loc) · 11.4 KB
/
background.html
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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="lib/jquery.min.js"></script>
<script type="text/javascript" src="lib/crypto-min.js"></script>
<script type="text/javascript" src="lib/md5-min.js"></script>
<script type="text/javascript" src="lib/main.js"></script>
<script>
if (!localStorage['firstLaunch']) {
var args = {
method:'auth.getToken',
api_key:'ac5dbe86ac1e96c2d31f8d1d'
}
args['api_sig'] = calculateSignature(args,'4c5fbddec6eea1aecedaa2ff');
$.get('https://like.fm/api/1.0',args,function(data,code) {
localStorage['token'] = data['token'];
// Find tab with browser install page opened
chrome.tabs.getAllInWindow(null, function (tabs) {
for (x in tabs) {
if (tabs[x].url.match(/^https?:\/\/(?:www.)?like.fm\/signup\/download/i)) {
chrome.tabs.update(tabs[x].id,{url:"https://like.fm/api/auth/?api_key=" + args['api_key'] + "&token=" + data['token']});
return false;
}
}
chrome.tabs.create({
url:"https://like.fm/api/auth/?api_key=" + args['api_key'] + "&token=" + data['token']
});
});
},'json');
localStorage['firstLaunch'] = true;
}
// Page action
chrome.pageAction.onClicked.addListener(function(tab) {
chrome.tabs.sendRequest(tab.id, {messageType: "statusBox.show"}, function(response) {
});
});
// Comm link with content script
chrome.extension.onRequest.addListener(
function(track, sender, sendResponse) {
if (track.messageType == 'track') {
if (track.data.title) {
// Explicit track
if (track.data.type == 'touch') {
LikeFM.sendTouchSignal(false,function(resp){
// Display notice
chrome.tabs.sendRequest(sender.tab.id, {messageType: "statusBox.show", resp: resp, session_key: localStorage["session_key"]}, function(response) {});
chrome.tabs.getSelected(null,function(tab){
if (tab.id != sender.tab.id) {
// Display desktop notification
}
});
});
} else
LikeFM.sendFinishSignal();
LikeFM.currentTrack = track.data;
} else if (track.data.query) {
// Only report track if no report timestamp found, or track is different, or if timeout has passed for current track
//if (!LikeFM.timestamp || track.data != LikeFM.rawTrack || (new Date().getTime() - LikeFM.timestamp) > 60000) {
//LikeFM.timestamp = new Date().getTime();
// Minimal control
// var track = ...
var args = {
method: 'track.normalize', // Like.fm only api method
q: track.data.query
};
$.get('https://like.fm/api/1.0/',args,function(nTrack,code) {
if (!nTrack.error) {
nTrack.lsource = track.data.lsource;
nTrack.source = track.data.source;
LikeFM.currentTrack = nTrack;
if (track.data.type == 'touch') {
LikeFM.sendTouchSignal(false,function(resp){
// Display notice
chrome.tabs.sendRequest(sender.tab.id, {messageType: "statusBox.show", resp: resp, session_key: localStorage["session_key"]}, function(response) {});
chrome.tabs.getSelected(null,function(tab){
if (tab.id != sender.tab.id) {
// Display desktop notification
}
});
});
}
else
LikeFM.sendFinishSignal();
}
},'json');
//LikeFM.rawTrack = track.data;
//}
} else if (track.data.amazonLink) {
$.get(track.data.amazonLink,function(data){
if (document.getElementById("ext-root")) {
var ext = document.getElementById("ext-root");
ext.innerHTML = data;
} else {
var ext = document.createElement("div");
ext.setAttribute("id","ext-root");
ext.innerHTML = data;
$("body")[0].appendChild(ext);
}
track.data.title = $("#btAsinTitle").text();
track.data.artist = $("#btAsinTitle").parent().next().text();
LikeFM.currentTrack = track.data;
if (track.data.type == 'touch') {
// Get response object from TOUCH
LikeFM.sendTouchSignal(false,function(resp){
// Display notice
chrome.tabs.sendRequest(sender.tab.id, {messageType: "statusBox.show", resp: resp, session_key: localStorage["session_key"]}, function(response) {});
chrome.tabs.getSelected(null,function(tab){
if (tab.id != sender.tab.id) {
// Display desktop notification
}
});
});
}
else
LikeFM.sendFinishSignal();
});
}
} else if (track.messageType == 'link') {
// Link account
var args = {
method:'auth.getToken',
api_key:'ac5dbe86ac1e96c2d31f8d1d'
}
args['api_sig'] = calculateSignature(args,'4c5fbddec6eea1aecedaa2ff');
if (localStorage['token']) {
chrome.tabs.create({
url:"https://like.fm/api/auth/?api_key=" + args['api_key'] + "&token=" + localStorage['token']
});
} else {
$.get('https://like.fm/api/1.0',args,function(data,code) {
localStorage['token'] = data['token'];
chrome.tabs.create({
url:"https://like.fm/api/auth/?api_key=" + args['api_key'] + "&token=" + data['token']
});
},'json');
}
} else if (track.messageType == 'getSession') {
// Grab session and init session
if (localStorage['token']) {
var args = {
'method': 'auth.getSession',
'api_key': 'ac5dbe86ac1e96c2d31f8d1d',
'token': localStorage['token']
};
args['api_sig'] = calculateSignature(args,'4c5fbddec6eea1aecedaa2ff');
// Get session with token
$.get('https://like.fm/api/1.0',args,function(data) {
if(data['error']) {
delete localStorage['token'];
}
if (data['session']['name'] && data['session']['key']) {
localStorage['name'] = data['session']['name'];
localStorage['session_key'] = data['session']['key'];
LikeFM.handshake();
delete localStorage['token'];
}
},'json');
}
} else if (track.messageType == 'checkSession') {
if (!localStorage['session_key'])
sendResponse(true);
else
sendResponse(false);
} else if (track.messageType == 'getTrack') {
sendResponse(LikeFM.currentTrack);
} else if (track.messageType == 'statusBox.resize') {
var data = {};
data.width = track.data.width;
data.height = track.data.height;
chrome.tabs.sendRequest(sender.tab.id, {messageType: "statusBox.resize", data: data}, function(response) {});
} else if (track.messageType == 'statusBox.show') {
chrome.tabs.sendRequest(sender.tab.id, {messageType: "statusBox.show", resp: resp, session_key: localStorage["session_key"]}, function(response) {});
}
sendResponse({});
});
// Called when the url of a tab changes.
function checkUrl(tabId, changeInfo, tab) {
if (changeInfo.status == 'loading') {
//LikeFM.handshake();
//LikeFM.currentTrack = null;
//LikeFM.rawTrack = null;
chrome.tabs.executeScript(tabId,
{file:"globalInject.js"});
if (tab.url.match(/^https?:\/\/(?:www.)?pandora.com/i)) {
chrome.pageAction.show(tabId);
LikeFM.initPandora(tabId);
} else if (tab.url.match(/^https?:\/\/(?:www.)?youtube.com/i)) {
chrome.pageAction.show(tabId);
LikeFM.initYoutube(tabId);
} else if (tab.url.match(/^https?:\/\/(?:listen.)?grooveshark.com/i)) {
chrome.pageAction.show(tabId);
LikeFM.initGrooveshark(tabId);
} else if (tab.url.match(/^https?:\/\/(?:www.)?meemix.com/i)) {
chrome.pageAction.show(tabId);
LikeFM.initMeemix(tabId);
} else if (tab.url.match(/^https?:\/\/(?:www.)?earbits.com/i)) {
chrome.pageAction.show(tabId);
LikeFM.initEarbits(tabId);
} else if (tab.url.match(/^https?:\/\/(?:www.)?rdio.com/i)) {
chrome.pageAction.show(tabId);
LikeFM.initRdio(tabId);
} else if (tab.url.match(/^https?:\/\/(?:www.)?beta.deezer.com/i)) {
chrome.pageAction.show(tabId);
LikeFM.initBetaDeezer(tabId);
} else if (tab.url.match(/^https?:\/\/(?:www.)?www.live365.com/i)) {
chrome.pageAction.show(tabId);
LikeFM.initLive365(tabId);
} else if (tab.url.match(/^https?:\/\/(?:www.)?like.fm/i)) {
chrome.pageAction.show(tabId);
LikeFM.initSupported(tabId);
}
}
}
// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkUrl);
</script>
</head>
</html>