-
Notifications
You must be signed in to change notification settings - Fork 0
/
NL4.js
360 lines (314 loc) · 16.9 KB
/
NL4.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
console.log('test8');
var sheetId = '1EceUR6V_uozN0fAkYTE_p9NHLIew8OBOI_Ab_10Z490';
var base = `https://docs.google.com/spreadsheets/d/${sheetId}/gviz/tq?`;
const sheetName = 'user-data';
const query = encodeURIComponent('Select *');
const url = `${base}&sheet=${sheetName}&tq=${query}`;
const data = [];
document.addEventListener('DOMContentLoaded', init);
const output = document.querySelector('.output');
// Main init function
function init() {
fetch(url)
.then(res => res.text())
.then(rep => {
const jsonData = JSON.parse(rep.substring(47).slice(0, -2));
const colz = [];
const tr = document.createElement('tr');
// Extract column labels
jsonData.table.cols.forEach((heading) => {
if (heading.label) {
let column = heading.label;
colz.push(column);
const th = document.createElement('th');
th.innerText = column;
tr.appendChild(th);
}
});
// Extract row data
jsonData.table.rows.forEach((rowData) => {
const row = {};
colz.forEach((ele, ind) => {
row[ele] = (rowData.c[ind] != null) ? rowData.c[ind].v : '';
});
data.push(row);
});
processRows(data);
var liste = document.querySelectorAll('.exercice');
console.log("la liste traitee est :", traitementListe(liste));
console.log('la classlist du document est :', document.body.classList.value);
console.log('la classlist du document contient paying :', document.body.classList.value.includes('paying'));
if (!document.body.classList.value.includes('paying')) {
injectHTML(traitementListe(liste));
}
});
}
// Function to hide blurs for premium users
function displayNone() {
var contenairexercice = document.querySelectorAll(".exercice iframe");
for (let i = 0; i < contenairexercice.length; ++i) {
var blur = contenairexercice[i].contentWindow.document.body.querySelectorAll(".blur");
for (let j = 0; j < blur.length; ++j) {
blur[j].style.display = "none";
}
}
}
// Observer for detecting body class changes (e.g., if user becomes premium)
const classChangeCallback = function (mutationsList, observer) {
for (const mutation of mutationsList) {
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
if (document.body.classList.contains('paying')) {
displayNone();
observer.disconnect();
}
}
}
};
const bodyObserver = new MutationObserver(classChangeCallback);
bodyObserver.observe(document.body, { attributes: true });
// Function to start the countdown from inside the iframe
function startCountdownInIframe(iframeDocument, elementId, nextAttemptTime, submitButton, blurElement, outcomeElement) {
const countdownElement = iframeDocument.getElementById(elementId);
if (!countdownElement) {
console.error(`Countdown element with ID ${elementId} not found inside iframe.`);
return;
}
function updateCountdown() {
const now = new Date();
const timeRemaining = nextAttemptTime - now;
if (timeRemaining > 0) {
// Time calculations for hours, minutes, and seconds
const hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
countdownElement.innerHTML = ` ${minutes}m ${seconds}s`;
} else {
countdownElement.innerHTML = "";
clearInterval(countdownInterval);
// Enable the submit button and remove the grey effect
if (submitButton) {
submitButton.disabled = false;
submitButton.style.backgroundColor = ""; // Reset to default
submitButton.style.cursor = ""; // Reset to default
}
// Remove the blur and gosabonner div
if (blurElement) {
blurElement.remove(); // Fully remove the blur element
}
// Remove the color and text-shadow from the outcome element
if (outcomeElement) {
outcomeElement.style.color = ""; // Reset to default
outcomeElement.style.textShadow = ""; // Reset to default
}
}
}
const countdownInterval = setInterval(updateCountdown, 1000);
updateCountdown();
}
// Inject HTML with blur for fiches and exercices
function injectHTML(liste) {
const exercicesAvecBlur = {};
const isblurred = {};
// Inject blur for fiches
var fiche = document.querySelectorAll(".fiche");
for (let i = 0; i < fiche.length; ++i) {
identifiantFiche = fiche[i].id;
if (checkStatus(identifiantFiche)) {
// Check if blur has already been added
if (!fiche[i].querySelector('.blur')) {
fiche[i].insertAdjacentHTML('beforeend',
`<div class="blur">
<div class="gosabonner">Pour voir cette <b>fiche</b> il faut un compte premium 👑.
<br><a target="_parent" class="awhite" href="https://galilee.ac/local/membership/plan.php">
<div class="whitebutton"><b> Nos offres</b></div></a> </div></div>`
);
exercicesAvecBlur[identifiantFiche] = true;
}
}
}
///// FILL THE CORRECTIONS THAT ARE ALREADY DISPLAYED WHEN THE PAGE LOADS
var contenairexercice = document.querySelectorAll(".exercice iframe");
for (let i = 0; i < contenairexercice.length; ++i) {
let iframe = contenairexercice[i];
let identifiantExercice = iframe.parentElement.id;
console.log(`Processing exercise already loaded with ID: ${identifiantExercice}`);
if (checkStatus(identifiantExercice)) {
const iframeDocument = iframe.contentWindow.document;
console.log(`Iframe loaded for exercise ${identifiantExercice}`);
// Fetch the max attempts flag and next attempt time from the iframe
const maxAttemptsReachedElement = iframeDocument.querySelector("#maxAttemptsReached");
const nextAttemptTimeElement = iframeDocument.querySelector("#nextAttemptTime");
const maxAttemptsReached = maxAttemptsReachedElement ? (maxAttemptsReachedElement.innerText === 'true') : false;
const nextAttemptTime = nextAttemptTimeElement ? new Date(nextAttemptTimeElement.innerText) : null;
console.log(`Max Attempts Reached for exercise ${identifiantExercice}: ${maxAttemptsReached}`);
console.log(`Next Attempt Time for exercise ${identifiantExercice}: ${nextAttemptTime}`);
// Disable submit button if user has reached the max attempts
const submitButton = iframeDocument.querySelector("input[type='submit']");
let blurElement = null;
let outcomeElement = null;
if (submitButton && maxAttemptsReached) {
// Check if the button is already disabled
if (!submitButton.disabled) {
submitButton.disabled = true; // Disable the button
submitButton.style.backgroundColor = "grey"; // Apply greyish effect
submitButton.style.cursor = "not-allowed"; // Change cursor to indicate it's disabled
}
// Add a span next to the button for the countdown if not already added
let countdownElement = iframeDocument.querySelector(`#countdown_${identifiantExercice}`);
if (!countdownElement) {
submitButton.insertAdjacentHTML('afterend', `<span id="countdown_${identifiantExercice}" style="margin-left: 10px; color: grey;"></span>`);
console.log(`Added countdown span for exercise ${identifiantExercice}`);
}
// Always get the countdown element
countdownElement = iframeDocument.querySelector(`#countdown_${identifiantExercice}`);
}
// Check for the correction and inject blur only inside the correction
const correction = iframeDocument.body.querySelector('.outcome');
if (correction && maxAttemptsReached && nextAttemptTime) {
// Check if blur has already been added to the correction
if (!correction.querySelector('.blur')) {
console.log(`Inserting blur with countdown for exercise ${identifiantExercice}`);
correction.insertAdjacentHTML('beforeend', `
<div class="blur">
<div class="gosabonner">
Tu as atteint le maximum d'erreurs 😥 Attends <b><span id="countdown_${identifiantExercice}_blur"></span></b>
<br> ou
<a target="_parent" class="awhite" href="https://galilee.ac/local/membership/plan.php">
<div class="whitebutton"><b>Deviens Premium 👑</b></div>
</a>
</div>
</div>
`);
correction.style.color = "transparent";
correction.style.textShadow = "0 0 8px #000";
exercicesAvecBlur[identifiantExercice] = true;
}
// Always assign blurElement and outcomeElement
blurElement = correction.querySelector('.blur');
outcomeElement = correction; // Save the outcome element reference to reset later
// Run the countdown inside the iframe context for the blur
const blurCountdownElementId = `countdown_${identifiantExercice}_blur`;
setTimeout(function () {
startCountdownInIframe(iframeDocument, blurCountdownElementId, nextAttemptTime, submitButton, blurElement, outcomeElement);
}, 500);
}
// Run the countdown inside the iframe context for the submit button
if (maxAttemptsReached && nextAttemptTime) {
const countdownElementId = `countdown_${identifiantExercice}`;
setTimeout(function () {
startCountdownInIframe(iframeDocument, countdownElementId, nextAttemptTime, submitButton, blurElement, outcomeElement);
}, 500);
}
}
}
// Inject blur for exercices that are dynamically loaded
var contenairexercice = document.querySelectorAll(".exercice iframe");
console.log("la liste des containerexercice est ", contenairexercice);
for (let i = 0; i < contenairexercice.length; ++i) {
let iframe = contenairexercice[i];
let identifiantExercice = iframe.parentElement.id;
console.log(`Processing exercise with ID: ${identifiantExercice}`);
if (checkStatus(identifiantExercice)) {
iframe.addEventListener('load', function () {
const iframeDocument = iframe.contentWindow.document;
console.log(`Iframe loaded for exercise ${identifiantExercice}`);
// Fetch the max attempts flag and next attempt time from the iframe
const maxAttemptsReachedElement = iframeDocument.querySelector("#maxAttemptsReached");
const nextAttemptTimeElement = iframeDocument.querySelector("#nextAttemptTime");
const maxAttemptsReached = maxAttemptsReachedElement ? (maxAttemptsReachedElement.innerText === 'true') : false;
const nextAttemptTime = nextAttemptTimeElement ? new Date(nextAttemptTimeElement.innerText) : null;
console.log(`Max Attempts Reached for exercise ${identifiantExercice}: ${maxAttemptsReached}`);
console.log(`Next Attempt Time for exercise ${identifiantExercice}: ${nextAttemptTime}`);
// Disable submit button if user has reached the max attempts
const submitButton = iframeDocument.querySelector("input[type='submit']");
let blurElement = null;
let outcomeElement = null;
if (submitButton && maxAttemptsReached) {
// Check if the button is already disabled
if (!submitButton.disabled) {
submitButton.disabled = true; // Disable the button
submitButton.style.backgroundColor = "grey"; // Apply greyish effect
submitButton.style.cursor = "not-allowed"; // Change cursor to indicate it's disabled
}
// Add a span next to the button for the countdown if not already added
let countdownElement = iframeDocument.querySelector(`#countdown_${identifiantExercice}`);
if (!countdownElement) {
submitButton.insertAdjacentHTML('afterend', `<span id="countdown_${identifiantExercice}" style="margin-left: 10px; color: grey;"></span>`);
console.log(`Added countdown span for exercise ${identifiantExercice}`);
}
// Always get the countdown element
countdownElement = iframeDocument.querySelector(`#countdown_${identifiantExercice}`);
}
// Check for the correction and inject blur only inside the correction
const correction = iframeDocument.body.querySelector('.outcome');
if (correction && maxAttemptsReached && nextAttemptTime) {
// Check if blur has already been added to the correction
if (!correction.querySelector('.blur')) {
console.log(`Inserting blur with countdown for exercise ${identifiantExercice}`);
correction.insertAdjacentHTML('beforeend', `
<div class="blur">
<div class="gosabonner">
Tu as atteint le maximum d'erreurs 😥 Attends <b><span id="countdown_${identifiantExercice}_blur"></span></b>.
<br> ou
<a target="_parent" class="awhite" href="https://galilee.ac/local/membership/plan.php">
<div class="whitebutton"><b>Deviens Premium 👑</b></div>
</a>
</div>
</div>
`);
correction.style.color = "transparent";
correction.style.textShadow = "0 0 8px #000";
exercicesAvecBlur[identifiantExercice] = true;
}
// Always assign blurElement and outcomeElement
blurElement = correction.querySelector('.blur');
outcomeElement = correction; // Save the outcome element reference to reset later
// Run the countdown inside the iframe context for the blur
const blurCountdownElementId = `countdown_${identifiantExercice}_blur`;
setTimeout(function () {
startCountdownInIframe(iframeDocument, blurCountdownElementId, nextAttemptTime, submitButton, blurElement, outcomeElement);
}, 500);
}
// Run the countdown inside the iframe context for the submit button
if (maxAttemptsReached && nextAttemptTime) {
const countdownElementId = `countdown_${identifiantExercice}`;
setTimeout(function () {
startCountdownInIframe(iframeDocument, countdownElementId, nextAttemptTime, submitButton, blurElement, outcomeElement);
}, 500);
}
});
} else {
console.log(`Exercise ${identifiantExercice} is not a paying exercise`);
}
}
}
// Helper functions
function traitementListe(liste) {
var listeTraitee = [];
for (let i = 0; i < liste.length; i++) {
var id = liste[i].id;
if (checkStatus(id) == 1) {
listeTraitee.push(liste[i]);
}
}
return listeTraitee;
}
function checkStatus(id) {
for (let i = 0; i < data.length; i++) {
if (data[i].id == id) {
return data[i].Payant;
}
}
return false;
}
function processRows(json) {
json.forEach((row) => {
const tr = document.createElement('tr');
const keys = Object.keys(row);
keys.forEach((key) => {
const td = document.createElement('td');
td.textContent = row[key];
tr.appendChild(td);
});
});
}