forked from josephdadams/spotify-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
66 lines (58 loc) · 1.61 KB
/
util.js
File metadata and controls
66 lines (58 loc) · 1.61 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
'use strict'
const { Notification, nativeImage } = require('electron')
const _ = require('lodash')
const config = require('./config.js')
const API = require('./api.js')
// 1x1 transparent PNG – used when stored icon is missing or invalid (e.g. corrupted config)
const FALLBACK_ICON_DATA_URL =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='
function getIcon() {
const raw = config.get('icon')
if (
typeof raw === 'string' &&
raw.startsWith('data:image') &&
raw.length > 0 &&
raw.length < 500000
) {
try {
return nativeImage.createFromDataURL(raw)
} catch (_) {
// invalid data URL, use fallback
}
}
return nativeImage.createFromDataURL(FALLBACK_ICON_DATA_URL)
}
function showNotification() {
const icon = getIcon()
if (global.STATUS.playbackInfo.playerState === 'Playing' && config.get('showNotifications')) {
const NOTIFICATION_TITLE = global.STATUS.playbackInfo.name
const NOTIFICATION_BODY = global.STATUS.playbackInfo.artist
new Notification({
title: NOTIFICATION_TITLE,
subtitle: NOTIFICATION_BODY,
icon: icon,
silent: true,
}).show()
}
}
module.exports = {
getIcon,
processNotification: function (event, info) {
try {
if (config.get('allowedEvents').includes(event)) {
//do the stuff with the things
switch (event) {
case 'com.spotify.client.PlaybackStateChanged':
global.STATUS.playbackInfo = _.mapKeys(info, (v, k) => _.camelCase(k))
API.sendUpdates()
showNotification()
break
default:
break
}
}
} catch (error) {
console.log(error)
}
},
}