-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
128 lines (118 loc) · 4.38 KB
/
background.js
File metadata and controls
128 lines (118 loc) · 4.38 KB
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
chrome.runtime.onStartup.addListener( () => {
chrome.action.setBadgeBackgroundColor({ color: '#0062ff' });
chrome.storage.local.get('total', (counter) => {
if (counter.total) {
chrome.action.setBadgeText({'text': counter.total.toString()})
} else {
chrome.action.setBadgeText({'text': '0'})
}
})
})
chrome.action.onClicked.addListener( () => {
const newTimestamp = Date.now()
chrome.storage.local.get(['total', 'step', 'limit', 'notification', 'sound', 'volume', 'chronology'], (counter) => {
const step = counter.step
let newTotal = counter.total + step
if (!Number.isInteger(newTotal)) {
const digitsBeforePoint = Math.ceil(Math.log10(Math.floor(Math.abs(newTotal))+1))
const toPrecisionIndex = digitsBeforePoint + 1
const preciseTotal = newTotal.toPrecision(toPrecisionIndex)
newTotal = Math.trunc(preciseTotal * 10) / 10
}
if (counter.notification) {
const limit = counter.limit
sendNotification(step, newTotal, limit)
}
//SOUND
/* if (counter.sound) {
const clickSound = new Audio(chrome.runtime.getURL('Res/Sounds/click_128.mp3'))
clickSound.volume = counter.volume
clickSound.play()
} */
if (counter.sound) {
const source = chrome.runtime.getURL('Res/Sounds/click_128.mp3')
const volume = counter.volume
playSound(source, volume)
}
const chronology = counter.chronology.length < 1000 ? counter.chronology : counter.chronology.slice(-199)
chronology.push(newTimestamp)
chrome.storage.local.set({'total': newTotal, 'timestamp': newTimestamp, 'chronology': chronology}, () => {
chrome.action.setBadgeText({'text': newTotal.toString()})
})
})
})
chrome.runtime.onInstalled.addListener((details) => {
/* const currentVersion = chrome.runtime.getManifest().version
const previousVersion = details.previousVersion */
const reason = details.reason
switch (reason) {
case 'install':
chrome.storage.local.set({
"limit": 0,
"notification": false,
"step": 1,
"total": 0,
"sound": false,
"volume": 0.5,
"timestamp": "",
"showTimestamp": true,
"chronology": [],
"chronologyOrder": "oldest"
}, () => {
chrome.action.setBadgeBackgroundColor({ color: '#0062ff' });
setUpContextMenus()
})
break;
case 'update':
chrome.storage.local.get(['total', 'step', 'limit', 'notification', 'sound', 'volume', 'timestamp', 'showTimestamp', 'chronology', 'chronologyOrder'], (counter) => {
let notification = counter.notification ? counter.notification : false
let total = counter.total ? counter.total : 0
let step = counter.step ? counter.step : 1
let limit = counter.limit ? counter.limit : 0
let sound = counter.sound ? counter.sound : false
let volume = counter.volume ? counter.volume : 0.5
let timestamp = counter.timestamp ? counter.timestamp : ''
let showTimestamp = typeof counter.showTimestamp == 'boolean' ? counter.showTimestamp : true
let chronology = counter.chronology ? counter.chronology : []
let chronologyOrder = counter.chronologyOrder ? counter.chronologyOrder : 'oldest'
chrome.storage.local.set({
"limit": limit,
"step": step,
"total": total,
"notification": notification,
"sound": sound,
"volume": volume,
"timestamp": timestamp,
"showTimestamp": showTimestamp,
"chronology": chronology,
"chronologyOrder": chronologyOrder
}, () => {
chrome.action.setBadgeText({'text': total.toString()})
chrome.action.setBadgeBackgroundColor({ color: '#0062ff' });
chrome.contextMenus.removeAll(() => {
setUpContextMenus()
})
})
})
break;
case 'chrome_update':
break;
case 'shared_module_update':
break;
default:
break;
}
})
async function playSound(source, volume) {
await createOffscreen();
await chrome.runtime.sendMessage({ play: { source, volume } });
}
// Create offscreen document if one doesn't already exist
async function createOffscreen() {
if (await chrome.offscreen.hasDocument()) return;
await chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: ['AUDIO_PLAYBACK'],
justification: 'testing'
});
}