-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.js
157 lines (135 loc) · 4.87 KB
/
widget.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
const elements = {
widget: document.querySelector(".widget"),
cover: document.querySelector(".songcover"),
title: document.querySelector(".songtitle"),
artist: document.querySelector(".songartist"),
time: document.querySelector(".songtime")
};
const config = {
updateTimeout: 500,
aimpData: {
address: 'localhost',
port: 3333
},
placeholderData: {
title: "Loading",
artist: "AIMP Widget by Ponywka",
cover: 'image.png',
}
};
function updateWidget(data) {
if (data.title) elements.title.innerText = data.title;
if (data.artist) elements.artist.innerText = data.artist;
if (data.cover) elements.cover.src = data.cover;
if (data.time) elements.time.innerText = data.time;
if (typeof data.is_playing == 'boolean') {
if (data.is_playing) {
elements.widget.classList.remove('widget-hide');
} else {
elements.widget.classList.add('widget-hide');
}
}
}
updateWidget(Object.assign(config.placeholderData, {
is_playing: false
}));
function secondsToTime(sec) {
sec = Math.floor(sec);
let seconds = sec % 60;
let minutes = Math.floor(sec / 60) % 60;
let hours = Math.floor(sec / 3600);
minutes = minutes.toString().padStart(2, "0");
seconds = seconds.toString().padStart(2, "0");
return hours > 0 ? `${hours}:${minutes}:${seconds}` : `${minutes}:${seconds}`;
}
function netRequest(params) {
return new Promise((resolve, reject) => {
let postData = JSON.stringify(params.data || {});
let headers = params.headers || {};
headers['Content-Type'] = 'application/json';
let xhr = new XMLHttpRequest();
xhr.open(params.method || 'GET', `http://${params.hostname || 'localhost'}:${params.port || 3333}${params.path || '/'}`);
for (let headername of Object.keys(headers)) xhr.setRequestHeader(headername, headers[headername]);
xhr.send(postData);
xhr.onload = function() {
resolve(xhr.responseText);
};
xhr.onerror = function() {
reject();
};
});
}
function aimpRequest(method, params = {}) {
return netRequest({
hostname: config.aimpData.address,
port: config.aimpData.port,
path: '/RPC_JSON',
method: 'POST',
data: {
"method": method,
"params": params,
"jsonrpc": 2.0
}
});
}
let musicPlayingNow = {};
let workRightNow = false;
let worker = setInterval(async function() {
if (workRightNow) return;
workRightNow = true;
work: try {
let playerControlPanelState = await aimpRequest("GetPlayerControlPanelState");
if (playerControlPanelState == "") {
// No info received, hiding widget
updateWidget({
is_playing: false
});
break work;
}
playerControlPanelState = JSON.parse(playerControlPanelState);
if (playerControlPanelState.error) {
// Error in responce, hiding widget
updateWidget({
is_playing: false
});
break work;
}
// Update time + Hide/Show widget when music paused/playing
updateWidget({
time: `${secondsToTime(playerControlPanelState.result.track_position)} / ${secondsToTime(playerControlPanelState.result.track_length)}`,
is_playing: playerControlPanelState.result.playback_state == 'playing'
});
if (musicPlayingNow.id == playerControlPanelState.result.track_id) {
// Do not receive information about song (Information is already there)
break work;
}
let playlistEntryInfo = JSON.parse(await aimpRequest("GetPlaylistEntryInfo", {
"track_id": playerControlPanelState.result.track_id
}));
if (playlistEntryInfo.error) {
// Error in responce
break work;
}
musicPlayingNow = playlistEntryInfo.result;
let musicCover = JSON.parse(await aimpRequest("GetCover", {
"track_id": musicPlayingNow.id
}));
if (!musicCover.error) {
musicPlayingNow.cover = `http://${config.aimpData.address}:${config.aimpData.port}/${musicCover.result.album_cover_uri}`
} else {
musicPlayingNow.cover = config.placeholderData.cover;
}
if (!musicPlayingNow.title) musicPlayingNow.title = "Untitled";
if (!musicPlayingNow.artist) musicPlayingNow.artist = "Unknown artist";
updateWidget({
title: musicPlayingNow.title,
artist: (musicPlayingNow.album) ? `${musicPlayingNow.artist} - ${musicPlayingNow.album}` : musicPlayingNow.artist,
cover: musicPlayingNow.cover
});
} catch (e) {
updateWidget({
is_playing: false
});
}
workRightNow = false;
}, config.updateTimeout);