-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
204 lines (180 loc) · 6.23 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
(() => {
// Utility imports
const {getToken, getUserId} = imports('*').from('./utils/utils.js');
// Feature imports
const CahierDeTexte = {main: imports('main').from('./features/cahierdetexte.js')};
const EmploiDuTemps = {main: imports('main').from('./features/emploidutemps.js')};
const Messagerie = {main: imports('main').from('./features/messagerie.js')};
const Design = imports('*').from('./features/design.js');
const Notes = {main: imports('main').from('./features/notes.js')};
let lastUrl = window.location.href;
/**
* Initializes URL change observer.
*/
function initUrlObserver() {
new MutationObserver(handleUrlChange).observe(document, {
subtree: true,
childList: true
});
}
/**
* Handles URL change events.
*/
function handleUrlChange() {
const url = window.location.href;
if (url !== lastUrl) {
lastUrl = url;
main(1);
removeEdMenuClass();
}
}
/**
* Removes 'ed-menu' CSS class from all menu elements.
*/
function removeEdMenuClass() {
const menuElements = document.querySelectorAll('div.menu.ed-menu');
menuElements.forEach((element) => element.classList.remove('ed-menu'));
}
/**
* Main function to handle different page behaviors based on URL.
* @param {number} num - Identifier for the call instance.
*/
function main(num) {
if (lastUrl.includes('/login')) {
Design.login();
} else {
const token = getToken();
const userId = getUserId();
if (debug)
console.log('[DEBUG]', 'main ' + num, 'ID of the selected user.', userId);
window.onload = Design.main;
Design.main();
if (debug)
console.log('[DEBUG]', 'main ' + num, 'Token and last URL.', {token, lastUrl});
handlePageSpecificChanges(token, userId);
}
}
/**
* Handles page-specific changes based on the current URL.
* @param {string} token - Authentication token.
* @param {string} userId - User ID.
*/
function handlePageSpecificChanges(token, userId) {
if (token && lastUrl.includes('CahierDeTexte')) {
waitForElement('.all-devoirs', () => CahierDeTexte.main(userId, token));
}
if (token && lastUrl.includes('EmploiDuTemps')) {
waitForElement('.dhx_cal_data > div:nth-child(7)', () =>
EmploiDuTemps.main(userId, token)
);
}
if (token && lastUrl.includes('Notes')) {
waitForElement('td.discipline', () => Notes.main(userId, token));
}
if (token && lastUrl.includes('Messagerie')) {
waitForElement('[class *= btn-group] > button:not([class *= dropdown])', () =>
Messagerie.main(userId, token)
);
}
}
/**
* Waits for a specific element to be loaded and then executes the callback.
* @param {string} selector - CSS selector of the element to wait for.
* @param {Function} callback - Callback to execute when the element is found.
*/
function waitForElement(selector, callback) {
document.kmlcWaitForElement(selector).then(() => {
if (document.querySelector('.sidebar:hover')) {
document.getElementById('main-part').classList.add('sidebarhover');
}
callback();
});
}
/**
* Executes the observer for the 'ed-menu' class changes.
* @param {MutationObserver} observer - MutationObserver instance.
*/
function executeEdMenuObserver(observer) {
waitForElement('.menu-bar', () => {
observer.observe(document.querySelector('.menu-bar'), {
characterData: false,
attributes: true,
attributeFilter: ['class'],
childList: true,
subtree: true
});
});
}
// Observer for 'ed-menu' class changes in the navigation bar
const edMenuObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (
mutation.attributeName === 'class' &&
mutation.target.className.includes('ed-menu')
) {
mutation.target.classList.remove('ed-menu');
}
});
});
// Initialize the observer for the URL changes
initUrlObserver();
// Initialize the main function
main(4);
executeEdMenuObserver(edMenuObserver);
// Trash, not executed anymore but still just in case of
// Trash, not executed anymore but still just in case of
// Trash, not executed anymore but still just in case of
/**
* Listens for changes in the loading page and reinitializes when loading is complete.
*/
function loop2() {
if (backdropScript) {
waitForElement(
"div[class *= 'ng-tns'][class *= 'ng-busy']:not([class *= 'backdrop'])",
() => {
backdropScript = false;
new Promise((resolve) => {
new MutationObserver((mutations, observer) => {
mutations.forEach((mutation) => {
mutation.removedNodes.forEach((removedNode) => {
try {
if (
removedNode.getAttribute('class').includes('ng-tns') &&
removedNode.getAttribute('class').includes('ng-busy') &&
!removedNode.getAttribute('class').includes('backdrop')
) {
observer.disconnect();
resolve();
}
} catch (e) {
// Handle error silently
}
});
});
}).observe(document.body, {
subtree: true,
childList: true
});
}).then(() => {
backdropScript = true;
main(3);
loop2();
});
}
);
}
}
exports({main}).to('./main.js');
})();
// Listener for runtime messages from the Chrome extension
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log(message, sender);
if (message.action === 'getCredential') {
sendResponse({token: getToken(), id: window.location.pathname.split('/')[2]});
}
});
document.addEventListener('kmlcIsolatedMainCom', function (data) {
const main = imports('main').from('./main.js');
const newToken = data.detail;
main(5);
});