-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
152 lines (128 loc) · 3.03 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
// SETTINGS
var default_options = {
inactivity_time: 30
}
var equalizer = {
min: 1,
max: 11,
current: 1,
prefix: 'img/equalizer/step',
suffix: '.png',
update: 80 // ms
}
var stream = 'http://flux.nina.fm/nina.mp3'
// SCRIPT
var settings, ninaPlayer, inactivity, time, refreshInterval
chrome.storage.sync.get(default_options, function (data) {
chrome.storage.sync.set(data)
settings = data
// Initialize the player
ninaPlayer = new Audio(stream)
ninaPlayer.muted = true
ninaPlayer.load()
// Launch the icon periodical update
updateIcon()
// Handle the click on extension icon
chrome.browserAction.onClicked.addListener(toggleAudio)
})
// FUNCTIONS
/**
* Play the audio stream
*/
function playAudio () {
ninaPlayer.src = stream
ninaPlayer.muted = false
ninaPlayer.play()
// Init data for inactivity check
inactivity = Date.now()
time = ninaPlayer.currentTime
// Run the live refresh
refreshInterval = setInterval(refresh, 1000)
}
/**
* Stop the audio stream
*/
function stopAudio () {
// Stop the live refresh
clearInterval(refreshInterval)
// Kill the audio stream with blank audio data
ninaPlayer.pause()
ninaPlayer.src = 'data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEAVFYAAFRWAAABAAgAZGF0YQAAAAA='
time = ninaPlayer.currentTime = 0
inactivity = Date.now()
}
/**
* Callback function to toggle the mute status
*/
function toggleAudio () {
if (ninaPlayer.paused) {
playAudio()
} else {
ninaPlayer.muted = !ninaPlayer.muted
if (ninaPlayer.muted) {
inactivity = Date.now()
}
}
}
/**
* Callback function for refreshing
*/
function refresh () {
chrome.storage.sync.get(['inactivity_time'], function (data) {
settings = data
})
checkPlayer()
}
/**
* Function to check the player status
*/
function checkPlayer () {
if (!ninaPlayer.muted && playerIsDown()) {
playAudio()
}
if (ninaPlayer.muted && inactivityExpired()) {
stopAudio()
}
}
/**
* Check if the inactivity delay is expired
* @returns {boolean}
*/
function inactivityExpired () {
let inactivityDuration = Date.now() - inactivity
let delay = settings.inactivity_time * 1000
return inactivityDuration > delay
}
/**
* Check if the player is down
* @returns {boolean}
*/
function playerIsDown () {
return time >= ninaPlayer.currentTime && time > 0
}
/**
* Function to update the extension icon
* depending on the mute status
*/
function updateIcon () {
// Default icon is the off one
let icon = equalizer.prefix + 'off' + equalizer.suffix
// If player is on, loop on the equalizer images
if (ninaPlayer && !ninaPlayer.muted) {
if (equalizer.current > equalizer.max) {
equalizer.current = equalizer.min
}
icon = equalizer.prefix + equalizer.current + equalizer.suffix
equalizer.current++
}
// Update the icon
chrome.browserAction.setIcon({
path: icon,
},
function () {
chrome.runtime.lastError
}
)
// Call this function again after a timeout
window.setTimeout(updateIcon, equalizer.update)
}