-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
439 lines (380 loc) · 18.4 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
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
console.log("iskopower - načteno");
let currentUrl = location.href;
let keys = [];
// Prvotní inicializace / reset
function firstInit() {
chrome.storage.sync.get(['keys', 'colorCheckbox', 'discoColor', 'colorPicker', 'picturesCheckbox', 'firstSetup'], function (result) {
if (result.keys === undefined || result.keys.length === 0) {
let defaultKeys = ["ZEMAN", "SBOR", "KOMENSKY"];
chrome.storage.sync.set({keys: defaultKeys}, function () {
console.log('iskopower - Nebyly nalezeny žádné klíče, nastaveny výchozí klíče');
});
}
if (result.firstSetup === undefined || result.firstSetup === false) {
chrome.storage.sync.set({
colorCheckbox: true,
discoColor: true,
colorPicker: "#4caf50",
picturesCheckbox: true,
firstSetup: true
}, function () {
console.log('iskopower - Nebyly nalezeny žádné nastavení, nastaveny výchozí nastavení');
});
}
});
}
firstInit();
// Hlavní funkce
chrome.storage.sync.get(['keys', 'colorCheckbox', 'discoColor', 'colorPicker', 'picturesCheckbox', 'firstSetup'], function (result) {
// Nastavení klíčů, podle těch, které byly uloženy
keys = result.keys;
// Kontrola, jestli je člověk v nastavení
if (currentUrl == "https://is.ghrabuvka.cz/profile") {
setTimeout(() => {
loadSettings();
}, 250);
}
if (currentUrl == "https://is.ghrabuvka.cz/" || currentUrl == "https://is.ghrabuvka.cz/plan")
{
setTimeout(() => {
loadCallendarManager();
}, 300);
}
setInterval(() => {
if (location.href !== currentUrl) {
currentUrl = location.href;
if (currentUrl == "https://is.ghrabuvka.cz/profile") {
setTimeout(() => {
loadSettings();
}, 250);
} else if (currentUrl == "https://is.ghrabuvka.cz/" || currentUrl == "https://is.ghrabuvka.cz/plan")
{
setTimeout(() => {
loadCallendarManager();
}, 300);
}
}
}, 100);
// Načtení nastavení
function loadSettings() {
// Přidání nastavení do profilu uživatele
const moduleProfile = document.querySelector('#module_profile');
const settingsDiv = document.createElement('div');
settingsDiv.innerHTML = `<div id="iskopower-settings">
<h1 class="varColor">Nastavení doplňku iskopower <span class="material-icons" title="Tento doplněk je produktem třetí strany a nemá nic společného se samotným informačním systémem. Některá nastavení potřebují obnovení stránky pro aplikování.">info</span></h1>
<div class="left">
<div class="form-group"><label for="colorCheckbox">Povolit barvy</label><input type="checkbox" name="colorCheckbox" id="colorCheckbox"></div>
<div class="form-group"><label for="discoColor">Disco mód</label><input type="checkbox" name="discoColor" id="discoColor"></div>
<div class="form-group"><label for="colorPicker">Výběr barvy</label><input type="color" name="colorPicker" id="colorPicker"></div>
<div class="form-group"><label for="picturesCheckbox">Povolit fotky</label><input type="checkbox" name="picturesCheckbox" id="picturesCheckbox"></div>
<div class="form-group"><label for="key">Zadejte klíč</label><input type="text" name="key" id="key"></div>
<div class="form-group"><button id="verifyKey" class="btn varBg">Ověřit klíč</button></div></div>
</div>`;
moduleProfile.appendChild(settingsDiv);
// Nastavení checkboxů
const colorCheckbox = document.getElementById('colorCheckbox');
const discoColor = document.getElementById('discoColor');
const colorPicker = document.getElementById('colorPicker');
const picturesCheckbox = document.getElementById('picturesCheckbox');
const keyInput = document.getElementById('key');
const verifyKey = document.getElementById('verifyKey');
colorCheckbox.checked = result.colorCheckbox;
discoColor.checked = result.discoColor;
colorPicker.value = result.colorPicker;
picturesCheckbox.checked = result.picturesCheckbox;
if (!result.colorCheckbox) {
discoColor.disabled = true;
colorPicker.disabled = true;
}
colorCheckbox.addEventListener('input', (event) => {
if (!event.target.checked) {
discoColor.disabled = true;
colorPicker.disabled = true;
} else {
discoColor.disabled = false;
colorPicker.disabled = false;
}
chrome.storage.sync.set({colorCheckbox: event.target.checked}, function () {
console.log('iskopower - Nastavení povolení barvy bylo uloženo');
});
});
discoColor.addEventListener('input', (event) => {
chrome.storage.sync.set({discoColor: event.target.checked}, function () {
console.log('iskopower - Nastavení disco módu bylo uloženo');
});
});
let lastColorPickerUpdate = 0;
colorPicker.addEventListener('input', (event) => {
if (Date.now() - lastColorPickerUpdate > 100) {
chrome.storage.sync.set({colorPicker: event.target.value}, function () {
console.log('iskopower - Nastavení barvy bylo uloženo');
});
lastColorPickerUpdate = Date.now();
}
});
picturesCheckbox.addEventListener('input', (event) => {
chrome.storage.sync.set({picturesCheckbox: event.target.checked}, function () {
console.log('iskopower - Nastavení povolení fotek bylo uloženo');
});
});
// Ověření klíče
verifyKey.addEventListener('click', (event) => {
axios({
method: 'get',
url: `https://pocketbase.thevelda.eu/api/collections/pictures/records/?key=${keyInput.value}`,
}).then(function (response) {
let data = response.data.items[0]
if (!data) {
alert('Neplatný klíč');
return;
}
let picture = {
name: data.name,
imageSrc: `https://pocketbase.thevelda.eu/api/files/${data.collectionId}/${data.id}/${data.image}`,
style: {
top: data.top,
left: data.left,
height: data.height
},
color: data.color
}
if (result.keys.includes(keyInput.value)) {
alert('Tento klíč jsi již našel.');
return;
}
alert('Klíč si napsal správně!\nNázev obrázku: ' + picture.name);
result.keys.push(keyInput.value);
chrome.storage.sync.set({keys: result.keys}, function () {
});
// Přidávání nalezeného klíče do seznamu
let pictureSettingDiv = document.createElement('div');
pictureSettingDiv.id = key;
pictureSettingDiv.innerHTML = `<div class="imageWrapper"><img src="${picture.imageSrc}" alt="${picture.name}" class="studentImage">
<i class="material-icons">delete</i>
<i class="material-icons" style="top: 30px">info</i>
</div></div></div>`;
selectStudents.appendChild(pictureSettingDiv);
pictureSettingDiv.querySelector('div > i:nth-child(2)').addEventListener('click', (event) => {
if (confirm(`Opravdku chceš odstranit obrázek ${picture.name}, který má klíč: ${picture.key}?`)) {
let index = result.keys.indexOf(key);
if (index > -1) {
result.keys.splice(index, 1);
}
chrome.storage.sync.set({keys: result.keys}, function () {
});
pictureSettingDiv.remove();
}
});
pictureSettingDiv.querySelector('div > i:nth-child(3)').addEventListener('click', (event) => {
alert(`Název: ${picture.name}\nBarva: ${picture.color}\nKlíč: ${picture.key}`);
});
});
});
const settingsPicturesDiv = document.createElement('div');
settingsPicturesDiv.innerHTML = '<div id="iskopower-pictures"><h1 class="varColor">Obrázky doplňku iskopower <span class="material-icons" title="Tento doplněk je produktem třetí strany a nemá nic společného se samotným informačním systémem.">info</span></h1><div id="selectStudents"></div></div>';
moduleProfile.appendChild(settingsPicturesDiv);
const selectStudents = document.querySelector("#iskopower-pictures > div")
// Přidávnaí nalezených klíčů
if (!result.keys) return;
keys.forEach(key => {
axios({
method: 'get',
url: `https://pocketbase.thevelda.eu/api/collections/pictures/records/?key=${key}`,
}).then(function (response) {
let data = response.data.items[0]
if (!data) return false;
let picture = {
name: data.name,
key: data.key,
imageSrc: `https://pocketbase.thevelda.eu/api/files/${data.collectionId}/${data.id}/${data.image}`,
style: {
top: data.top,
left: data.left,
height: data.height
},
color: data.color
}
let pictureSettingDiv = document.createElement('div');
pictureSettingDiv.id = key;
pictureSettingDiv.innerHTML = `<div class="imageWrapper"><img src="${picture.imageSrc}" alt="${picture.name}" class="studentImage">
<i class="material-icons">delete</i>
<i class="material-icons" style="top: 30px">info</i>
</div></div></div>`;
selectStudents.appendChild(pictureSettingDiv);
pictureSettingDiv.querySelector('div > i:nth-child(2)').addEventListener('click', (event) => {
if (confirm(`Opravdku chceš odstranit obrázek ${picture.name}, který má klíč: ${picture.key}?`)) {
let index = result.keys.indexOf(key);
if (index > -1) {
result.keys.splice(index, 1);
}
chrome.storage.sync.set({keys: result.keys}, function () {
});
pictureSettingDiv.remove();
}
});
pictureSettingDiv.querySelector('div > i:nth-child(3)').addEventListener('click', (event) => {
alert(`Název: ${picture.name}\nBarva: ${picture.color}\nKlíč: ${picture.key}`);
});
});
})
}
function loadCallendarManager() {
console.log("iskopower - Načítám správu kalendáře")
let lessons = document.getElementsByClassName("rozvrhFirstRow");
for (let i = 0; i < lessons.length; i++) {
lessons[i].addEventListener("contextmenu", function (event) {
event.preventDefault();
let lessonelement = event.target;
let calendartext = prompt("Napište, co chcete přidat do kalendáře.", ``);
if (calendartext == null || calendartext == "") {
// Zrušeno
} else {
let title = calendartext;
let hour = lessonelement.parentElement.getAttribute("data-hour") - 3;
let dateelement = lessonelement.parentElement.parentElement.parentElement;
let date = dateelement.getAttribute("data-date");
const regex = /^(\d{4})-(\d{2})-(\d{2})$/;
const match = date.match(regex);
let month = parseInt(match[2]);
let year = parseInt(match[1]);
let params = {
title,
date,
hour,
month,
year,
};
let formData = new FormData();
formData.append("js", "1");
formData.append("params", JSON.stringify(params));
fetch("https://is.ghrabuvka.cz/plan/addCalendarEvent", {
method: "POST",
body: formData,
})
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error);
});
}
});
}
}
// Nastavení barvy
if (result.colorCheckbox) {
if (result.discoColor) {
let hsla = 0;
setInterval(() => {
hsla = hsla + 1;
document.querySelector("head > style").innerHTML = `.varColor { color: hsla(${hsla}, 100%, 20%, 1) !important; } .varBg { background: hsla(${hsla}, 100%, 20%, 1) !important; }`
}, 5);
} else {
document.querySelector("head > style").innerHTML = `.varColor { color: ${result.colorPicker} !important; } .varBg { background: ${result.colorPicker} !important; }`
}
}
if (result.picturesCheckbox) {
// Výběr náhodného klíče
const key = keys[Math.floor(Math.random() * keys.length)];
// Nastavování obrázku
axios({
method: 'get',
url: `https://pocketbase.thevelda.eu/api/collections/pictures/records/?key=${key}`,
}).then(function (response) {
let data = response.data.items[0]
if (!data) return false;
let picture = {
name: data.name,
imageSrc: `https://pocketbase.thevelda.eu/api/files/${data.collectionId}/${data.id}/${data.image}`, //TODO: Zrušit hardcoded db/api xd
style: {
top: data.top,
left: data.left,
height: data.height
},
color: data.color
}
let img = document.querySelector("#studentImg");
img.src = picture.imageSrc
img.style.top = "calc(var(--top-slider) * 1px)";
img.style.height = "calc(var(--height-slider) * 1px)";
img.style.left = "calc(var(--left-slider) * 1px)";
document.documentElement.style.setProperty('--top-slider', picture.style.top);
document.documentElement.style.setProperty('--height-slider', picture.style.height);
document.documentElement.style.setProperty('--left-slider', picture.style.left);
if (!result.colorCheckbox) {
document.querySelector("head > style").innerHTML = `.varColor { color: ${picture.color} !important; } .varBg { background: ${picture.color} !important; }`
}
});
}
});
// Funkce pro debugování
function debugMode() {
// Pomocný element k umístění
const helpElement = document.getElementById('studentFeedback');
// Vytvoření elementu pro zadání čísla
const topInput = document.createElement('input');
const heightInput = document.createElement('input');
const leftInput = document.createElement('input');
const srcInput = document.createElement('input');
// Nastavení atributů elementu
topInput.type = 'number';
topInput.step = '10';
topInput.value = '300';
topInput.id = 'top-slider';
heightInput.type = 'number';
heightInput.step = '10';
heightInput.value = '300';
heightInput.min = '0';
heightInput.id = 'height-slider';
leftInput.type = 'number';
leftInput.step = '10';
leftInput.value = '50';
leftInput.id = 'left-slider';
srcInput.type = 'text';
srcInput.id = 'src-input';
// Přidání elementu do DOM
helpElement.appendChild(document.createElement("br"));
helpElement.appendChild(topInput);
helpElement.appendChild(heightInput);
helpElement.appendChild(leftInput);
helpElement.appendChild(srcInput);
const topSlider = document.getElementById('top-slider');
const heightSlider = document.getElementById('height-slider');
const leftSlider = document.getElementById('left-slider');
const srcSlider = document.getElementById('src-input');
//Vytvoření labelů
const topLabel = document.createElement('span');
const heightLabel = document.createElement('span');
const leftLabel = document.createElement('span');
const srcLabel = document.createElement('span');
// Nastavení popisků k debug inputům:"
topLabel.textContent = 'Odsazení ze shora:';
heightLabel.textContent = 'Velikost:';
leftLabel.textContent = 'Odsazení z leva:';
srcLabel.textContent = 'Odkaz:';
topLabel.className = 'greyOption';
heightLabel.className = 'greyOption';
leftLabel.className = 'greyOption';
srcLabel.className = 'greyOption';
topSlider.parentNode.insertBefore(topLabel, topSlider);
heightSlider.parentNode.insertBefore(heightLabel, heightSlider);
leftSlider.parentNode.insertBefore(leftLabel, leftSlider);
srcSlider.parentNode.insertBefore(srcLabel, srcSlider);
topSlider.addEventListener('input', (event) => {
document.documentElement.style.setProperty('--top-slider', event.target.value);
});
heightSlider.addEventListener('input', (event) => {
document.documentElement.style.setProperty('--height-slider', event.target.value);
});
leftSlider.addEventListener('input', (event) => {
document.documentElement.style.setProperty('--left-slider', event.target.value);
});
srcSlider.addEventListener('input', (event) => {
document.querySelector("#studentImg").src = event.target.value;
});
let img = document.querySelector("#studentImg");
img.style.top = "calc(var(--top-slider) * 1px)";
img.style.height = "calc(var(--height-slider) * 1px)";
img.style.left = "calc(var(--left-slider) * 1px)";
}