-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.coffee
272 lines (228 loc) · 7.38 KB
/
background.coffee
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
"use strict"
###*
# *Groovetoggle* lets you control playback of Grooveshark from Opera.
#
# @module GrooveToggle
###
root = @
{window} = @
opera = root.opera
GrooveToggle =
###*
# `Button` works a container for methods that manage the toolbar button.
#
# @namespace GrooveToggle
# @class Button
###
Button: do ->
# Default options for creating toolbar button. Not all available options
# are described.
defaults =
disabled: false
icon: 'play_16.png'
title: ''
onclick: ->
# Private button instance
button = undefined
# Shortcuts
toolbar = opera.contexts.toolbar
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: (options = {}) ->
if button isnt undefined then return @
button = toolbar.createItem defaults
button.update options
@
###*
# 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: (options = {}) ->
if button is undefined then do @init
# Opera takes care of selecting which keys are valid.
button.update options
@
###*
# Shows the button in the toolbar. It will be initialized if it has not
# been done before.
#
# @method show
# @chainable
###
show: ->
if button is undefined then do @init
toolbar.addItem button
@
###*
# Hides the button in the toolbar. It will be initialized if it has not
# been done before.
#
# @method hide
# @chainable
###
hide: ->
if button is undefined then do @init
toolbar.removeItem button
@
###*
# Is the button currently being shown in the toolbar?
#
# @method isVisible
# @return {Boolean} Is visible or not.
###
isVisible: ->
!!toolbar.length
###*
# `myBgApp` contains the background process logic including listeners.
#
# @namespace GrooveToggle
# @class myBgApp
###
myBgApp: do ->
# Gateway for messaging injected script.
source = undefined
# The tab id where the injected script is running.
tab = undefined # for pingTab
tabId = undefined # for handleTabClose
# Reference to the pingTab setInterval
interval = undefined
# Return public methods
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: (message) ->
@[message.data.topic]? message
###*
# 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: (message) ->
# Note here that we overwrite the port since this method is called only
# when the script is injected. This means that new Grooveshark tabs are
# preferred over the previous one created.
{source} = message
# Using a while loop cause we wanna break as soon as we find the right tab
allTabs = do opera.extension.tabs.getAll
{length} = allTabs
while length
length -= 1
if allTabs[length].port is source
tab = allTabs[length]
tabId = tab.id
break
# Show button on toolbar.
GrooveToggle.Button.init().update(
title: 'GrooveToggle'
icon: 'play_16.png'
onclick: @handleClick
).show()
# Ping tab url
do @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: ->
source.postMessage topic:'togglePlayPause'
return
###*
# Listen to changes of the song status.
#
# @method handleStatus
# @param {Object} message The original message sent by injected script.
###
handleStatus: (message) ->
{body} = message.data
{song} = body
switch body.status
when 'none'
result =
title: ''
icon: 'play_16.png'
when 'paused'
status = do body.status.charAt(0).toUpperCase + body.status.substr 1
title = if song then "#{status} #{song.songName} by #{song.artistName}" else "#{status}"
result =
title: title
icon: 'pause_16.png'
when 'loading', 'playing', 'completed'
status = do body.status.charAt(0).toUpperCase + body.status.substr 1
title = if song then "#{status} #{song.songName} by #{song.artistName}" else "#{status}"
result =
title: title
icon: 'play_16.png'
GrooveToggle.Button.update result
return
###*
# Check if the tab that have been closed if the one hosting the injected script.
#
# @method handleTabClose
# @param {Object} e The original event.
###
handleTabClose: (e) ->
if e.tab.id is tabId then do @destroy
###*
# Hide button and remove references to injected script.
#
# @method destroy
###
destroy: ->
window.clearInterval interval
do GrooveToggle.Button.hide
tab = button = tabId = source = null
###*
# Ping the tab with the injected script to make sure we are still in Grooveshark.
#
# @method pingTab
###
pingTab: ->
self = @
urlRegEx = /^http[s]?:\/\/grooveshark\.com.*/
interval = window.setInterval ->
window.console?.log 'interval'
if not urlRegEx.test tab.url
window.clearInterval interval
do self.destroy
, 1000
###*
# Init the app.
#
# @method init
###
init: ->
opera.extension.onmessage = (e) ->
GrooveToggle.myBgApp.listen.call GrooveToggle.myBgApp, e
opera.extension.tabs.onclose = (e) ->
GrooveToggle.myBgApp.handleTabClose.call GrooveToggle.myBgApp, e
do GrooveToggle.myBgApp.init