-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.html
110 lines (99 loc) · 4.6 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
<!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>
// Comm link with content script
safari.application.addEventListener("message",
function(theMessageEvent) {
if (theMessageEvent.name == 'track') {
var track = theMessageEvent.message;
if (track.title) {
// Explicit track
LikeFM.currentTrack = track;
if (track.type == 'touch') {
LikeFM.sendTouchSignal(false);
}
else
LikeFM.sendFinishSignal();
} else if (track.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 != LikeFM.rawTrack || (new Date().getTime() - LikeFM.timestamp) > 60000) {
// LikeFM.timestamp = new Date().getTime();
// Minimal control
// TODO: send search string to server to get track object back
// var track = ...
var args = {
method: 'track.normalize', // Like.fm only api method
q: track.query
};
$.get('http://like.fm/api/1.0/',args,function(nTrack,code) {
if (!nTrack.error) {
nTrack.lsource = track.lsource;
nTrack.source = track.source;
LikeFM.currentTrack = nTrack;
if (track.type == 'touch') {
LikeFM.sendTouchSignal(false);
}
else
LikeFM.sendFinishSignal();
// LikeFM.sendTouchSignal(true);
}
},'json');
LikeFM.rawTrack = track;
//}
}
} else if (theMessageEvent.name == 'link') {
// Link account
var args = {
method:'auth.getToken',
api_key:'ac5dbe86ac1e96c2d31f8d1d'
}
args['api_sig'] = calculateSignature(args,'4c5fbddec6eea1aecedaa2ff');
if (localStorage['token']) {
var appLinkTab = safari.application.openBrowserWindow().activeTab;
appLinkTab.url = 'https://like.fm/api/auth/?api_key=' + args['api_key'] + '&token=' + localStorage['token'];
} else {
$.get('http://like.fm/api/1.0',args,function(data,code) {
localStorage['token'] = data['token'];
var appLinkTab = safari.application.openBrowserWindow().activeTab;
appLinkTab.url = 'https://like.fm/api/auth/?api_key=' + args['api_key'] + '&token=' + data['token'];
},'json');
}
} else if (theMessageEvent.name == '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('http://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 (theMessageEvent.name == 'checkSession') {
if (!localStorage['session_key']) {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("promptLink", true);
}
} else if (theMessageEvent.name == 'clearTrack') {
//LikeFM.currentTrack = null;
//LikeFM.rawTrack = null;
}
},false);
</script>
</head>
</html>