This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
content_script.js
1198 lines (1046 loc) · 44.5 KB
/
content_script.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//----------- Begin miniPandA declaration --------------//
const defaultTab = document.querySelectorAll('.Mrphs-sitesNav__menuitem');
const defaultTabCount = Object.keys(defaultTab).length;
const otherSiteTab = document.querySelectorAll('#otherSiteList > li');
const otherSiteTabCount = Object.keys(otherSiteTab).length;
const header_name = ["締め切り24時間以内", "締め切り5日以内", "締め切り14日以内", "その他"];
const header_color = ["danger", "warning", "success", "other"];
const initLetter = ["a", "b", "c", "d"];
const nowTime = new Date().getTime();
// const nowTime = 1590937200000;
const cacheInterval = 120
const tabList = getTabList();
let hamburger = createElem("div", {className: "loader"});
let header = createElem("div");
let header_title = createElem("span", {className: "q"});
let list_container = createElem("div", {className: "sidenav-list"});
let list_body = createElem("div");
let h2 = createElem("h2");
let p_chkbox = createElem("input", {type: "checkbox", className: "todo-check"});
let p_label = createElem("label");
let p_date = createElem("p", {className: "kadai-date"});
let remain = createElem("span", {className: "time-remain"});
let p_title = createElem("p", {className: "kadai-title"});
let main_div = createElem("div", {id: "mySidenav"});
let kadaiDiv = createElem("div", {className: "kadai-tab"});
let examDiv = createElem("div", {className: "exam-tab"});
let parent = document.getElementById('pageBody');
let ref = document.getElementById('toolMenuWrap');
//----------- End miniPandA declaration --------------//
function createElem(tag, dict) {
let elem = document.createElement(tag);
for (let key in dict) {
elem[key] = dict[key];
}
return elem
}
function appendChildAll(to, array) {
for (let obj in array) {
to.appendChild(array[obj]);
}
return to
}
function insertCSS() {
let css = createElem("link", {rel: "stylesheet", type: "text/css"});
css.href = chrome.extension.getURL('css/custom-panda.css');
document.body.appendChild(css);
}
function parseID(lectureIDList) {
let idList = {};
for (let i = 0; i < lectureIDList.length; i++) {
let id = lectureIDList[i].lectureID;
idList[id] = lectureIDList[i].lectureName;
}
return idList;
}
function sortKadai(parsedKadai) {
for (let i = 0; i < parsedKadai.length; i++) {
let kadaiList = parsedKadai[i].kadaiList;
kadaiList.sort(function (a, b) {
if (a.dueTimeStamp < b.dueTimeStamp) return -1;
if (a.dueTimeStamp > b.dueTimeStamp) return 1;
if (a.kadaiTitle < b.kadaiTitle) return -1;
if (a.kadaiTitle > b.kadaiTitle) return 1;
return 0;
});
parsedKadai[i].kadaiList = kadaiList;
}
parsedKadai.sort(function (a, b) {
if (a.closestTime < b.closestTime) return -1;
else return 1;
});
return parsedKadai;
}
function genUniqueStr() {
return "m" + new Date().getTime().toString(16) + Math.floor(123456 * Math.random()).toString(16);
}
function getDaysUntil(dt1, dt2) {
let diff = (dt2 - dt1) / 1000;
diff /= 3600 * 24;
if (diff < 0) diff = 9999;
return (diff);
}
function getTimeRemain(_remainTime) {
let day = Math.floor(_remainTime / (3600 * 24));
let hours = Math.floor((_remainTime - (day * 3600 * 24)) / 3600);
let minutes = Math.floor((_remainTime - (day * 3600 * 24 + hours * 3600)) / 60);
return [day, hours, minutes]
}
function toggleKadaiTab() {
let kadaiTab = document.querySelector('.kadai-tab');
kadaiTab.style.display = '';
let examTab = document.querySelector('.exam-tab');
examTab.style.display = 'none';
let addMemoButton = document.querySelector('.plus-button');
addMemoButton.style.display = '';
let lastKadaiGetTime = document.querySelector('.kadai-time');
lastKadaiGetTime.style.display = '';
}
function toggleExamTab() {
let kadaiTab = document.querySelector('.kadai-tab');
kadaiTab.style.display = 'none';
let examTab = document.querySelector('.exam-tab');
examTab.style.display = '';
let addMemoButton = document.querySelector('.plus-button');
addMemoButton.style.display = 'none';
let lastKadaiGetTime = document.querySelector('.kadai-time');
lastKadaiGetTime.style.display = 'none';
// console.log("examtab pressed");
loadExamfromStorage();
}
function toggleMemoBox() {
let addMemoBox = document.querySelector('.addMemoBox');
let toggleStatus = addMemoBox.style.display;
if (toggleStatus === '') addMemoBox.style.display = 'none';
else addMemoBox.style.display = '';
}
function addMemo(kadaiMemo, kadaiMemoListAll) {
let idList = parseID(tabList);
kadaiMemo = sortKadai(kadaiMemo)
//Delete old
let oldMemo = document.querySelectorAll('.todoMemo');
oldMemo.forEach((item) => {
item.remove()
});
for (let i = 0; i < 4; i++) {
for (let row = 0; row < kadaiMemo.length; row++) {
let kadaiMemoList = kadaiMemo[row].kadaiList;
let lectureID = kadaiMemo[row].lectureID;
for (let memo = 0; memo < kadaiMemoList.length; memo++) {
let dueTime = kadaiMemoList[memo].dueTimeStamp;
let kid = kadaiMemoList[memo].kid;
let kadaiTitle = kadaiMemoList[memo].kadaiTitle;
let daysUntilDue = getDaysUntil(nowTime, dueTime);
if ((daysUntilDue <= 1 && i === 0) || (daysUntilDue > 1 && daysUntilDue <= 5 && i === 1) || (daysUntilDue > 5 && daysUntilDue <= 14 && i === 2) || (daysUntilDue > 14 && daysUntilDue <= 1000 && i === 3)) {
let kadaiTodoDiv = document.querySelector(`#${initLetter[i]}${lectureID}`);
let chkbox = p_chkbox.cloneNode(true);
let label = p_label.cloneNode(true);
let date = p_date.cloneNode(true);
let remain_time = remain.cloneNode(true);
let title = p_title.cloneNode(true);
let _date = new Date(dueTime);
let dispDue = _date.toLocaleDateString() + " " + _date.getHours() + ":" + ('00' + _date.getMinutes()).slice(-2);
let timeRemain = getTimeRemain((dueTime - nowTime) / 1000);
if (kadaiTodoDiv === null) {
let C_list_body = list_body.cloneNode(true);
C_list_body.className = `kadai-${header_color[i]}`;
C_list_body.classList.add("todoMemo");
C_list_body.id = initLetter[i] + lectureID;
let lectureName = idList[lectureID];
if (lectureName === undefined) lectureName = lectureID;
let C_h2 = h2.cloneNode(true);
C_h2.className = `lecture-${header_color[i]}`;
C_h2.textContent = "" + lectureName;
C_list_body.appendChild(C_h2);
// search for appropriate place to append
let parent = document.querySelector(`.sidenav-list-${header_color[i]}`);
parent.appendChild(C_list_body);
parent.style.display = "";
let header = document.querySelector(`.sidenav-${header_color[i]}`);
header.style.display = "";
kadaiTodoDiv = document.querySelector(`#${initLetter[i]}${lectureID}`);//todo 不安
}
date.textContent = "" + dispDue;
remain_time.textContent = `あと${timeRemain[0]}日${timeRemain[1]}時間${timeRemain[2]}分`;
let memoBadge = document.createElement('span');
memoBadge.classList.add("add-badge");
memoBadge.classList.add("add-badge-success");
memoBadge.innerText = "メモ";
let deleteBadge = document.createElement('span');
deleteBadge.className = "del-button";
deleteBadge.id = kid;
deleteBadge.addEventListener('click', deleteKadaiMemo, true);
deleteBadge.innerText = "×";
title.appendChild(memoBadge);
title.append(kadaiTitle);
title.appendChild(deleteBadge);
const q = kadaiMemoListAll.findIndex((kadai) => {
return (kadai.kid === kid);
});
if (q !== -1) {
if (kadaiMemoListAll[q].isFinished === 1) chkbox.checked = true;
}
chkbox.id = kid;
chkbox.lectureID = lectureID;
chkbox.addEventListener('change', updateKadaiMemoTodo, false);
label.htmlFor = kid;
//add memo tag
chkbox.classList.add("todoMemo");
label.classList.add("todoMemo");
date.classList.add("todoMemo");
remain_time.classList.add("todoMemo");
title.classList.add("todoMemo");
appendChildAll(kadaiTodoDiv, [chkbox, label, date, remain_time, title]);
//hide relaxpanda
let relaxPanda = document.querySelector(".relaxpanda");
if (relaxPanda) relaxPanda.innerHTML = "";
}
}
}
}
}
function parseKadaiMemo(kadaiMemo, _kadaiMemoListAll) {
if (_kadaiMemoListAll === undefined) _kadaiMemoListAll = [];
let kadaiMemoListAll = [];
for (let item = 0; item < kadaiMemo.length; item++) {
let kadaiMemoList = kadaiMemo[item].kadaiList;
let lectureID = kadaiMemo[item].lectureID;
for (let kadai = 0; kadai < kadaiMemoList.length; kadai++) {
let kadaiMemoID = kadaiMemoList[kadai].kid;
let kadaiMemoTitle = kadaiMemoList[kadai].kadaiTitle;
let kadaiMemoDue = kadaiMemoList[kadai].dueTimeStamp;
let isFinished = 0;
const q = _kadaiMemoListAll.findIndex((item) => {
return (item.kid === kadaiMemoID);
});
if (q !== -1) {
isFinished = _kadaiMemoListAll[q].isFinished;
}
kadaiMemoListAll.push({
kid: kadaiMemoID,
dueDate: kadaiMemoDue,
isFinished: isFinished,
lectureID: lectureID,
title: kadaiMemoTitle
});
}
}
return kadaiMemoListAll;
}
function todoAdd(event) {
let selectedIdx = document.querySelector(".todoLecName").selectedIndex;
let todoLecID = document.querySelector(".todoLecName").options[selectedIdx].id;
let todoContent = document.querySelector(".todoContent").value;
let todoDue = document.querySelector(".todoDue").value;
let todoTimestamp = new Date(`${todoDue}`).getTime();
getFromStorage('kadaiMemo').then(function (_kadaiMemo) {
let kadaiMemo;
if (typeof _kadaiMemo !== 'undefined' && _kadaiMemo.length > 0) {
const q = _kadaiMemo.findIndex((item) => {
return (item.lectureID === todoLecID);
});
if (q !== -1) {
_kadaiMemo[q].kadaiList.push({
kid: genUniqueStr(),
dueTimeStamp: todoTimestamp,
kadaiTitle: todoContent
});
} else {
let kadaiList = [{kid: genUniqueStr(), dueTimeStamp: todoTimestamp, kadaiTitle: todoContent}];
let kadai = {lectureID: todoLecID, kadaiList: kadaiList};
_kadaiMemo.push(kadai);
}
kadaiMemo = _kadaiMemo;
} else {
let kadaiList = [{kid: genUniqueStr(), dueTimeStamp: todoTimestamp, kadaiTitle: todoContent}];
kadaiMemo = [{lectureID: todoLecID, kadaiList: kadaiList}];
}
getFromStorage('kadaiMemoTodo').then(function (kadaiMemoTodo) {
let kadaiMemoListAll = parseKadaiMemo(kadaiMemo, kadaiMemoTodo);
addMemo(kadaiMemo, kadaiMemoListAll);
// Save
let entity = {};
entity.kadaiMemo = kadaiMemo;
chrome.storage.local.set(entity, function () {
});
entity = {};
entity.kadaiMemoTodo = kadaiMemoListAll;
chrome.storage.local.set(entity, function () {
});
});
});
}
function createSideNav(lastKadaiGetTime) {
let lectureIDList = tabList;
// add hamburger
let topbar = document.getElementById("mastLogin");
hamburger.addEventListener('click', toggleSideNav);
try {
topbar.appendChild(hamburger);
} catch (e) {
console.log("could not launch miniPandA.")
}
main_div.classList.add("sidenav");
main_div.classList.add("cp_tab");
const img = chrome.extension.getURL("img/logo.png");
let logo = createElem("img", {className: "logo", alt: "logo", src: img});
let a = createElem("a", {href: "#", id: "close_btn", textContent: "×"});
a.classList.add("closebtn");
a.classList.add("q");
a.addEventListener('click', toggleSideNav);
let kadaiTab = createElem("input", {type: "radio", id: "kadaiTab", name: "cp_tab", checked: true});
kadaiTab.addEventListener('click', toggleKadaiTab);
let kadaiTabLabel = createElem("label", {htmlFor: "kadaiTab", innerText: "課題一覧"});
let examTab = createElem("input", {type: "radio", id: "examTab", name: "cp_tab", checked: false});
examTab.addEventListener('click', toggleExamTab);
let examTabLabel = createElem("label", {htmlFor: "examTab", innerText: "テスト・クイズ一覧"});
let addMemoButton = createElem("button", {className: "plus-button", innerText: "+"});
addMemoButton.addEventListener('click', toggleMemoBox, true);
if ((nowTime - lastKadaiGetTime) / 1000 >= cacheInterval) lastKadaiGetTime = nowTime
if (lastKadaiGetTime === undefined) lastKadaiGetTime = nowTime
let dateTime = new Date(lastKadaiGetTime);
let lastKadaiLoad = createElem("p", {className: "kadai-time"});
lastKadaiLoad.innerText = "取得日時: " + dateTime.toLocaleDateString() + " " + dateTime.getHours() + ":" + ('00' + dateTime.getMinutes()).slice(-2) + ":" + ('00' + dateTime.getSeconds()).slice(-2);
appendChildAll(main_div, [logo, a, kadaiTab, kadaiTabLabel, examTab, examTabLabel, addMemoButton, lastKadaiLoad]);
// add edit box
let memoEditBox = createElem("div");
memoEditBox.classList.add("examBox");
memoEditBox.classList.add("addMemoBox");
memoEditBox.style.display = "none";
let todo_label = createElem("label");
todo_label.style.display = "block";
let todoLecLabel = todo_label.cloneNode(true);
todoLecLabel.innerText = "講義名";
let todoLecSelect = createElem("select", {className: "todoLecName"});
let todoLecOption = createElem("option");
for (let i = 0; i < lectureIDList.length; i++) {
if (lectureIDList[i].lectureName === undefined) continue;
let c_todoLecOption = todoLecOption.cloneNode(true);
c_todoLecOption.text = lectureIDList[i].lectureName;
c_todoLecOption.id = lectureIDList[i].lectureID;
todoLecSelect.appendChild(c_todoLecOption);
}
todoLecLabel.appendChild(todoLecSelect);
let todoContentLabel = todo_label.cloneNode(true);
todoContentLabel.innerText = "メモ";
let todoContentInput = createElem("input", {type: "text", className: "todoContent"});
todoContentLabel.appendChild(todoContentInput);
let todoDueLabel = todo_label.cloneNode(true);
todoDueLabel.innerText = "期限";
let todoDueInput = createElem("input", {type: "datetime-local", className: "todoDue"});
todoDueInput.value = new Date(`${new Date().toISOString().substr(0, 16)}-10:00`).toISOString().substr(0, 16);
todoDueLabel.appendChild(todoDueInput);
let todoSubmitButton = createElem("button", {type: "submit", id: "todo-add", innerText: "追加"});
todoSubmitButton.addEventListener('click', todoAdd, true);
appendChildAll(memoEditBox, [todoLecLabel, todoContentLabel, todoDueLabel, todoSubmitButton]);
kadaiDiv.appendChild(memoEditBox);
// add edit box
// try {
// parent.insertBefore(main_div, ref);
// } catch (e) {
// console.log("Could not create sidenav.");
// }
parent.insertBefore(main_div, ref);
}
function insertSideNav(parsedKadai, kadaiListAll, lectureIDList) {
let idList = parseID(lectureIDList);
parsedKadai = sortKadai(parsedKadai);
console.log(parsedKadai);
console.log(idList);
// generate kadai todo list
for (let i = 0; i < 4; i++) {
let item_cnt = 0;
var C_header = header.cloneNode(true);
var C_header_title = header_title.cloneNode(true);
C_header.className = `sidenav-${header_color[i]}`;
C_header.style.display = "none";
C_header_title.textContent = `${header_name[i]}`;
C_header.appendChild(C_header_title);
// list begin //
var C_list_container = list_container.cloneNode(true);
C_list_container.classList.add(`sidenav-list-${header_color[i]}`);
C_list_container.style.display = "none";
for (let item = 0; item < parsedKadai.length; item++) {
let kadaiList = parsedKadai[item].kadaiList;
let lectureID = parsedKadai[item].lectureID;
var C_list_body = list_body.cloneNode(true);
C_list_body.className = `kadai-${header_color[i]}`;
C_list_body.id = initLetter[i] + lectureID;
let lectureName = idList[lectureID];
if (lectureName === undefined) lectureName = "不明";
var C_h2 = h2.cloneNode(true);
C_h2.className = `lecture-${header_color[i]}`;
C_h2.textContent = "" + lectureName;
C_list_body.appendChild(C_h2);
let cnt = 0;
for (let id = 0; id < kadaiList.length; id++) {
let chkbox = p_chkbox.cloneNode(true);
let label = p_label.cloneNode(true);
let date = p_date.cloneNode(true);
let remain_time = remain.cloneNode(true);
let title = p_title.cloneNode(true);
let dueTime = kadaiList[id].dueTimeStamp;
let _date = new Date(dueTime);
let kid = kadaiList[id].kid;
let kadaiTitle = kadaiList[id].kadaiTitle;
let dispDue = _date.toLocaleDateString() + " " + _date.getHours() + ":" + ('00' + _date.getMinutes()).slice(-2);
let timeRemain = getTimeRemain((dueTime - nowTime) / 1000);
let daysUntilDue = getDaysUntil(nowTime, dueTime);
if ((daysUntilDue <= 1 && i === 0) || (daysUntilDue > 1 && daysUntilDue <= 5 && i === 1) || (daysUntilDue > 5 && daysUntilDue <= 14 && i === 2) || (daysUntilDue > 14 && i === 3)) {
date.textContent = "" + dispDue;
remain_time.textContent = `あと${timeRemain[0]}日${timeRemain[1]}時間${timeRemain[2]}分`;
title.textContent = "" + kadaiTitle;
const q = kadaiListAll.findIndex((kadai) => {
return (kadai.kid === kid);
});
if (q !== -1) {
if (kadaiListAll[q].isFinished === 1) chkbox.checked = true;
}
chkbox.id = kid;
chkbox.lectureID = lectureID;
chkbox.addEventListener('change', updateKadaiTodo, false);
label.htmlFor = kid;
appendChildAll(C_list_body, [chkbox, label, date, remain_time, title]);
cnt++;
}
}
if (cnt > 0) {
C_list_container.appendChild(C_list_body);
item_cnt++;
}
}
// list end //
if (item_cnt > 0) {
C_header.style.display = "";
C_list_container.style.display = "";
}
appendChildAll(main_div, [kadaiDiv, examDiv]);
appendChildAll(kadaiDiv, [C_header, C_list_container]);
}
if (parsedKadai.length === 0) {
let kadaiTab = kadaiDiv;
const img_relaxPanda = chrome.extension.getURL("img/relaxPanda.png");
let relaxDiv = createElem("div", {className: "relaxpanda"});
let relaxPandaP = createElem("p", {className: "relaxpanda-p", innerText: "現在表示できる課題はありません"});
let relaxPandaImg = createElem("img", {className: "relaxpanda-img", alt: "logo", src: img_relaxPanda});
appendChildAll(relaxDiv, [relaxPandaP, relaxPandaImg]);
kadaiTab.appendChild(relaxDiv);
}
Promise.all([getFromStorage('kadaiMemo'), getFromStorage('kadaiMemoTodo')])
.then(([kadaiMemo, kadaiMemoTodo]) => {
if (kadaiMemo !== undefined) {
if (kadaiMemoTodo === undefined) kadaiMemoTodo = [];
addMemo(kadaiMemo, kadaiMemoTodo);
}
});
}
function insertSideNavExam(parsedExam, examListAll, lectureIDList, lastExamGetTime) {
let idList = parseID(lectureIDList);
let examDiv = document.querySelector('.exam-tab');
examDiv.innerHTML = '';
let examBox = createElem("div", {className: "examBox"});
// let loadButton = createElem("button", {innerText: "テスト・クイズ情報を取得する", className: "btn-square"});
// loadButton.addEventListener("click", loadExamfromPanda, false);
let dateTime = new Date(lastExamGetTime);
let lastLoad = createElem("p", {className: "lastLoad"});
lastLoad.innerText = "最終更新: " + dateTime.toLocaleDateString() + " " + dateTime.getHours() + ":" + ('00' + dateTime.getMinutes()).slice(-2) + ":" + ('00' + dateTime.getSeconds()).slice(-2);
if (lastExamGetTime === undefined) lastLoad.innerText = "最終更新:未取得";
let info1 = createElem("p", {innerText: "現在調整中です。"});
// let info1 = createElem("p", {innerText: "※PandAに若干の負荷がかかるため、必要時以外取得ボタンを押さないようお願いします。"});
// let info2 = createElem("p", {innerText: "※各コースサイトの「テスト・クイズ」に関連付けられてないものについては取得できません。取得されたテスト・クイズ一覧は参考程度にご覧ください。"});
// appendChildAll(examBox, [lastLoad, loadButton, info1, info2]);
appendChildAll(examBox, [info1]);
// generate exam todo list
for (let i = 0; i < 4; i++) {
let item_cnt = 0;
var C_header = header.cloneNode(true);
var C_header_title = header_title.cloneNode(true);
C_header.className = `sidenav-${header_color[i]}`;
C_header_title.textContent = `${header_name[i]}`;
// list begin //
var C_list_container = list_container.cloneNode(true);
let parsedExam_length = 0;
if (parsedExam !== undefined) parsedExam_length = parsedExam.length;
for (let item = 0; item < parsedExam_length; item++) {
let examList = parsedExam[item].examList;
let lectureID = parsedExam[item].lectureID;
var C_list_body = list_body.cloneNode(true);
C_list_body.className = `kadai-${header_color[i]}`;
let lectureName = idList[lectureID];
if (lectureName === undefined) lectureName = "不明";
var C_h2 = h2.cloneNode(true);
C_h2.className = `lecture-${header_color[i]}`;
C_h2.textContent = "" + lectureName;
C_list_body.appendChild(C_h2);
let cnt = 0;
for (let id = 0; id < examList.length; id++) {
let chkbox = p_chkbox.cloneNode(true);
let label = p_label.cloneNode(true);
let date = p_date.cloneNode(true);
let remain_time = remain.cloneNode(true);
let title = p_title.cloneNode(true);
let dueTime = examList[id].dueDate;
let _date = new Date(dueTime);
let eid = examList[id].eid;
let kadaiTitle = examList[id].title;
let dispDue = _date.toLocaleDateString() + " " + _date.getHours() + ":" + ('00' + _date.getMinutes()).slice(-2);
let timeRemain = getTimeRemain((dueTime - nowTime) / 1000);
let daysUntilDue = getDaysUntil(nowTime, dueTime);
if ((daysUntilDue <= 1 && i === 0) || (daysUntilDue > 1 && daysUntilDue <= 5 && i === 1) || (daysUntilDue > 5 && daysUntilDue <= 14 && i === 2) || (daysUntilDue > 14 && i === 3)) {
date.textContent = "" + dispDue;
remain_time.textContent = `あと${timeRemain[0]}日${timeRemain[1]}時間${timeRemain[2]}分`;
title.textContent = "" + kadaiTitle;
const q = examListAll.findIndex((exam) => {
return (exam.eid === eid);
});
if (q !== -1) {
if (examListAll[q].isFinished === 1) chkbox.checked = true;
}
chkbox.id = eid;
chkbox.lectureID = lectureID;
chkbox.addEventListener('change', updateExamTodo, false);
label.htmlFor = eid;
appendChildAll(C_list_body, [chkbox, label, date, remain_time, title]);
cnt++;
}
}
if (cnt > 0) {
C_list_container.appendChild(C_list_body);
C_header.appendChild(C_header_title);
item_cnt++;
}
}
// list end //
examDiv.appendChild(examBox);
if (item_cnt > 0) {
appendChildAll(examDiv, [C_header, C_list_container]);
}
}
}
let toggle = false;
function toggleSideNav() {
if (toggle) {
main_div.style.width = "0";
document.getElementById("cover").remove();
} else {
main_div.style.width = "300px";
let cover = document.createElement("div");
cover.id = "cover";
document.getElementsByTagName("body")[0].appendChild(cover);
cover.onclick = toggleSideNav;
}
toggle = 1 - toggle;
}
function updateKadaiTodo(event) {
// TODO: 済 にしてもいいかも
getFromStorage('kadaiTodo').then(function (kadaiTodo) {
if (typeof kadaiTodo !== 'undefined') {
const q = kadaiTodo.findIndex((kadai) => {
return (kadai.kid === event.target.id);
});
if (q !== -1) {
kadaiTodo[q].isFinished = 1 - kadaiTodo[q].isFinished;
}
}
saveKadaiTodo(kadaiTodo);
});
}
function updateKadaiMemoTodo(event) {
getFromStorage('kadaiMemoTodo').then(function (kadaiMemoTodo) {
if (typeof kadaiMemoTodo !== 'undefined') {
const q = kadaiMemoTodo.findIndex((kadai) => {
return (kadai.kid === event.target.id);
});
if (q !== -1) {
kadaiMemoTodo[q].isFinished = 1 - kadaiMemoTodo[q].isFinished;
}
}
saveKadaiMemoTodo(kadaiMemoTodo);
});
}
function updateExamTodo(event) {
getFromStorage('examTodo').then(function (examTodo) {
if (typeof examTodo !== 'undefined') {
const q = examTodo.findIndex((exam) => {
return (exam.eid === parseInt(event.target.id));
});
if (q !== -1) {
examTodo[q].isFinished = 1 - examTodo[q].isFinished;
}
}
saveExamTodo(examTodo);
});
}
function deleteKadaiMemo(event) {
getFromStorage('kadaiMemo').then(function (kadaiMemo) {
for (let i = 0; i < kadaiMemo.length; i++) {
let kadaiList = kadaiMemo[i].kadaiList;
for (let item = 0; item < kadaiList.length; item++) {
let kid = kadaiList[item].kid;
if (kid === event.target.id) {
let delMemoSpan = document.querySelectorAll(`#${event.target.id}`)[1];
delMemoSpan.innerText = "削除済";
kadaiList.splice(item, 1);
break;
}
}
}
let entity = {};
entity.kadaiMemo = kadaiMemo;
chrome.storage.local.set(entity, function () {
});
});
}
function addNotificationBadge(lectureIDList, upToDateKadaiList) {
const lectureIDCount = lectureIDList.length;
//タブにある全科目を回す。
for (let i = 0; i < lectureIDCount; i++) {
// default Tab
if (lectureIDList[i].type === 'default') {
for (let j = 2; j < defaultTabCount; j++) {
let lectureID = defaultTab[j].getElementsByTagName('a')[1].getAttribute('data-site-id');
if (lectureID === null) lectureID = defaultTab[j].getElementsByTagName('a')[0].getAttribute('data-site-id');
const q = upToDateKadaiList.findIndex((kadai) => {
return (kadai.lectureID === lectureID);
});
if (q !== -1) {
if (upToDateKadaiList[q].isUpdate === 1) {
defaultTab[j].classList.add('badge');
}
let daysUntilDue = getDaysUntil(nowTime, upToDateKadaiList[q].closestTime);
if (daysUntilDue <= 1) {
defaultTab[j].classList.add('nav-danger');
defaultTab[j].getElementsByTagName('a')[0].classList.add('nav-danger');
defaultTab[j].getElementsByTagName('a')[1].classList.add('nav-danger');
} else if (daysUntilDue <= 5) {
defaultTab[j].classList.add('nav-warning');
defaultTab[j].getElementsByTagName('a')[0].classList.add('nav-warning');
defaultTab[j].getElementsByTagName('a')[1].classList.add('nav-warning');
} else if (daysUntilDue <= 14) {
defaultTab[j].classList.add('nav-safe');
defaultTab[j].getElementsByTagName('a')[0].classList.add('nav-safe');
defaultTab[j].getElementsByTagName('a')[1].classList.add('nav-safe');
}
}
}
}
}
}
function getTabList() {
const elementCollection = document.getElementsByClassName("fav-sites-entry");
const elements = Array.prototype.slice.call(elementCollection);
const lectureIDList = [];
for (const elem of elements) {
let tabInfo = {}
let lecture = elem.getElementsByTagName("div")[0].getElementsByTagName("a")[0];
let lectureName = lecture.title;
let lectureID = lecture.href.match("(https?:\/\/[^/]+)\/portal\/site-reset\/([^/]+)")
if (lectureID && lectureID[2].slice(0,4) === "2021") {
tabInfo.type = 'default'; //TODO
tabInfo.lectureID = lectureID[2];
tabInfo.lectureName = lectureName;
lectureIDList.push(tabInfo);
}
}
console.log("getTabList", lectureIDList)
return lectureIDList;
}
function parseKadai(data, types) {
let parsedKadai = [];
let item = data.assignment_collection;
// console.log(data.assignment_collection);
for (let i = 0; i < item.length; i++) {
let temp = {};
let lecID = item[i].context;
let kid = item[i].id;
let title = item[i].title;
let due = parseInt(item[i].dueTime.epochSecond)*1000;
let isFinished = 0;
let status = item[i].status;
// add only available kadai
if (due <= nowTime) {
continue;
}
// TODO: これでもいいかも
// if (status === "CLOSED"){
// continue
// }
let kadaiDict = {kid: kid, dueTimeStamp: due, kadaiTitle: title};
if (types === 'mini') {
kadaiDict = {kid: kid, dueTimeStamp: due, kadaiTitle: title, isFinished: isFinished};
}
// すでに科目がListにあるか見る
const q = parsedKadai.findIndex((kadai) => {
return (kadai.lectureID === lecID);
});
//無ければ新規作成
if (q === -1) {
temp.lectureID = lecID;
temp.kadaiList = [kadaiDict];
if (types !== "mini") {
temp.closestTime = due;
temp.farthestTime = due;
}
parsedKadai.push(temp);
} else {
temp = parsedKadai[q];
//一番期限がやばい課題のタイムスタンプを記録
if (types !== "mini") {
if (temp.closestTime > due) temp.closestTime = due;
if (temp.farthestTime < due) temp.farthestTime = due;
}
temp.kadaiList.push(kadaiDict);
parsedKadai[q] = temp;
}
}
return parsedKadai;
}
function extractKadai(parsedKadai) {
let kadaiListAll = [];
for (let i = 0; i < parsedKadai.length; i++) {
let kadaiList = parsedKadai[i].kadaiList;
for (let kadai = 0; kadai < kadaiList.length; kadai++) {
let tmp = {};
tmp.kid = kadaiList[kadai].kid;
tmp.isFinished = 0;
kadaiListAll.push(tmp);
}
}
return kadaiListAll
}
function getKadaiTodo(parsedKadai) {
let kadaiListAll = extractKadai(parsedKadai);
getFromStorage('kadaiTodo').then(function (kadaiTodo) {
if (typeof kadaiTodo !== 'undefined') {
for (let i = 0; i < kadaiListAll.length; i++) {
let kid = kadaiListAll[i].kid;
const q = kadaiTodo.findIndex((kadai) => {
return (kadai.kid === kid);
});
if (q !== -1) {
if (kadaiTodo[q].isFinished === 1) kadaiListAll[i].isFinished = 1;
}
}
}
if (parsedKadai.length !== 0) {
saveKadaiTodo(kadaiListAll);
}
insertSideNav(parsedKadai, kadaiListAll, tabList);
});
}
function getExamTodo(examListAll, parsedExam) {
getFromStorage('examTodo').then(function (examTodo) {
if (typeof examTodo !== 'undefined' && examTodo.length > 0) {
for (let i = 0; i < examListAll.length; i++) {
let eid = examListAll[i].eid;
const q = examTodo.findIndex((exam) => {
return (exam.eid === eid);
});
if (q !== -1) {
if (examTodo[q].isFinished === 1) examListAll[i].isFinished = 1;
}
}
}
saveExamTodo(examListAll, parsedExam);
insertSideNavExam(parsedExam, examListAll, tabList, nowTime);
});
}
function getKadaiFromPandA() {
// console.log("connecting to panda api");
return $.ajax({
url: "https://panda.ecs.kyoto-u.ac.jp/direct/assignment/my.json",
dataType: "json",
type: "get",
cache: false,
});
}
function getFromStorage(key) {
return new Promise(function (resolve, reject) {
chrome.storage.local.get(key, function (items) {
resolve(items[key]);
});
});
}
function updateVisited(lectureID) {
getFromStorage('hasNewItem').then(function (hasNewItem) {
if (hasNewItem === undefined) return 0;
const q = hasNewItem.findIndex((kadai) => {
return (kadai.lectureID === lectureID);
});
if (q !== -1) {
hasNewItem[q].isUpdate = 0;
saveHasNew(hasNewItem);
}
});
}
function saveLectureID(tablist) {
let entity = {};
entity.lectureInfo = tablist;
entity.lastLectureIDGetTime = nowTime;
chrome.storage.local.set(entity, function () {
});
}
function saveKadai(parsedKadai) {
let entity = {};
entity.kadai = parsedKadai;
entity.lastKadaiGetTime = nowTime;
chrome.storage.local.set(entity, function () {
});
}
function saveHasNew(noticationList) {
let entity = {};
entity.hasNewItem = noticationList;
chrome.storage.local.set(entity, function () {
});
}
function saveKadaiTodo(kadaiListAll) {
let entity = {};
entity.kadaiTodo = kadaiListAll;
chrome.storage.local.set(entity, function () {
});
}
function saveKadaiMemoTodo(kadaiMemoListAll) {
let entity = {};
entity.kadaiMemoTodo = kadaiMemoListAll;
chrome.storage.local.set(entity, function () {
});
}
function saveExamTodo(examListAll, parsedExam) {
let entity = {};
entity.examTodo = examListAll;
chrome.storage.local.set(entity, function () {
});
if (parsedExam !== undefined) {
entity = {};
entity.parsedExam = parsedExam;
chrome.storage.local.set(entity, function () {
});
}
entity = {};
entity.lastExamGetTime = nowTime;
chrome.storage.local.set(entity, function () {
});
}
function createNotificationList(upToDateKadaiList, hasNewItem) {
let notificationList = [];
for (let i = 0; i < upToDateKadaiList.length; i++) {
let tmp = upToDateKadaiList[i];
let lectureID = upToDateKadaiList[i].lectureID;
if (typeof hasNewItem !== 'undefined') {
const q = hasNewItem.findIndex((item) => {
return (item.lectureID === lectureID);
});
if (q === -1) {
// 差分あり
if (upToDateKadaiList[i].isUpdate === 1) {
tmp.isUpdate = 1;
}
// 差分なし
else {
tmp.isUpdate = 0;
}
} else {
let hasNew = hasNewItem[q].isUpdate;
// 差分あり
if (upToDateKadaiList[i].isUpdate === 1) {
tmp.isUpdate = 1;
}
// 差分なし
else {
//もし過去にupdateを確認してなかったら(hasNew=1だったら)引き続き1が入る。
//TODO: farthestTime が nowTimeより古ければisUpdate=0
tmp.isUpdate = hasNew;
}
}
} else {
// 差分あり
if (upToDateKadaiList[i].isUpdate === 1) {
tmp.isUpdate = 1;
}
// 差分なし
else {
tmp.isUpdate = 0;
}
}
notificationList.push(tmp);
}
return notificationList;
}
function compareKadai(parsedKadai, storedKadai) {
let upToDateKadaiList = [];
// 最新の課題を基準に1つずつ見ていく
for (let i = 0; i < parsedKadai.length; i++) {
let tmp = {};
let lectureID = parsedKadai[i].lectureID;
let closestTime = parsedKadai[i].closestTime;