-
Notifications
You must be signed in to change notification settings - Fork 2
/
launcher.js
582 lines (581 loc) · 29.3 KB
/
launcher.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
const axios = require('axios').default,
net = require("net"),
remote = require("@electron/remote"),
fs = require("graceful-fs"),
os = require("os"),
path = require("path"),
powerControl = require("power-control"),
{shell} = require("electron"),
$ = require("jquery");
const appPath = remote.app.getPath("userData"),
// green pink blue deeporange purple yellow cyan brown
colors = ["#00e676", "#ff80ab", "#64b5f6", "#ffb74d", "#ea80fc", "#ffff8d", "#18ffff", "#bcaaa4"],
prefsFile = path.join(appPath, "prefs.json");
var scheduledActionInfo = {},
broadcastStrings = {},
prefs = {};
axios.defaults.adapter = "http";
function checkInternet(online) {
if (online) {
$("#overlayInternetCheck").fadeIn("fast", () => {
$("#overlayInternetFail").stop().hide();
});
require("electron").ipcRenderer.send("autoUpdate");
} else {
$("#overlayInternetFail").fadeIn("fast", () => {
$("#overlayInternetCheck").stop().hide();
});
setTimeout(updateOnlineStatus, 1000);
}
}
const updateOnlineStatus = async () => {
checkInternet((await isReachable("www.jw.org", 443)));
};
updateOnlineStatus();
require("electron").ipcRenderer.on("hideThenShow", (event, message) => {
$("#overlay" + message[1]).fadeIn(400, () => {
$("#overlay" + message[0]).hide();
});
});
require("electron").ipcRenderer.on("updateDownloadProgress", (event, message) => {
var dotsDone = Math.floor(parseFloat(message[0]) / 10);
$("#updatePercent i:nth-of-type(" + dotsDone + ")").addClass("fa-circle text-primary").removeClass("fa-dot-circle");
});
require("electron").ipcRenderer.on("macUpdate", () => {
$("#btn-mac-update").click(function() {
shell.openExternal("https://github.com/sircharlo/jw-launcher/releases/latest");
}).parent().fadeIn();
});
require("electron").ipcRenderer.on("goAhead", () => {
$("#overlayPleaseWait").fadeIn(400, () => {
$("#overlayUpdateCheck").hide();
goAhead();
});
});
$(".links tbody").on("change", "input, select", function() {
$(".links tbody input:visible, .links tbody select:visible").removeClass("is-invalid").filter(function() {
return !this.value;
}).addClass("is-invalid");
$(this).closest("tr").find(".btn-delete-link").toggle($(this).closest("tr").find(".linkType").val() !== "");
updateScheduleTargets();
});
function goAhead() {
languageRefresh().then(function() {
if (fs.existsSync(prefsFile)) {
try {
prefs = JSON.parse(fs.readFileSync(prefsFile));
updateCleanup();
} catch (err) {
console.error(err, prefs);
toggleScreen("overlaySettings", true);
}
}
prefsInitialize();
processSettings();
$("#version span.badge").html("v" + remote.app.getVersion());
$("#overlayPleaseWait").fadeOut();
window.addEventListener("keyup", handleKeyPress, true);
scheduleLoader();
});
}
function isEscapeButton(event) {
return event.key === "Escape" || event.key === "Esc"
}
function supportKey(event) {
return !(event.ctrlKey || event.metaKey || event.shiftKey) && (event.code.includes("Key") || isEscapeButton(event));
}
function handleKeyPress (event) {
if (!supportKey(event) || $("#overlayPleaseWait").is(":visible")) {
return;
}
if ($("#home").is(":visible")) {
if (event.key.toLowerCase() == String.fromCharCode($(".links tbody tr").filter(function() {
return $(this).find(".linkName").val() !== "";
}).length + 65).toLowerCase()) {
$("#broadcast1button").click();
} else {
$(".actions .buttonContainer button").eq(event.key.toLowerCase().charCodeAt(0) - 97).click();
}
} else if ($("#closeButton").is(":visible") && (event.key.toLowerCase() == "x" || isEscapeButton(event))) {
$("#closeButton").click();
} else if ($(".go-home").is(":visible") && isEscapeButton(event)) {
$("#btnGoHome").click();
} else if ($(".featuredVideos").is(":visible") && !$("#closeButton").is(":visible")) {
$(".featuredVideos > div > div").eq(event.key.toLowerCase().charCodeAt(0) - 97).click();
} else if ($(".streamingVideos").is(":visible") && $("#closeButton").is(":not(:visible)")) {
$(".streamingVideos > div > div").eq(event.key.toLowerCase().charCodeAt(0) - 97).click();
}
}
function isReachable(hostname, port) {
return new Promise(resolve => {
try {
let client = net.createConnection(port, hostname);
client.setTimeout(5000);
client.on("timeout", () => {
client.destroy("Timeout: " + hostname + ":" + port);
});
client.on("connect", function() {
client.destroy();
resolve(true);
});
client.on("error", function(e) {
console.error(e);
resolve(false);
});
} catch(err) {
resolve(false);
}
});
}
$("#overlaySettings tbody").on("click", ".btn-delete", function() {
let parentTable = $(this).closest("tbody");
$(this).closest("tr").remove();
parentTable.find("tr").last().find("input").first().change();
});
$(".links tbody").on("change", ".streamUrl", function() {
$(this).val($(this).val().replace("https://fle.stream.jw.org/", ""));
});
$(".links tbody").on("change", ".linkName, .linkDetails input", function() {
let linkArray = [];
$(".links tbody tr").each(function () {
if ($(this).find(".linkType").val().length > 0) {
linkArray.push($(this).find(".linkType, .linkName, .linkDetails input").map(function () { return $(this).val(); }).get());
}
});
if ($(".links tbody tr .is-invalid:visible").length ===0) $("#linkArray").val(JSON.stringify(linkArray)).change();
});
$(".links tbody").on("change", ".linkType", function() {
let linkType = $(this).val();
let thisRow = $(this).closest("tr");
thisRow.find(".linkDetails").empty();
thisRow.find(".linkName").val("").toggle(!!linkType);
if (linkType === "zoom") {
thisRow.find(".linkDetails").append("<div class='input-group input-group-sm'><span class='input-group-text'>ID</span><input type='text' class='form-control form-control-sm zoomId dynamic-field' placeholder='Zoom meeting ID' /><span class='input-group-text'>Password</span><input type='text' class='form-control form-control-sm zoomPassword dynamic-field' placeholder='Zoom meeting password' /></div>");
thisRow.find(".zoomId").inputmask(["999 999 999[9]", "999 9999 9999"], { "clearIncomplete": true });
} else if (linkType === "stream") {
thisRow.find(".linkDetails").append("<div class='input-group input-group-sm'><span class='input-group-text'>https://fle.stream.jw.org/</span><input type='text' class='form-control form-control-sm streamUrl dynamic-field' placeholder='t/ABCDEFGHIJKLOMNOPQRSTUVWXYZ' /></div>");
}
thisRow.find("input").addClass("is-invalid");
if (!$(this).hasClass("initializing")) validateSettings();
});
$(".schedule tbody").on("change", "input, select", function() {
$(".schedule tbody input, .schedule tbody select").removeClass("is-invalid").filter(function() {
return !this.value;
}).addClass("is-invalid");
let scheduleArray = [];
$(".schedule tbody tr").each(function () {
scheduleArray.push($(this).find("input, select").map(function () { return $(this).val(); }).get());
});
if ($(".schedule tbody tr .is-invalid:visible").length ===0) {
$("#scheduleArray").val(JSON.stringify(scheduleArray)).change();
} else {
validateSettings();
}
});
async function languageRefresh() {
let availMedia = (await getJson("https://b.jw-cdn.org/apis/mediator/v1/categories/E/StudioFeatured")).category.media.concat((await getJson("https://b.jw-cdn.org/apis/mediator/v1/categories/E/LatestVideos")).category.media).map(item => item.availableLanguages);
let availLangs = [...new Set([].concat(...availMedia))];
for (var lang of Object.values((await getJson("https://b.jw-cdn.org/apis/mediator/v1/languages/E/all?clientType=www")).languages).filter(value => availLangs.includes(value.code))) {
$("#broadcastLang").append($("<option>", {
value: lang.code,
text: lang.vernacular + " (" + lang.name + ")"
}));
}
$("#broadcastLang").select2();
}
function addNewLink() {
$(".links tbody").append("<tr draggable='true'><td><select class='form-select form-select-sm linkType dynamic-field'><option value='' hidden>Select a type</option><option value='zoom'>Zoom</option><option value='stream'>JW Stream</option></select></td><td><input type='text' class='form-control form-control-sm linkName dynamic-field' style='display: none;' placeholder='Enter a meaningful description' /></td><td><div class='linkDetails input-group'></div></td><td class='text-end'><button type='button' class='btn btn-light btn-sm btn-sort-schedule me-2 text-dark'><i class='fas fa-sort'></i></button><button type='button' class='btn btn-danger btn-sm btn-delete btn-delete-link' style='display: none;'><i class='fas fa-minus'></i></button></td></tr>");
$(".links tbody tr").last().find(".linkType").addClass("is-invalid");
}
function addNewSchedule() {
$(".schedule tbody").append("<tr draggable='true'><td><select class='form-select form-select-sm triggerDay dynamic-field is-invalid'><option value='' hidden></option><option value='1'>Monday</option><option value='2'>Tuesday</option><option value='3'>Wednesday</option><option value='4'>Thursday</option><option value='5'>Friday</option><option value='6'>Saturday</option><option value='0'>Sunday</option></select></td><td><input type='text' class='form-control form-control-sm triggerTime dynamic-field is-invalid' /></td><td><select class='form-select form-select-sm targetAction dynamic-field is-invalid'><option value='' hidden></option></select></td><td class='text-end'><button type='button' class='btn btn-light btn-sm btn-sort-schedule me-2 text-dark'><i class='fas fa-sort'></i></button><button type='button' class='btn btn-danger btn-sm btn-delete btn-delete-schedule'><i class='fas fa-minus'></i></button></td></tr>");
$(".schedule tbody tr").last().find(".triggerTime").inputmask({ regex: "[0-2]\\d:[0-5]\\d", "clearIncomplete": true });
updateScheduleTargets();
}
function updateScheduleTargets() {
$(".schedule select.targetAction").each(function() {
let sel = $(this);
let links = $(".links tbody tr").filter(function() {
return $(this).find(".linkName").val() !== "";
});
sel.find("option").slice(links.length + 1).remove();
sel.change();
links.each(function(index) {
if (sel.find("option[value=" + index + "]").length === 0) sel.append($("<option>", {
value: index
}));
sel.find("option[value=" + index + "]").text($(this).find(".linkName").val() + " - " + $(this).find(".linkType option:selected").text());
});
});
}
async function broadcastLoad() {
var videos = 0;
if (prefs.broadcastLang) {
let req = await getJson("https://b.jw-cdn.org/apis/mediator/v1/translations/" + prefs.broadcastLang);
broadcastStrings = req.translations[prefs.broadcastLang];
$("#broadcast1button").css("background-color", colors[($(".links tbody tr").filter(function() {
return $(this).find(".linkName").val() !== "";
}).length % colors.length)]).html("<div><kbd>" + String.fromCharCode($(".links tbody tr").filter(function() {
return $(this).find(".linkName").val() !== "";
}).length + 65) + "</kbd></div><div class='align-items-center flex-fill' style='display: flex;'>" + broadcastStrings.ttlHome + "</div>");
$("#lblGoHome").html(broadcastStrings.btnStillWatchingGoBack);
try {
var studioFeatured = (await getJson("https://b.jw-cdn.org/apis/mediator/v1/categories/" + prefs.broadcastLang + "/StudioFeatured?detailed=0&clientType=www")).category.media;
var latestVideos = (await getJson("https://b.jw-cdn.org/apis/mediator/v1/categories/" + prefs.broadcastLang + "/LatestVideos?detailed=0&clientType=www")).category.media;
let allVideos = studioFeatured.concat(latestVideos.filter(item => !studioFeatured.includes(item))).slice(0, 16);
allVideos = [...new Map(allVideos.map(item => [item["guid"], item])).values()];
$(".featuredVideos > div:not(:first)").remove();
for (var featuredVideo of allVideos) {
videos++;
var featuredVideoElement = $("<div class='mt-0 pt-2'><div class='bg-light flex-column h-100 rounded text-dark' data-url='" + featuredVideo.files.slice(-1)[0].progressiveDownloadURL + "' style='display: flex;'><div class='row'><img style='width: 100%' src='" + featuredVideo.images.pnr.lg + "'/></div><div class='flex-column flex-fill m-2' style='display: flex;'><div><h5 class='kbd'><kbd>" + String.fromCharCode(65 + videos) + "</kbd></h5></div><div class='align-items-center flex-fill flex-row' style='display: flex;'><h5>" + featuredVideo.title + "</h5></div></div></div></div>").click(function() {
$("#videoPlayer").append("<video controls autoplay></video>").fadeIn();
$("#videoPlayer video").append("<source src='" + $(this).find("div").data("url") + "' / >");
});
$(".featuredVideos").append(featuredVideoElement);
}
$(".featuredVideos > div").css("height", 100 / Math.ceil($(".featuredVideos > div").length / 5) + "%");
} catch(err) {
console.error("[ERROR] https://b.jw-cdn.org/apis/mediator/v1/categories/" + prefs.broadcastLang + "/StudioFeatured?detailed=0&clientType=www");
}
$("#broadcast1button").parent().toggle(videos > 0);
}
return videos;
}
function buttonHeight(broadcastVideos) {
let links = $(".links tbody tr").filter(function() {
return $(this).find(".linkName").val() !== "";
}).length,
broadcast = (broadcastVideos > 0 ? 1 : 0);
$(".actions > div").css("height", 100 / Math.ceil((broadcast + links) / 3) + "%");
}
async function downloadFile(url, progressElem) {
try {
let response = await axios.get(url, {
adapter: require("axios/lib/adapters/xhr"),
responseType: "arraybuffer",
onDownloadProgress: function(progressEvent) {
var percent = progressEvent.loaded / progressEvent.total * 100;
progressElem.css("width", percent + "%");
}
});
return response.data;
} catch (err) {
console.error(err);
return err;
}
}
function generateButtons() {
$(".actions .buttonContainer").remove();
let links = $(".links tbody tr").filter(function() {
return $(this).find(".linkName").val() !== "";
});
for (let link = 0; link <links.length; link++) {
$(".actions").append("<div class='buttonContainer pt-2 flex-grow-1'><button type='button' class='align-items-center btn btn-lg btn-" + $(links[link]).find(".linkType").val() + " flex-column h-100 w-100' style='display: flex; background-color: " + colors[(link % colors.length)] + "' data-link-details='" + $(links[link]).find(".linkDetails input").map(function () { return $(this).val(); }).get() + "'><div><kbd>" + String.fromCharCode(link + 65) + "</kbd></div><div class='align-items-center flex-fill' style='display: flex;'>" + $(links[link]).find(".linkName").val() + "</div></button></div>");
}
$(".actions").append($(".actions > .broadcastContainer"));
}
async function getJson(url) {
let response = null,
payload = null;
try {
payload = await axios.get(url);
response = payload.data;
} catch (err) {
console.error(url, err, payload);
}
return response;
}
function prefsInitialize() {
for (var pref of ["linkArray", "scheduleArray", "labelShutdown", "labelRemoteAssistance", "labelSettings", "autoRunAtBoot", "enableShutdown", "enableRemoteAssistance", "username"]) {
if (!(Object.keys(prefs).includes(pref)) || !prefs[pref]) {
prefs[pref] = null;
}
if ($("#" + pref)[0].type == "text" || $("#" + pref)[0].type == "select-one" || $("#" + pref)[0].type == "range" || $("#" + pref)[0].type == "hidden") {
$("#" + pref).val(prefs[pref]);
} else if ($("#" + pref)[0].type == "checkbox") {
$("#" + pref).prop("checked", prefs[pref]);
}
}
$("#broadcastLang").val(prefs.broadcastLang ? prefs.broadcastLang : "").select2();
if (prefs.linkArray && JSON.parse(prefs.linkArray).length > 0) {
for (let link of JSON.parse(prefs.linkArray)) {
addNewLink();
$(".links tbody tr").last().find(".linkType").addClass("initializing").val(link[0]).change().removeClass("initializing is-invalid");
link.shift();
for (let linkPart = 0; linkPart < link.length; linkPart++) {
$(".links tbody tr").last().find("input").eq(linkPart).val(link[linkPart]).removeClass("is-invalid");
}
}
$(".links tbody tr").last().find(".linkName").change();
}
if (prefs.scheduleArray && JSON.parse(prefs.scheduleArray).length > 0) {
for (let schedule of JSON.parse(prefs.scheduleArray)) {
addNewSchedule();
for (let schedulePart = 0; schedulePart < schedule.length; schedulePart++) {
$(".schedule tbody tr").last().find("input, select").eq(schedulePart).val(schedule[schedulePart]).removeClass("is-invalid");
}
}
$(".schedule tbody tr").last().find("input").first().change();
}
processSettings();
}
function processSettings() {
for (var label of ["Settings", "Shutdown", "RemoteAssistance"]) {
$("#lbl" + label).html(prefs["label" + label]);
}
for (var enableMe of ["Shutdown", "RemoteAssistance"]) {
$("#btn" + enableMe).parent().toggle(!!prefs["enable" + enableMe]);
}
remote.app.setLoginItemSettings({
openAtLogin: prefs.autoRunAtBoot
});
broadcastLoad().then(function(broadcastVideos) {
for (var setting of ["broadcastLang", "username"]) {
$("#" + setting).toggleClass("is-invalid", !prefs[setting]).next("span.select2").toggleClass("is-invalid", !prefs[setting]);
}
validateSettings();
buttonHeight(broadcastVideos);
});
}
function scheduleLoader() {
let d = new Date();
let schedules = [];
$(".schedule tbody tr").filter(function() {
return $(this).find(".is-invalid").length === 0;
}).each(function () {
let scheduledItem = {};
$(this).find("select, input").each(function () {
scheduledItem[Array.from($(this).prop("classList")).filter(str => new RegExp("trigger|target", "g").test(str)).join("")] = $(this).val();
});
schedules.push(scheduledItem);
});
if (schedules.length > 0 && (!scheduledActionInfo.lastExecution || (d - scheduledActionInfo.lastExecution) / 1000 / 60 / 60 > 12) && (!scheduledActionInfo.lastSkip || (d - scheduledActionInfo.lastSkip) / 1000 / 60 / 60 > 12)) {
if ($("#home:visible").length > 0) {
for (var s = 0; s < schedules.length; s++) {
let schedule = schedules[s];
if (d.getDay() == schedule.triggerDay) {
let triggerTime = schedule.triggerTime.split(":");
let targetDate = new Date(d);
targetDate.setHours(triggerTime[0], triggerTime[1], 0, 0);
let timeToEvent = (targetDate - d) / 1000 / 60;
if (timeToEvent >=-105 && timeToEvent <= 30) {
console.log("[SCHEDULE] Triggering scheduled item, as we are within the acceptable timeframe");
$("#actionDesc").text($(".schedule tbody tr .targetAction").eq(s).find("option:selected").text());
$("#overlayScheduledAction").stop().fadeIn().delay(10000).fadeOut(400, function() {
if (!scheduledActionInfo.lastSkip || (d - scheduledActionInfo.lastSkip) / 1000 / 60 / 60 > 12) $(".actions button").eq(schedule.targetAction).click();
});
let timeLeft = 10;
let downloadTimer = setInterval(function(){
if(timeLeft <= 0) clearInterval(downloadTimer);
$("#scheduledDelayProgress .progress-bar").css("width", (10 - timeLeft + 1) * 10 + "%");
timeLeft -= 1;
}, 1000);
}
}
}
}
}
setTimeout(scheduleLoader, 15000);
}
function toggleScreen(screen, forceShow, forceHide) {
var visible = $("#" + screen).is(":visible");
if (forceShow) {
$("#" + screen).fadeIn("fast");
$("#home" + (screen !== "overlayPleaseWait" ? ", #overlayPleaseWait" : "")).hide();
} else if (forceHide || visible) {
$("#home").show();
$("#" + screen).fadeOut("fast");
} else {
$("#" + screen).fadeIn("fast");
$("#home" + (screen !== "overlayPleaseWait" ? ", #overlayPleaseWait" : "")).hide();
}
}
function updateCleanup() {
try {
for (var oldHidePref of ["Shutdown", "RemoteAssistance"]) {
if ("hide" + oldHidePref in prefs) {
prefs["enable" + oldHidePref] = !prefs["hide" + oldHidePref];
delete prefs["hide" + oldHidePref];
}
}
fs.writeFileSync(prefsFile, JSON.stringify(Object.keys(prefs).sort().reduce((acc, key) => ({...acc, [key]: prefs[key]}), {}), null, 2));
} catch (err) {
console.error(err);
}
}
function validateSettings() {
let configIsValid = $("#overlaySettings .is-invalid").length === 0;
$(".btnSettings").prop("disabled", !configIsValid).toggleClass("btn-danger", !configIsValid).toggleClass("btn-secondary", configIsValid);
$("#settingsIcon").toggleClass("text-danger", !configIsValid).toggleClass("text-muted", configIsValid);
if (configIsValid) generateButtons();
if (!configIsValid) toggleScreen("overlaySettings", true);
return configIsValid;
}
$(".btnSettings, #btnSettings").on("click", function() {
toggleScreen("overlaySettings");
});
$("#overlayScheduledAction button").on("click", function() {
$("#overlayScheduledAction").stop().fadeOut;
scheduledActionInfo.lastSkip = new Date();
});
$("#overlaySettings input:not(.dynamic-field), #overlaySettings select:not(.dynamic-field)").on("change", function() {
if ($(this).prop("tagName") == "INPUT") {
if ($(this).prop("type") == "checkbox") {
prefs[$(this).prop("id")] = $(this).prop("checked");
} else if ($(this).prop("type") == "radio") {
prefs[$(this).closest("div").prop("id")] = $(this).closest("div").find("input:checked").val();
} else if ($(this).prop("type") == "text" || $(this).prop("type") == "password" || $(this).prop("type") == "range" || $(this).prop("type") == "hidden") {
prefs[$(this).prop("id")] = $(this).val();
}
} else if ($(this).prop("tagName") == "SELECT") {
prefs[$(this).prop("id")] = $(this).find("option:selected").val();
}
fs.writeFileSync(prefsFile, JSON.stringify(Object.keys(prefs).sort().reduce((acc, key) => ({...acc, [key]: prefs[key]}), {}), null, 2));
processSettings();
});
$("#autoRunAtBoot").on("change", function() {
remote.app.setLoginItemSettings({
openAtLogin: prefs.autoRunAtBoot
});
});
$("#broadcastLang").on("change", function() {
$(".featuredVideos > div:not(:first-of-type)").remove();
});
$("#btnShutdown").on("click", function() {
powerControl.powerOff();
});
$(".btn-add-link").on("click", function() {
addNewLink();
});
$(".btn-add-schedule").on("click", function() {
addNewSchedule();
validateSettings();
});
$("#btnExport").on("click", function() {
fs.writeFileSync(remote.dialog.showSaveDialogSync({
defaultPath : "prefs.json"
}), JSON.stringify(prefs, null, 2));
});
$("#closeButton").on("click", function() {
$("#videoPlayer").fadeOut().find("video").remove();
});
$("#broadcast1button").on("click", function() {
$("#videos>div").show();
$(".streamingVideos").first().parent().hide();
$("#videoPlayer").hide();
toggleScreen("videos");
});
$("#btnGoHome, #btnGoHome2").on("click", function() {
toggleScreen("videos");
});
$(".streamingVideos").on("click", ".flex-column:not(.lblGoHome2)", function() {
$("#videoPlayer").append("<video controls autoplay><source src='" + $(this).data("url") + "' / ></video>").fadeIn();
});
$(".actions").on("click", ".btn-zoom", function () {
let linkDetails = $(this).data("link-details").split(",");
shell.openExternal("zoommtg://zoom.us/join?confno=" + linkDetails[0].replace(/\D+/g, "") + "&pwd=" + linkDetails[1] + "&uname=" + prefs.username);
scheduledActionInfo.lastExecution = new Date();
let timeLeft = 15;
let loadZoomTimer = setInterval(function(){
$("#loadingProgress .progress-bar").css("width", (15 - timeLeft + 1) * 100 / 15 + "%").closest("div.align-self-center").show();
if(timeLeft <= 0) {
clearInterval(loadZoomTimer);
$("#loadingProgress .progress-bar").css("width", "0%").closest("div.align-self-center").hide();
}
timeLeft -= 1;
}, 1000);
$("#overlayPleaseWait").fadeIn().delay(15000).fadeOut();
});
$(".actions").on("click", ".btn-stream", async function () {
toggleScreen("overlayPleaseWait");
try {
$("#videos>div").hide();
$(".streamingVideos").first().parent().show();
$(".streamingVideos > div:not(:first)").remove();
let tempHeaders = { "x-requested-with": "XMLHttpRequest" };
let streamLangs = (await axios.get("https://fle.stream.jw.org/language/getlanguages", { headers: tempHeaders })).data.languages;
tempHeaders.cookie = (await axios.post("https://fle.stream.jw.org/token/check", JSON.stringify({ token: decodeURIComponent($(this).data("link-details").split(",")[0]).split("/").slice(-1)[0] }), { headers: tempHeaders })).headers["set-cookie"].join("; ");
var streamLang = (await axios.get("https://fle.stream.jw.org/member/getinfo", { headers: tempHeaders })).data.data.language;
var langSymbol = streamLangs.filter(lang => lang.locale == streamLang)[0].symbol;
$("#lblGoHome2").html((await getJson("https://prod-assets.stream.jw.org/translations/" + langSymbol + ".json")).translations[langSymbol]["button_previous"]);
let streamFiles = Object.entries((await axios.post("https://fle.stream.jw.org/event/languageVideos", JSON.stringify({ language: streamLangs.filter(lang => lang.locale == streamLang)[0] }), { headers: tempHeaders })).data);
for (var streamFile of streamFiles) {
$("#loadingProgress .progress-bar").css("width", (parseInt(streamFile[0]) + 1) * 100 / streamFiles.length + "%").closest("div.align-self-center").show();
var mediaFile = await axios.head(streamFile[1].vod_firstfile_url, { headers: { Cookie: tempHeaders.cookie } });
$(".streamingVideos").append("<div class='mt-0 pt-2'><div class='flex-column flex-fill h-100 rounded bg-light p-2 text-dark' data-url='" + mediaFile.request.protocol + "//" + mediaFile.request.host + mediaFile.request.path + "' style='display: flex;'><div class='flex-row'><h5><kbd>" + String.fromCharCode(parseInt(streamFile[0]) + 66) + "</kbd></h5></div><div class='align-items-center flex-fill flex-row' style='display: flex;'><h5>" + streamFile[1].description + "</h5></div></div>");
}
$("#loadingProgress .progress-bar").css("width", "0%").closest("div.align-self-center").hide();
$(".streamingVideos > div").css("height", 100 / Math.ceil($(".streamingVideos > div").length / 4) + "%");
scheduledActionInfo.lastExecution = new Date();
toggleScreen("videos");
} catch(err) {
let badLink = $(this).data("link-details");
$(".streamUrl").filter(function() { return $(this).val() === badLink; }).addClass("is-invalid");
toggleScreen("overlaySettings", true);
console.error(err);
}
});
$("#btnRemoteAssistance").on("click", async function() {
$("#loadingProgress .progress-bar").closest("div.align-self-center").show();
$("#overlayPleaseWait").fadeIn();
var qsUrl = "https://download.teamviewer.com/download/TeamViewerQS.exe";
if (os.platform() == "darwin") {
qsUrl = "https://download.teamviewer.com/download/TeamViewerQS.dmg";
} else if (os.platform() == "linux") {
qsUrl = "https://download.teamviewer.com/download/version_11x/teamviewer_qs.tar.gz";
}
var initialTriggerText = $(this).html();
$(this).prop("disabled", true);
var qs = await downloadFile(qsUrl, $("#loadingProgress .progress-bar"));
$("#overlayPleaseWait").delay(15000).fadeOut(400, function() {
$("#loadingProgress .progress-bar").closest("div.align-self-center").hide();
});
var qsFilename = path.basename(qsUrl);
fs.writeFileSync(path.join(appPath, qsFilename), new Buffer(qs));
shell.openExternal(path.join(appPath, qsFilename));
$(this).html(initialTriggerText).prop("disabled", false);
});
$("#overlaySettings tr.onOffToggle").on("click", function() {
$(this).find("input.optional-action-enabled").click();
});
$("#overlaySettings tr.onOffToggle input.optional-action-enabled").click(function(e) {
e.stopPropagation();
});
$(document).on("select2:open", () => {
document.querySelector(".select2-search__field").focus();
});
$(".btn-sort-schedules").on("click", function() {
const getCellValue = (tr, idx) => {
let val = $(tr).find("td").eq(idx).find("input, select").val();
return (val == 0 ? val + 7 : val);
};
const comparer = (idx, asc) => (a, b) => ((v1, v2) =>
v1 !== "" && v2 !== "" && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
)(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
const table = $(".schedule tbody").get(0);
Array.from(table.querySelectorAll("tr")).sort(comparer(1, true)).sort(comparer(0, true)).forEach(tr => table.appendChild(tr));
$(".schedule tbody input, .schedule tbody schedule").last().change();
});
let eventSrcElem, sourceRow;
$(".links tbody, .schedule tbody").on("dragstart", "tr", function() {
eventSrcElem = $(event.target).closest("tbody").get(0);
sourceRow = $(event.target).closest("tr");
if ($(event.target).closest("tr").find(".btn-delete:visible").length === 0) return false;
}).on("dragover", "tr", function(){
event.preventDefault();
try {
if (eventSrcElem === $(event.target).closest("tbody").get(0)) {
let targetRow = $(event.target).closest("tr").get(0);
let targetTableChildren = Array.from($(event.target).closest("tbody").get(0).children);
if (targetTableChildren.indexOf(targetRow) > targetTableChildren.indexOf(sourceRow.get(0))) {
targetRow.after(sourceRow.get(0));
} else if (targetTableChildren.indexOf(targetRow) < targetTableChildren.indexOf(sourceRow.get(0))) {
targetRow.before(sourceRow.get(0));
}
}
} catch(err) {
console.error(err);
}
}).on("dragend", "tr", function() {
sourceRow.find("input, select").last().change();
});