-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
230 lines (204 loc) · 5.25 KB
/
main.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
"use strict";
import { Clock } from "./clocks/clock.js";
import { CountdownClock } from "./clocks/countdownClock.js";
import { getClock } from "./helpers.js";
function getAuth(password) { return JSON.stringify({ action: "authenticate", protocol: 750, password: password }); }
let ws, main;
let host = "192.168.2.130";
let port = 49404;
let loggedin = false;
let library = [];
let currentPresentationId = -1;
let currentSlideIndex = -1;
let currentPresentation = null;
let showOnlyFirstLine = true;
window.addEventListener("load", _ => {
Swal.fire({
title: 'Authentication',
text: "Please provide the password for ProPresenter 7",
input: 'password',
inputValue: "pizza",
inputAttributes: {
autocapitalize: 'off',
autocomplete: 'off',
placeholder: "pizza",
},
showCancelButton: true,
confirmButtonText: 'Sign in',
showLoaderOnConfirm: true,
preConfirm: (passwd) => {
return new Promise((resolve, reject) => {
ws = new WebSocket(`ws://${host}:${port}/remote`);
ws.onerror = e => {
console.log("event")
reject(e);
};
ws.onopen = _ => {
ws.send(getAuth(passwd));
};
ws.onmessage = event => {
resolve(event.data);
};
}).catch(e => {
console.error(e);
Swal.fire({
title: 'Couldn\'t connect!',
html: `The url was ws://${host}:${port}<br><br>Is the IP/port correct?<br>If it is, chech that PP7 is running. Otherwise, change the configuration, and retry.`,
icon: 'error',
showConfirmButton: true,
confirmButtonText: "Retry",
}).then(_ => {
location.reload();
});
});
},
allowOutsideClick: () => !Swal.isLoading()
}).then((data) => {
let msg = JSON.parse(data.value);
if (msg.authenticated) {
Swal.fire({
title: 'Logged in!',
icon: 'success',
showConfirmButton: false,
timer: 1500
});
loggedin = true;
loadInfo();
} else {
Swal.fire({
title: 'Unkown exception!',
text: msg.error,
icon: 'error',
showConfirmButton: true,
confirmButtonText: "Retry",
}).then(_ => {
location.reload();
});
}
})
});
function loadInfo() {
ws.onmessage = event => {
let msg = JSON.parse(event.data);
switch (msg.action) {
case 'presentationCurrent':
handlePresentationCurrent(msg);
break;
case 'libraryRequest':
handleLibrary(msg);
break;
case 'clearText':
break;
case 'presentationTriggerIndex':
handlePresentationTriggerIndex(msg);
break;
case 'clockRequest':
handleClockInfo(msg);
break;
default:
console.warn("unhandled " + msg.action)
console.log(msg)
break;
}
};
ws.send('{"action":"clockRequest"}');
ws.send('{"action":"libraryRequest"}');
setTimeout(_ => {
getCurrentPresentation();
}, 1000)
}
function handlePresentationCurrent(msg) {
currentPresentation = msg.presentation;
updateCurrentSlide();
}
function updateCurrentSlide() {
let img = document.getElementById("slide");
let slidesUL = document.getElementById("currentSlides");
let text = "";
let counter = 0;
currentPresentation.presentationSlideGroups.forEach(group => {
text += `<li style="background-color: ${doRGBA(group.groupColor)};">${group.groupName}`;
console.log(group)
text += `<ul>`;
group.groupSlides.forEach(slide => {
if (slide.slideText != "" && slide.slideText != " ") {
if (counter == currentSlideIndex) {
text += `<li class="active"> ${doText(slide.slideText)}</li>`;
} else {
text += `<li>${doText(slide.slideText)}</li>`;
}
}
if (counter == currentSlideIndex) {
img.src = "data:image/png;base64," + slide.slideImage;
}
counter++;
});
text += `</ul>`;
text += `</li>`;
});
slidesUL.innerHTML = text;
}
function handleLibrary(msg) {
library = msg.library;
let lib = document.getElementById("libraryList");
library.forEach(e => {
let a = e.split('/');
a = a[a.length - 1];
a = a.substr(0, a.length - 4);
if (lib) lib.innerHTML += `<li>${a}</li>`;
});
}
function handlePresentationTriggerIndex(msg) {
if (msg.presentationPath != currentPresentationId) {
getCurrentPresentation();
currentPresentationId = msg.presentationPath;
}
currentSlideIndex = msg.slideIndex;
updateCurrentSlide();
}
function handleClockInfo(msg) {
msg.clockInfo.forEach(clock => {
if (clock.clockName == "Pregação") {
handleClockPregacao(clock)
}
});
return
}
function handleClockPregacao(cl) {
let clp = getClock(cl)
console.log(clp)
let cpNode = document.getElementById("clockPregacao");
cpNode.getElementsByClassName("finishTime")[0].textContent = clp.clockEnd.toLocaleTimeString();
setInterval(() => {
cpNode.getElementsByClassName("remaningTime")[0].textContent = clp.getTimeRemaining();
}, 9);
}
function getCurrentPresentation() {
ws.send('{ "action":"presentationCurrent", "presentationSlideQuality": 100}');
}
/**
*
* @param {string} colorString a proporesenter color string (e.g. 0.23 0.74 0.44 1)
* @returns {string} a css rgba() string
*/
function doRGBA(colorString) {
let colors = colorString.split(" ");
let ret = "rgba(";
let i = 0;
colors.forEach(c => {
c = c.replace(",", ".")
c *= 100;
if (i++ < 3)
ret += c + "%, ";
else
ret += c
});
return ret + ")";
}
function doText(t) {
t = t.replace(/(\r\n|\n|\r)/gm, "<br />");
if (showOnlyFirstLine) {
t = t.split("\r\n\r\n")[0];
}
return t;
}