-
Notifications
You must be signed in to change notification settings - Fork 0
/
nptu-redux.user.js
1913 lines (1816 loc) · 75 KB
/
nptu-redux.user.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
// ==UserScript==
// @name NPTU Redux
// @description Provides QOL improvements for the web control panel of Taiwan Pingtung University
// @license MIT
// @author MT.Hack
// @grant GM_setClipboard
// @grant GM_download
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_notification
// @inject-into auto
// @require https://cdn.jsdelivr.net/npm/dom-to-image-more@2.8.0/dist/dom-to-image-more.min.js
// @require https://code.getmdl.io/1.3.0/material.min.js
// @match *://webap*.nptu.edu.tw/*
// @downloadUrl https://raw.githubusercontent.com/mt-hack/nptu-redux/master/nptu-redux.user.js
// @updateUrl https://raw.githubusercontent.com/mt-hack/nptu-redux/master/nptu-redux.user.js
// @version 1.6.5
// ==/UserScript==
/*
=========================================================
User configurable options
=========================================================
*/
const optionIds = {
STYLIZED_LOGIN: 'enable-login-mod',
BUTTON_REPLACEMENT: 'enable-new-buttons',
GRADES_WIDGET: 'enable-grades-widget',
ABSENCE_WIDGET: 'enable-absence-widget',
CUSTOM_EXPORTS: 'enable-custom-exports',
CLASSROOM_AUTOFILL: 'enable-classroom-autofill',
SURVEY_AUTOFILL: 'enable-survey-autofill',
CHECKIN_HELPER: 'enable-checkin-helper',
TABLE_EXPORT: 'enable-table-exports',
WORK_DESCRIPTIONS: 'work-descriptions'
}
let options = {
// Beautifies login page (WIP)
enableLoginPageMod: GM_getValue(optionIds.STYLIZED_LOGIN, true),
// Enables button replacement (design WIP)
enableButtonReplacement: GM_getValue(optionIds.BUTTON_REPLACEMENT, true),
// Enables grade widget (Student accounts only)
enableGradeOnHome: GM_getValue(optionIds.GRADES_WIDGET, true),
// Enables absence widget (Student accounts only)
enableAbsenceOnHome: GM_getValue(optionIds.ABSENCE_WIDGET, true),
// Enables custom export options for printing
enableCustomExport: GM_getValue(optionIds.CUSTOM_EXPORTS, true),
// Enables max student number autofill based on classroom selection (Employee accounts only)
enableClassroomAutofillOnSelect: GM_getValue(optionIds.CLASSROOM_AUTOFILL, true),
// Adds buttons that help auto-fill all options in opinion surveys
enableSurveyAutoFill: GM_getValue(optionIds.SURVEY_AUTOFILL, true),
// Adds buttons that help speed up the process of checking in
enableCheckInHelper: GM_getValue(optionIds.CHECKIN_HELPER, true),
// Enables saving specific tables as images
enableTableExports: GM_getValue(optionIds.TABLE_EXPORT, true),
// Enables classroom shortcut (Employee accounts only)
enableClassroomShortcut: true,
// Enables instructor shortcut (Employee accounts only)
enableInstructorShortcut: true,
// Enables shortcut auto submit (Employee accounts only)
enableShortcutAutoSubmit: true,
// Pages whose tables need to be fixed; works like a whitelist
tableFixWhitelist: ["A0432SPage", "A0433SPage"],
locationSelectionPage: ["A0413A02Page"],
instructorShortcutPage: ["A0413S1Page"],
classShortcutPage: ["A0434SPage"],
// Enables table downloading on these table/div IDs
tableExportWhitelist: ["A0515S1_dgData", "A0515S_dgData", "A0809Q_dgData", "A0702S1_dgData", "B0105S_dgData", "B0208S_dgData", "A0425S_dgData", "B4002S_dgData", "A0413S_dgData_Content", "A0423S_dgData_Content"],
isFlexRowWhitelist: ["A0428S3Page", "B4002SPage", "A0428S1Page", "A0428S2Page", "A0428SPage", "A1609QPage", "B1414SPage", "A0711SPage", "A1305SPage"]
};
// Replace the subject groups with your own if you are an employee
let subjectGroups = {
ENG1001: 30,
ENG1003: 60,
ENG1004: 60,
ENG2001: 30,
ENG2002: 30,
ENG2003: 60,
ENG2005: 60,
ENG2009: 60,
ENG2015: 60,
ENG2027: 30,
ENG3001: 30,
ENG3005: 60,
ENG3007: 8,
ENG3008: 45,
ENG3019: 45,
ENG3040: 60,
ENG3032: 30,
ENG2008: 45,
ENG2028: 30,
ENG2031: 60,
ENG2032: 45,
ENG2033: 45,
ENG2034: 45,
ENG3004: 45,
ENG3009: 60,
ENG3039: 60,
ENG3040: 60,
ENG4001: 30,
ENG4002: 60,
ENG4008: 60,
ENG4014: 45,
ENG4024: 15,
ENG4036: 45,
ENG4037: 30,
ENG4041: 25,
ENI0001: 8,
ENI0005: 25,
ENI0007: 15,
ENI1001: 25,
ENI1007: 25,
ENI1011: 25,
ENI1117: 25,
ENI1135: 25,
ENI1137: 25,
ENI1302: 25,
}
let locationShortcuts = {
人文館103: 'G103',
人文館104: 'G104',
人文館二討: 'G212',
五育樓5F視聽: 'I500'
}
let instructorShortcuts = {
金大衛: '200010027',
余慧珠: '200010033',
項偉恩: '200009296',
梁愷: '200008819',
梁中行: '200009049',
王彩姿: '200008861',
楊昕昕: '200008862',
李惠敏: '200008812',
楊琇琇: '200008724',
張淑英: '200008978',
張理宏: '200008741'
}
/*
=========================================================
LIVE CODE
DO NOT TOUCH THE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
=========================================================
*/
// Button definitions
const buttonTypes = {
SEARCH: {
icon: 'search',
label: '查詢',
color: 'alt',
baseId: 'Query'
},
SEARCH_AGAIN: {
icon: 'search',
label: '重新查詢',
color: 'alt',
baseId: 'BackQuery'
},
BACK: {
icon: 'arrow_back',
label: '回上層',
color: 'flat',
baseId: 'Back'
},
PRINT: {
icon: 'print',
label: '產生報表',
color: 'alt',
baseId: 'Print'
},
CANCEL: {
icon: 'cancel',
label: '取消',
color: 'colored',
baseId: 'Cancel'
},
DELETE: {
icon: 'delete',
label: '刪除',
color: 'colored',
baseId: 'Delete'
},
LOOKUP: {
icon: 'pageview',
label: '帶出',
color: 'colored',
baseId: 'LookUp'
},
ADD: {
icon: 'add',
label: '新增',
color: 'alt',
baseId: 'Add'
},
SAVE: {
icon: 'save',
label: '存檔',
color: 'alt',
baseId: 'Save'
},
CHANGE_SEM: {
icon: 'event',
label: '切換學期',
color: 'alt',
baseId: 'GsTerm'
}
}
/*
Prototype helper methods
*/
Element.prototype.cloneElement = function(targetElementName = undefined, targetElementClass = undefined) {
let newElement = document.createElement(targetElementName || "span");
newElement.innerHTML = this.innerHTML;
if (targetElementClass) {
newElement.classList = targetElementClass;
} else {
newElement.classList = this.classList;
}
return newElement;
}
Element.prototype.replaceElement = function(newElementType = undefined) {
let newElement = document.createElement(newElementType || "span");
this.childNodes.forEach(x => {
newElement.appendChild(x);
})
this.replaceWith(newElement);
}
Element.prototype.appendAfter = function(element) {
element.parentNode.insertBefore(this, element.nextSibling);
}, false;
Element.prototype.appendBefore = function(element) {
element.parentNode.insertBefore(this, element);
}, false;
// modified from https://stackoverflow.com/a/10073788
Number.prototype.pad = function(width, z) {
z = z || '0';
let n = this + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
let emptyImage = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D';
let raisedButtonClassnames = 'mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent';
let raisedButtonAltClassnames = 'mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored';
let raisedButtonFlatClassnames = 'mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect';
let mainElement = document.querySelector('frame') || document.querySelector('form');
if (!mainElement) {
log('Main frame/form not detected; assuming unknown page.');
return;
}
let mainWindow = mainElement.contentWindow || mainElement.ownerDocument.defaultView;
let frameElement = mainWindow.frameElement || mainWindow;
let contentWindow = frameElement.contentWindow || frameElement;
if (contentWindow.WebForm_OnSubmit) {
contentWindow.WebForm_OnSubmit = function() {
toggleOverlay(mainWindow.document);
}
}
/*
===================
Image Fix Injection
===================
*/
let images = document.querySelectorAll('*[src*="_EN"]')
images.forEach(img => {
img.addEventListener('error', function() {
let englishSuffixRegex = new RegExp(/_en/gi);
this.src = this.src.replace(englishSuffixRegex, '');
})
})
/*
===================
CSS Injection
===================
*/
injectStyle(document.head, 'https://fonts.googleapis.com/icon?family=Material+Icons');
injectStyle(document.head, 'https://code.getmdl.io/1.3.0/material.indigo-blue.min.css');
injectCustomCss(document.head);
/*
====================
Homepage Injection
====================
*/
// fix alertify
if (document.querySelector('.alertify-log')) {
document.querySelectorAll('.alertify-log').forEach(x => {
let data = {
message: x.innerText,
timeout: 100000
};
x.remove();
let snackBar = make({
el: 'div',
class: 'mdl-js-snackbar mdl-snackbar',
id: 'material-alertify',
html: '<div class="mdl-snackbar__text"></div><button type="button" class="mdl-snackbar__action"></button>'
});
document.body.appendChild(snackBar);
componentHandler.upgradeElement(snackBar);
snackBar.MaterialSnackbar.showSnackbar(data);
})
}
// homepage
if (isHomepage(document)) {
if (!options.enableLoginPageMod) {
return;
}
let overlay = make({
el: 'div',
class: 'overlay',
html: `<div id="overlay-wave"><div class="wave -one"></div><div class="wave -two"></div><div class="wave -three"></div></div>`
});
document.body.appendChild(overlay);
let widthDummy = document.querySelector("#LoginDefault_txtScreenWidth");
if (widthDummy) {
widthDummy.style.display = "none";
}
let heightDummy = document.querySelector("#LoginDefault_txtScreenHeight");
if (heightDummy) {
heightDummy.style.display = "none";
}
let sidebarImages = document.querySelectorAll('#LoginDefault_imgUse_TP, #LoginStd_imgMain, [src*="P1New.gif"], [src*="P5New.gif"], [src*="P4New.gif"], .auto-style3');
if (sidebarImages) {
sidebarImages.forEach(x => {
x.remove();
})
}
let headerImage = document.querySelector('[style*="T1_back"], [style*="T1_Std_back"]');
if (headerImage) {
let newHeader = make({
el: 'header',
id: 'nptu-redux-header',
html: "<a class='header-text' href='https://webap.nptu.edu.tw'>🏫 國立屏東大學 (NPTU-Redux)</span>"
})
document.body.prepend(newHeader);
headerImage.remove();
}
let javaNote = document.querySelector('a[href*="java.com"]');
if (javaNote) {
javaNote.remove();
}
let loginButtons = document.querySelectorAll("input[id^=LoginDefault]");
let mainTable = document.querySelector("#TableMain");
if (loginButtons && mainTable) {
let newButtonContainer = make({
el: 'content',
class: 'container',
id: 'button-container'
});
loginButtons.forEach(x => {
let subButtonContainer = make({
el: 'div',
class: 'container'
})
x.style.borderRadius = "10px";
subButtonContainer.appendChild(x);
newButtonContainer.appendChild(subButtonContainer);
})
mainTable.parentNode.replaceChild(newButtonContainer, mainTable);
}
let copyrightDiv = document.evaluate(`//div[contains(., 'Copyright')]`, document).iterateNext();
if (copyrightDiv) {
copyrightDiv.innerHTML = `
<span>
<a href="https://github.com/mt-hack/nptu-redux">GitHub</a>
</span>
<br>
<span style="filter: invert(1)">
Copyright© ${new Date().getFullYear()} by MT.Hack (Still Hsu and its Contributors)
</span>
`;
}
if (isLoginPage(document)) {
let loginForm = document.querySelector('table.style1');
if (!loginForm) {
return;
}
let loginFormContainer = make({
el: 'section',
class: 'container',
id: 'login-container'
});
loginForm = loginForm.cloneElement('div', 'login-form');
loginFormContainer.appendChild(loginForm);
mainElement.appendChild(loginFormContainer);
mainElement.querySelector('table').remove();
let oldLoginBtn = mainElement.querySelector('input[id$=ibtLogin]');
if (oldLoginBtn) {
let captchaField = mainElement.querySelector('[id$="rfvCheckCode"]');
if (captchaField) {
oldLoginBtn.appendAfter(captchaField);
}
let captchaTextField = mainElement.querySelector('[id$="txtCheckCode"]');
if (captchaTextField) {
captchaTextField.autocomplete = "off";
}
let newLoginBtn = createShortcutButton('登入', 'vpn_key', 'colored');
newLoginBtn.addEventListener('click', function(e) {
this.nextElementSibling.click();
})
newLoginBtn.appendBefore(oldLoginBtn);
oldLoginBtn.style.display = 'none';
}
let captchaImage = mainElement.querySelector('#imgCaptcha');
if (captchaImage) {
captchaImage.addEventListener('click', function(e) {
this.src = `../Modules/CaptchaCreator.aspx?${Math.random()}`
})
}
}
}
// post-login frame
if (document.querySelector('body>form') && !isHomepage(document)) {
let contentBody = mainWindow.document.body;
let currentPage = contentBody.querySelector('body>form');
if (!frameElement) {
log('Frame element cannot be detected; assuming unknown page.');
return;
}
log(`Current page: ${currentPage.name}`)
if (currentPage.name == "Form1") {
log('Detected sidebar; returning after style injection...');
return;
}
if (options.locationSelectionPage.includes(currentPage.name)) {
if (options.enableClassroomShortcut) {
createQuickLocationSelection(contentBody);
}
}
if (options.instructorShortcutPage.includes(currentPage.name)) {
if (options.enableInstructorShortcut) {
createInstructorShortcut(contentBody);
}
}
injectHeader(contentBody);
pageCleanup(contentBody, options.isFlexRowWhitelist.includes(currentPage.name));
if (/Main.aspx/g.test(currentPage.action)) {
// Check for the semester change button; if one doesn't exist, likely student
if (!currentPage.querySelector('#CommonHeader_ibtChgSYearSeme') &&
!currentPage.querySelector('input[src*=GST_M]')) {
if (options.enableGradeOnHome) {
injectGradesTable(contentBody);
}
if (options.enableAbsenceOnHome) {
injectAbsenceTable(contentBody);
}
}
}
if (options.enableButtonReplacement) {
buttonReplacement(contentBody);
}
if (options.enableCustomExport) {
printFix(contentBody);
}
frameElement.onload = function() {
if (currentPage.name === "A0433SPage") {
injectNukeAll(contentBody);
}
if (options.tableFixWhitelist.includes(currentPage.name)) {
tableFix(contentBody);
if (options.enableClassroomAutofillOnSelect) {
injectTableAutoFillByClassroomType(contentBody);
}
for (var key in subjectGroups) {
injectTableAutofillBySubjectId(contentBody, key, subjectGroups[key]);
}
} else {
upgradeSelect(contentBody);
}
if (options.enableTableExports) {
options.tableExportWhitelist.forEach(x => {
contentBody.querySelectorAll(`table[id*=${x}]:not(.injected-frame), div[id*=${x}]:not(.injected-frame)`).forEach(table => {
injectTableDownload(table);
})
})
}
if (options.classShortcutPage.includes(currentPage.name)) {
createQuickRoomSelection(contentBody);
}
if (options.enableSurveyAutoFill) {
if (currentPage.name === "A1007SPage" || currentPage.name === "A1014SPage") {
injectFillAllOptions(contentBody);
}
}
if (options.enableCheckInHelper) {
if (currentPage.name === "B4002SPage") {
injectCheckInHelper(contentBody);
}
}
organizeCourseList(contentBody);
setupClipboard(contentBody);
}
}
// it's 2020, who still uses frameset??
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/frameset
if (document.querySelector('frameset')) {
let frames = document.querySelectorAll('frame');
frames.forEach(x => {
let newInlineFrame = document.createElement('iframe');
cloneAttributes(newInlineFrame, x);
x.replaceWith(newInlineFrame);
});
let frameset = document.querySelector('frameset');
let newBody = document.createElement('body');
newBody.classList.add('redux-patched-body');
newBody.innerHTML = frameset.innerHTML;
frameset.replaceWith(newBody);
}
// injection at base window-level, meaning this will be injected at the top frame
// this will allow us to inject top-body-level content, even if the content is opened as a new frame tab
getOrCreateSettingsLayer(window.parent.document.body);
function createQuickRoomSelection(contentBody) {
let table = contentBody.querySelector('[id="P01"]');
let classRoomList = contentBody.querySelector('[id$="ddlROOM_ID"]');
if (table && classRoomList) {
let humanitiesClassrooms = classRoomList.querySelectorAll('option[value^="I1"], option[value^="I2"], option[value^="I3"], option[value^="I4"], option[value^="I5"]');
let buttonContainer = make({ el: 'div', class: 'container', attr: { style: `display: grid;grid-template-columns: 1fr 1fr 1fr;` } });
humanitiesClassrooms.forEach(x => {
let color = x.innerText.match(/大|視聽/g) ? "colored" : "alt";
let shortcutButton = createShortcutButton(x.innerText, undefined, color);
shortcutButton.keyValue = x.value;
shortcutButton.addEventListener('click', function(event) {
classRoomList.value = event.currentTarget.keyValue;
let submitButton = contentBody.querySelector('input[id*=ibtSave]');
if (submitButton) {
submitButton.click();
}
});
buttonContainer.appendChild(shortcutButton);
})
table.appendChild(buttonContainer);
}
}
function injectNukeAll(contentBody) {
let delAllBtn = createShortcutButton('全選刪除', "delete", "colored");
let panel = contentBody.querySelector('[id$="pnlButtonUp"]');
if (panel) {
panel.prepend(delAllBtn);
delAllBtn.addEventListener('click', function(e) {
let chkDels = this.ownerDocument.body.querySelectorAll('[id$="chkDel"]');
chkDels.forEach(x => {
x.checked = true;
})
this.ownerDocument.body.querySelector('input[name*=Delete]').click();
})
}
}
function upgradeSelect(contentBody) {
function makeInputContainer() {
return make({
el: 'div',
class: 'mdl-textfield mdl-js-textfield mdl-textfield--floating-label',
id: 'material-select',
attr: {
style: 'width: auto;'
}
});
}
contentBody.querySelectorAll('select, input[type="text"]').forEach(selectNode => {
if (selectNode.style.opacity === "0" || selectNode.style.visibility === 'hidden' || selectNode.style.display === 'none' ||
selectNode.parentNode.classList.contains('is-upgraded')) {
return;
}
// stop hardcoding width reeeeeeee
selectNode.style.width = "auto";
let selectContainer = makeInputContainer();
let selectNodeParent = selectNode.parentNode;
let previousSibling = _try(() => selectNode.previousSibling.data.trim()) || selectNode.previousElementSibling || selectNode.parentNode.previousElementSibling
selectNode.classList.add('mdl-textfield__input');
selectNodeParent.replaceChild(selectContainer, selectNode);
selectContainer.appendChild(selectNode);
if (previousSibling && previousSibling.innerText) {
let placeholderText = make({
el: 'label',
text: previousSibling.innerText.replace(/[::]/g, ''),
class: 'mdl-textfield__label'
})
selectContainer.appendChild(placeholderText);
}
componentHandler.upgradeElement(selectContainer);
})
}
function injectCheckInHelper(contentBody) {
let tabs = contentBody.querySelector('[id*=htbMenu]');
let punchInField = contentBody.querySelector('input[id*=txtPUNCH_TM]');
let tabsParentNode = tabs.parentNode;
if (!tabs || !punchInField) {
return;
}
let toolsContainer = make({
el: 'div',
class: 'container',
id: 'tools-container'
});
let toolsHeader = createHeader("常用工具 Check-in Helper", "info");
let buttonsContainer = make({
el: 'div',
class: 'help container',
id: 'help-btn-container'
});
let insertTodayButton = createShortcutButton("今天", undefined, "alt");
insertTodayButton.addEventListener('click', () => {
let dateField = contentBody.querySelector('input[id*=txtPUNCH_DT]');
if (dateField) {
dateField.value = getChineseYear();
}
})
let timeHelperContainer = makeGenericContainer();
timeHelperContainer.appendChild(makeChipText("插入常用時間"));
timeHelperContainer.appendChild(insertTodayButton);
timeHelperContainer.appendChild(timeButtonFactory(contentBody, 8, 0));
timeHelperContainer.appendChild(timeButtonFactory(contentBody, 10, 0));
timeHelperContainer.appendChild(timeButtonFactory(contentBody, 12, 0));
timeHelperContainer.appendChild(timeButtonFactory(contentBody, 13, 30));
timeHelperContainer.appendChild(timeButtonFactory(contentBody, 15, 30));
timeHelperContainer.appendChild(timeButtonFactory(contentBody, 17, 30));
buttonsContainer.appendChild(timeHelperContainer);
let lateCheckinContainer = makeGenericContainer();
lateCheckinContainer.appendChild(makeChipText("插入補打卡原因"));
lateCheckinContainer.appendChild(excuseFactory(contentBody, ""));
lateCheckinContainer.appendChild(excuseFactory(contentBody, "工作繁忙"));
lateCheckinContainer.appendChild(excuseFactory(contentBody, "忘記"));
buttonsContainer.appendChild(lateCheckinContainer);
let workDescriptions = GM_getValue(optionIds.WORK_DESCRIPTIONS, []);
if (workDescriptions.length !== 0) {
let workDescriptionContainer = makeGenericContainer();
workDescriptionContainer.appendChild(makeChipText("插入工作內容"));
workDescriptions.forEach(x => {
workDescriptionContainer.appendChild(workDescriptionButtonFactory(contentBody, x));
})
buttonsContainer.appendChild(workDescriptionContainer);
}
toolsContainer.appendChild(toolsHeader);
toolsContainer.appendChild(buttonsContainer);
toolsContainer.appendAfter(tabsParentNode);
}
function getChineseYear(date = undefined) {
date = date === undefined ? new Date() : date;
return `${date.getFullYear() - 1911}/${(date.getMonth() + 1).pad(2)}/${date.getDate().pad(2)}`;
}
function makeChipText(text) {
return make({
el: 'span',
class: 'mdl-chip',
html: `<span class='mdl-chip__text'>${text}</span>`
});
}
function makeGenericContainer() {
return make({
el: 'div',
attr: {
style: "display: flex; align-items: center;"
}
})
}
function workDescriptionButtonFactory(body, desc) {
let workDescButton = createShortcutButton(desc, undefined, "alt");
workDescButton.addEventListener('click', () => {
let workDescField = body.querySelector('input[id*=txtJOB_NOTES]');
if (workDescField) {
workDescField.value = desc
}
})
return workDescButton;
}
function excuseFactory(body, excuse) {
let excuseDescriptor = excuse.match(/[\u200B-\u200D\uFEFF]/g) ? "插入空白字元" : excuse;
let excuseButton = createShortcutButton(excuseDescriptor, undefined, "alt");
excuseButton.addEventListener('click', () => {
let excuseField = body.querySelector('input[id*=txtFILL_NOTES]');
if (excuseField) {
excuseField.value = excuse
}
})
return excuseButton;
}
function timeButtonFactory(body, hour, min) {
let timeButton = createShortcutButton(`${hour.pad(2)}:${min.pad(2)}`, undefined, "alt");
timeButton.addEventListener('click', () => {
let timePunch = body.querySelector('input[id*=txtPUNCH_TM]');
if (timePunch) {
let randomMin = Math.floor(Math.random() * Math.floor(10));
let newMin = Math.random() > 0.5 && (min - randomMin) > 0 ? min - randomMin : min + randomMin;
newMin = newMin < 60 ? newMin : 59;
timePunch.value = `${hour.pad(2)}:${newMin.pad(2)}`;
}
})
return timeButton;
}
function injectFillAllOptions(contentBody) {
let surveyAnswerInputs = contentBody.querySelectorAll("input[id*='rblANSWER']");
let surveyTables = contentBody.querySelectorAll('table[id$="dgQUESTION"]')
if (surveyAnswerInputs.length != 0 && surveyTables.length != 0) {
let buttonContainer = make({
el: "div",
class: "container"
})
let stronglyAgreeBtn = createShortcutButton("非常同意");
let agreeBtn = createShortcutButton("同意");
let neutralBtn = createShortcutButton("普通");
let disagreeBtn = createShortcutButton("不同意");
let stronglyDisagreeBtn = createShortcutButton("很不同意");
let notApplicibleBtn = createShortcutButton("不適合反應");
stronglyAgreeBtn.addEventListener("click", () => {
checkAllInput(surveyTables, "rblANSWER_0")
});
agreeBtn.addEventListener("click", () => {
checkAllInput(surveyTables, "rblANSWER_1")
});
neutralBtn.addEventListener("click", () => {
checkAllInput(surveyTables, "rblANSWER_2")
});
disagreeBtn.addEventListener("click", () => {
checkAllInput(surveyTables, "rblANSWER_3")
});
stronglyDisagreeBtn.addEventListener("click", () => {
checkAllInput(surveyTables, "rblANSWER_4")
});
notApplicibleBtn.addEventListener("click", () => {
checkAllInput(surveyTables, "rblANSWER_5")
});
buttonContainer.appendChild(stronglyAgreeBtn);
buttonContainer.appendChild(agreeBtn);
buttonContainer.appendChild(neutralBtn);
buttonContainer.appendChild(disagreeBtn);
buttonContainer.appendChild(stronglyDisagreeBtn);
buttonContainer.appendChild(notApplicibleBtn);
surveyTables[0].parentNode.prepend(buttonContainer);
}
}
function checkAllInput(surveyTables, id) {
for (let index = 1; index < surveyTables.length; index++) {
let table = surveyTables[index];
table.querySelectorAll(`input[id*=\"${id}\"]`).forEach(x => {
x.checked = true
});
}
}
// todo: should refactor when possible - it's a mess to navigate through
function injectHeader(contentBody) {
let oldInnerHeader = contentBody.querySelector('.TableCommonHeader');
if (!oldInnerHeader) {
return;
}
let oldHeader = oldInnerHeader.parentNode.parentNode;
let newHeaderHtml = `<div class="top header container"><div class="alt buttons container left">`;
let oldHome = contentBody.querySelector('#CommonHeader_ibtBackHome');
if (oldHome) {
newHeaderHtml += `
<span for="home-button" class="mdl-tooltip mdl-tooltip--large">首頁</span>
<label id="home-button" for=${oldHome.id} class='btn hoverable' onclick='toggleOverlay(this.getRootNode()); this.nextElementSibling.click();'>home</label>
<input id=${oldHome.id} src=${emptyImage} style='display: none;' value='' type="image" name=${oldHome.name} alt=${oldHome.alt} title=${oldHome.title}>`;
}
newHeaderHtml += `</div><div class="sub container" id="module-info">`;
let moduleName = contentBody.querySelector('#CommonHeader_lblModule')
if (moduleName) {
newHeaderHtml += `
<div>
<i class="material-icons">dashboard</i>
<div class="hoverable" id="page-name">
${moduleName.innerText}
</div>
</div>`;
}
let semesterElement = contentBody.querySelector('#CommonHeader_lblYSC');
if (semesterElement) {
let semesterName = semesterElement.innerText.replace(/[::]/g, '');
let oldSemSwitch = contentBody.querySelector('#CommonHeader_ibtChgSYearSeme');
if (semesterName) {
newHeaderHtml += `<div><i class="material-icons">event</i>`;
}
if (oldSemSwitch) {
newHeaderHtml += `
<span for="semester-name" class="mdl-tooltip mdl-tooltip--large">切換學期</span>
<label id="semester-name" class="text clickable" onclick='this.nextElementSibling.click();'>${semesterName}</label>
<input id=${oldSemSwitch.id} src=${emptyImage} style='display: none;' value='' type="image" alt=${oldSemSwitch.name} name=${oldSemSwitch.name} title=${oldSemSwitch.title}>`;
} else {
newHeaderHtml += `<div class="hoverable" id="semester-name">${semesterName}</div>`;
}
newHeaderHtml += `</div>`;
}
newHeaderHtml += `</div><div class="sub container" id="user-info">`;
let loginName = contentBody.querySelector('#CommonHeader_lblName');
if (loginName) {
newHeaderHtml += `
<div>
<i class="material-icons">person</i>
<div class="hoverable" id="user-name">
${loginName.innerText}
</div>
</div>`;
}
let onlineUsers = contentBody.querySelector('.CommomHeadstyle2 font');
if (onlineUsers) {
newHeaderHtml += `
<div>
<i class="material-icons">people</i>
<div class="hoverable" id="user-count">
${onlineUsers.innerText}
</div>
</div>`;
}
newHeaderHtml += `</div><div class="alt buttons container right">`;
let oldPwdBtn = contentBody.querySelector('#CommonHeader_ibtChgPwd');
if (oldPwdBtn) {
newHeaderHtml += `
<span for="change-pw-button" class="mdl-tooltip mdl-tooltip--large">更改密碼</span>
<label for=${oldPwdBtn.id} id="change-pw-button" class='btn hoverable' onclick='this.nextElementSibling.click();'>lock</label>
<input id=${oldPwdBtn.id} src=${emptyImage} style='display: none;' value='' type="image" alt=${oldPwdBtn.name} name=${oldPwdBtn.name} title=${oldPwdBtn.title}>`;
}
let oldLogout = contentBody.querySelector('#CommonHeader_ibtLogOut');
if (oldLogout) {
newHeaderHtml += `
<span for="logout-button" class="mdl-tooltip mdl-tooltip--large">登出</span>
<label for=${oldLogout.id} id="logout-button" class='btn hoverable' onclick='this.nextElementSibling.click();'>exit_to_app</label>
<input id=${oldLogout.id} src=${emptyImage} style='display: none;' value='' type="image" alt=${oldLogout.name} name=${oldLogout.name} title=${oldLogout.title}>`;
}
newHeaderHtml += `</div></div>`;
let newHeader = make({
el: "header",
html: newHeaderHtml,
class: 'redux-header'
});
// For some reason they're using this for print detection? What the hell guys
let textUsedDummy = contentBody.querySelector('#CommonHeader_txtUsed');
if (textUsedDummy) {
textUsedDummy.style.opacity = 0;
textUsedDummy.style.position = 'absolute';
textUsedDummy.style.left = '-1px';
textUsedDummy.style.top = '-1px';
newHeader.appendChild(textUsedDummy);
}
let mainForm = mainWindow.document.body.querySelector('body>form');
let righthandButtons = newHeader.querySelector('.alt.buttons.container.right');
let settingsTooltip = make({
el: 'span',
text: 'Redux 設定',
attr: {
for: 'settings-button'
},
class: 'mdl-tooltip mdl-tooltip--large'
});
let settingsBtn = make({
el: 'label',
id: 'settings-button',
class: 'btn hoverable',
text: 'settings'
});
settingsBtn.addEventListener('click', function() {
let settingsOverlay = getOrCreateSettingsLayer(window.parent.document.body);
toggleVisibility(settingsOverlay);
})
righthandButtons.prepend(settingsTooltip);
righthandButtons.prepend(settingsBtn);
componentHandler.upgradeElement(newHeader);
mainForm.prepend(newHeader);
oldHeader.remove();
}
// Replaces each MainBody (td) with a div
function pageCleanup(contentBody, shouldRenderInRows) {
// #region Page Cleanup
let mainTable = contentBody.querySelector('.TableDefault');
if (!mainTable) {
log('TableDefault does not exist, skipping page cleanup.');
return;
}
let mainBodies = contentBody.querySelectorAll('.MainBody');
let mainForm = contentBody.querySelector('body>form');
let mainDiv = make({
el: 'div',
class: 'main container',
});
let printBtns = contentBody.querySelectorAll('[id*=hylPrint]');
let menuElements = [];
let elementDiv = null;
if (shouldRenderInRows) {
elementDiv = make({
el: 'div',
class: 'menu container'
});
}
let tables = contentBody.querySelectorAll('table');
tables.forEach(x => {
x.style.width = null;
x.style.height = null;
})
mainBodies.forEach(element => {
if (!isSafeToDelete(element)) {
// identifier for menu tabs
if (!element.querySelector('td.UnUse')) {
if (!shouldRenderInRows) {
elementDiv = make({
el: 'div',
class: 'menu container',
html: element.innerHTML
});
} else {
elementDiv.innerHTML += element.innerHTML;
}
mainDiv.appendChild(elementDiv);
} else {
menuElements.push(element.innerHTML);
}
}
});
if (elementDiv) {
menuElements.forEach(element => {
elementDiv.insertAdjacentHTML('afterbegin', element);
});
if (!elementDiv.querySelector('[id*=hylPrint]')) {
printBtns.forEach(x => {
elementDiv.appendChild(x);
});
}
}
mainForm.replaceChild(mainDiv, mainTable);
// #endregion
let infoDiv = contentBody.querySelector('.main .menu');
if (infoDiv) {
infoDiv.classList.add('information');
}
let oldAnnounceHeader = contentBody.querySelector("img[src*='Images/HotNews/Hotnew.gif']");
if (oldAnnounceHeader) {
let newAnnounceHeader = createHeader('系統公告 Announcements', 'speaker_notes');
oldAnnounceHeader.parentNode.replaceChild(newAnnounceHeader, oldAnnounceHeader);
}
let oldHelpPanel = contentBody.querySelector('#TableHelp');
if (oldHelpPanel) {
let helpText = oldHelpPanel.innerText.trim();
let newHelpPanel = make({
el: 'div',
class: 'help container'
});
if (helpText.length === 0) {
helpText = '此頁並無提供說明。No description provided.'
}
let helpHeader = createHeader('說明 Information', 'help');
let helpTextContainer = make({
el: 'div',
class: 'text container',
});
helpTextContainer.appendChild(document.createTextNode(helpText));
newHelpPanel.appendChild(helpHeader);
newHelpPanel.appendChild(helpTextContainer);
oldHelpPanel.parentNode.replaceChild(newHelpPanel, oldHelpPanel);
}
let dateInputFields = contentBody.querySelectorAll('input[id$=txtEND_DT], input[id$=txtBEGIN_DT]');
dateInputFields.forEach(x => {
x.autocomplete = "off";
})
}
function buttonReplacement(contentBody) {