/
background.js
305 lines (275 loc) · 8.49 KB
/
background.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Generated by CoffeeScript 1.6.2
(function() {
"use strict";
/**
# *Groovetoggle* lets you control playback of Grooveshark from Opera.
#
# @module GrooveToggle
*/
var GrooveToggle, opera, root, window;
root = this;
window = this.window;
opera = root.opera;
GrooveToggle = {
/**
# `Button` works a container for methods that manage the toolbar button.
#
# @namespace GrooveToggle
# @class Button
*/
Button: (function() {
var button, defaults, publicMethods, toolbar;
defaults = {
disabled: false,
icon: 'play_16.png',
title: '',
onclick: function() {}
};
button = void 0;
toolbar = opera.contexts.toolbar;
return publicMethods = {
/**
# Creates the button with specified or default options but does not show
# it by default. May be called by other methods if not called before.
#
# @method init
# @chainable
# @param {Object} [options={}] Defines button options.
# @param {Boolean} [options.disabled=false] Should the icon be disabled when shown?
# @param {String} [options.icon=""] URL to the icon file.
# @param {Function} [options.onclick=null] Callback that will handle the click event.
# @param {String} [options.title=""] Content of tooltip when hovering over
# button.
*/
init: function(options) {
if (options == null) {
options = {};
}
if (button !== void 0) {
return this;
}
button = toolbar.createItem(defaults);
button.update(options);
return this;
},
/**
# Update buttons options.
#
# @method update
# @chainable
# @param {Object} options Collection of properties to update.
# @param {String} [options.title] New content for tooltip.
# @param {String} [options.icon] New URL for icon.
# @param {Boolean} [options.disabled] Should the button be disabled?
# @param {Function} [options.onclick] New handler for click event.
*/
update: function(options) {
if (options == null) {
options = {};
}
if (button === void 0) {
this.init();
}
button.update(options);
return this;
},
/**
# Shows the button in the toolbar. It will be initialized if it has not
# been done before.
#
# @method show
# @chainable
*/
show: function() {
if (button === void 0) {
this.init();
}
toolbar.addItem(button);
return this;
},
/**
# Hides the button in the toolbar. It will be initialized if it has not
# been done before.
#
# @method hide
# @chainable
*/
hide: function() {
if (button === void 0) {
this.init();
}
toolbar.removeItem(button);
return this;
},
/**
# Is the button currently being shown in the toolbar?
#
# @method isVisible
# @return {Boolean} Is visible or not.
*/
isVisible: function() {
return !!toolbar.length;
}
};
})(),
/**
# `myBgApp` contains the background process logic including listeners.
#
# @namespace GrooveToggle
# @class myBgApp
*/
myBgApp: (function() {
var interval, pub, source, tab, tabId;
source = void 0;
tab = void 0;
tabId = void 0;
interval = void 0;
return pub = {
/**
# Listen to messages from injected script.
#
# @method listen
# @param {Object} [message] An object that represents the message. It must contain a `topic` and a `body`.
*/
listen: function(message) {
var _name;
return typeof this[_name = message.data.topic] === "function" ? this[_name](message) : void 0;
},
/**
# Triggered when the injected script sends the first
# * Shows the button on toolbar.
# * Saves a reference to the injected script for later messaging.
# * Saves a reference to the tab so we can check current URL.
#
# @method groovetoggleConnect
# @param {MessageEvent} [message] Original message event.
*/
groovetoggleConnect: function(message) {
var allTabs, length;
source = message.source;
allTabs = opera.extension.tabs.getAll();
length = allTabs.length;
while (length) {
length -= 1;
if (allTabs[length].port === source) {
tab = allTabs[length];
tabId = tab.id;
}
break;
}
GrooveToggle.Button.init().update({
title: 'GrooveToggle',
icon: 'play_16.png',
onclick: this.handleClick
}).show();
return this.pingTab();
},
/**
# Listen to clicks to button on Opera toolbar.
# It always send a message to injected script even if there are no songs
# in the queue cause we can't suscribe to the "restore-queue" event.
#
# @method handleClick
*/
handleClick: function() {
source.postMessage({
topic: 'togglePlayPause'
});
},
/**
# Listen to changes of the song status.
#
# @method handleStatus
# @param {Object} message The original message sent by injected script.
*/
handleStatus: function(message) {
var body, result, song, status, title;
body = message.data.body;
song = body.song;
switch (body.status) {
case 'none':
result = {
title: '',
icon: 'play_16.png'
};
break;
case 'paused':
status = body.status.charAt(0).toUpperCase() + body.status.substr(1);
title = song ? "" + status + " " + song.songName + " by " + song.artistName : "" + status;
result = {
title: title,
icon: 'pause_16.png'
};
break;
case 'loading':
case 'playing':
case 'completed':
status = body.status.charAt(0).toUpperCase() + body.status.substr(1);
title = song ? "" + status + " " + song.songName + " by " + song.artistName : "" + status;
result = {
title: title,
icon: 'play_16.png'
};
}
GrooveToggle.Button.update(result);
},
/**
# Check if the tab that have been closed if the one hosting the injected script.
#
# @method handleTabClose
# @param {Object} e The original event.
*/
handleTabClose: function(e) {
if (e.tab.id === tabId) {
return this.destroy();
}
},
/**
# Hide button and remove references to injected script.
#
# @method destroy
*/
destroy: function() {
var button;
window.clearInterval(interval);
GrooveToggle.Button.hide();
return tab = button = tabId = source = null;
},
/**
# Ping the tab with the injected script to make sure we are still in Grooveshark.
#
# @method pingTab
*/
pingTab: function() {
var self, urlRegEx;
self = this;
urlRegEx = /^http[s]?:\/\/grooveshark\.com.*/;
return interval = window.setInterval(function() {
var _ref;
if ((_ref = window.console) != null) {
_ref.log('interval');
}
if (!urlRegEx.test(tab.url)) {
window.clearInterval(interval);
return self.destroy();
}
}, 1000);
},
/**
# Init the app.
#
# @method init
*/
init: function() {
opera.extension.onmessage = function(e) {
return GrooveToggle.myBgApp.listen.call(GrooveToggle.myBgApp, e);
};
return opera.extension.tabs.onclose = function(e) {
return GrooveToggle.myBgApp.handleTabClose.call(GrooveToggle.myBgApp, e);
};
}
};
})()
};
GrooveToggle.myBgApp.init();
}).call(this);