-
Notifications
You must be signed in to change notification settings - Fork 1
/
integrate.js
287 lines (250 loc) · 9.24 KB
/
integrate.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
/*
* Copyright 2014 Stefano Bagnatica <[email protected]>
* Copyright 2018 Jiří Janoušek <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
'use strict';
(function (Nuvola) {
// Create media player component
const player = Nuvola.$object(Nuvola.MediaPlayer)
// Translations
const C_ = Nuvola.Translate.pgettext
// Custom actions
const ACTION_THUMBS_UP = 'thumbs-up'
const ACTION_THUMBS_DOWN = 'thumbs-down'
// Handy aliases
const PlaybackState = Nuvola.PlaybackState
const PlayerAction = Nuvola.PlayerAction
// Create new WebApp prototype
const WebApp = Nuvola.$WebApp()
// Initialization routines
WebApp._onInitWebWorker = function (emitter) {
Nuvola.WebApp._onInitWebWorker.call(this, emitter)
const state = document.readyState
if (state === 'interactive' || state === 'complete') {
this._onPageReady()
} else {
document.addEventListener('DOMContentLoaded', this._onPageReady.bind(this))
}
}
WebApp._onInitAppRunner = function (emitter) {
Nuvola.WebApp._onInitAppRunner.call(this, emitter)
Nuvola.actions.addAction('playback', 'win', ACTION_THUMBS_UP, C_('Action', 'Thumbs up'),
null, null, null, null)
Nuvola.actions.addAction('playback', 'win', ACTION_THUMBS_DOWN, C_('Action', 'Thumbs down'),
null, null, null, null)
}
// Page is ready for magic
WebApp._onPageReady = function () {
// Connect handler for signal ActionActivated
Nuvola.actions.connect('ActionActivated', this)
// Jango does not support going to previous
player.setCanGoPrev(false)
// custom actions
const actions = [ACTION_THUMBS_UP, ACTION_THUMBS_DOWN]
player.addExtraActions(actions)
// inizialize last thumb up / down variables
this.lastThumbUp = null
this.lastThumbDown = null
this.volumeInitialized = false
// Start update routine
this.update()
}
// Extract data from the web page
WebApp.update = function () {
// Default values
let state = PlaybackState.UNKNOWN
let albumArt = null
let song = null
let artist = null
let album = null
// Retrieve song details
song = this.getCurrentSongName()
if (song === null || song === 'Loading Music…') {
// is still loading music... wait some more...
setTimeout(this.update.bind(this), 250)
return
}
// Retrieve playing status details
state = this.isPlaying() ? PlaybackState.PLAYING : PlaybackState.PAUSED
// Retrieve artist and album details
let el = this.getJangoElement('player_current_artist')
if (el != null) {
const elLink = el.getElementsByTagName('a')
if (elLink.length > 0) artist = elLink[0].textContent.trim()
else artist = el.textContent.trim()
// now album name is declared near the artist name
if (el.childNodes.length > 4) album = el.childNodes[4].textContent
}
// I use current song image as album art
el = this.getJangoElement('player_main_pic_img')
if (el != null) albumArt = el.src
const shuffle = this._getShuffleButton()
// Update actions
const actionsEnabled = {}
actionsEnabled[PlayerAction.SHUFFLE] = !!shuffle
actionsEnabled[ACTION_THUMBS_UP] = false
actionsEnabled[ACTION_THUMBS_DOWN] = false
switch (state) {
case PlaybackState.PLAYING:
player.setCanGoNext(true)
player.setCanPause(true)
player.setCanPlay(false)
actionsEnabled[ACTION_THUMBS_UP] = true
actionsEnabled[ACTION_THUMBS_DOWN] = true
break
case PlaybackState.PAUSED:
player.setCanGoNext(true)
player.setCanPause(false)
player.setCanPlay(true)
actionsEnabled[ACTION_THUMBS_UP] = true
actionsEnabled[ACTION_THUMBS_DOWN] = true
break
default:
player.setCanGoNext(false)
player.setCanPause(false)
player.setCanPlay(false)
}
Nuvola.actions.updateEnabledFlags(actionsEnabled)
Nuvola.actions.updateState(PlayerAction.SHUFFLE, shuffle && shuffle.parentNode.classList.contains('on'))
// set track info
const track = {
title: song,
artist: artist,
album: album,
artLocation: albumArt
}
player.setTrack(track)
// set playback state (playing / pause)
player.setPlaybackState(state)
const elms = this._getElements()
if (elms.volumeBar) {
// ~ elms.volumeBar.parentNode.parentNode.style.display = 'block'
player.updateVolume(elms.volumeBar.style.left.split('%')[0] / 100)
// ~ elms.volumeBar.parentNode.parentNode.style.display = 'none'
}
player.setCanChangeVolume(!!elms.volumeBar)
// Schedule the next update
setTimeout(this.update.bind(this), 500)
}
// Handler of playback actions
WebApp._onActionActivated = function (emitter, name, param) {
switch (name) {
/* Base media player actions */
case PlayerAction.TOGGLE_PLAY:
case PlayerAction.PLAY:
case PlayerAction.PAUSE:
case PlayerAction.STOP:
this.clickJangoButton('btn-playpause')
break
case PlayerAction.NEXT_SONG:
this.clickJangoButton('btn-ff')
break
case PlayerAction.CHANGE_VOLUME: {
const elms = this._getElements()
elms.volumeIcon.style.display = 'none'
elms.volumeBar.parentNode.parentNode.style.display = 'block'
Nuvola.clickOnElement(elms.volumeBar.parentNode, param, 0.5)
elms.volumeBar.parentNode.parentNode.style.display = 'none'
elms.volumeIcon.style.display = 'block'
break
}
case PlayerAction.SHUFFLE:
Nuvola.clickOnElement(this._getShuffleButton())
break
/* Custom actions */
case ACTION_THUMBS_UP:
this.clickJangoButton('btn-fav')
setTimeout(this.autoCommit.bind(this), 250)
this.lastThumbUp = this.getCurrentSongName()
break
case ACTION_THUMBS_DOWN:
this.clickJangoButton('player_ban')
setTimeout(this.autoCommit.bind(this), 250)
this.lastThumbDown = this.getCurrentSongName()
break
}
}
WebApp._getElements = function () {
const elms = {
volumeIcon: document.getElementById('volume_icon'),
volumeBar: document.getElementById('volumeHandle')
}
if (elms.volumeIcon && elms.volumeBar && !this.volumeInitialized) {
Nuvola.triggerMouseEvent(elms.volumeIcon, 'mouseover')
Nuvola.triggerMouseEvent(elms.volumeBar, 'mouseout')
elms.volumeBar.parentNode.parentNode.style.display = 'none'
elms.volumeIcon.style.display = 'block'
this.volumeInitialized = true
}
return elms
}
WebApp._getShuffleButton = function () {
return document.querySelector('#action_shuffle a')
}
/**
* Get DOM element by id, from the document of Jango content frame
* @param id Element id
*/
WebApp.getJangoElement = function (id) {
return document.getElementById(id)
}
/**
* Emulate click on an element in Jango content frame
* @param id Element id
*/
WebApp.clickJangoButton = function (id) {
Nuvola.clickOnElement(this.getJangoElement(id))
}
/**
* Find if Jango is playing, looking for button class name
*/
WebApp.isPlaying = function () {
const el = this.getJangoElement('btn-playpause')
if (el == null) return false
return (el.className === 'player_ctrls pause')
}
WebApp.getCurrentSongName = function () {
const el = this.getJangoElement('current-song')
if (el == null) return null
return el.textContent.trim()
}
/**
* Click automatically the commit button after thumb up / down
* @param trycount Trying counter for button found
*/
WebApp.autoCommit = function () {
// find the commit button
const els = document.getElementsByName('commit')
if (els != null && els.length > 0) {
const element = els[0]
Nuvola.clickOnElement(element)
} else {
// the commit button could not be ready... retry for max 4 times
if (this.trycount === undefined) this.trycount = 1
if (this.trycount < 4) {
setTimeout(this.autoCommit.bind(this), 250)
}
}
}
WebApp.start()
})(this) // function(Nuvola)