This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.user.js
98 lines (85 loc) · 2.66 KB
/
helper.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
// ==UserScript==
// @name quiz helper
// @namespace https://github.com/0guanhua0/pmp-quiz-helper
// @version 1.0
// @description quiz helper for ucertify Project Management Professional (PMP)
// @author guanhua
// @match *://www.ucertify.com/*
// @require https://raw.githubusercontent.com/0guanhua0/pmp-quiz-helper/main/quiz.js
// ==/UserScript==
(function () {
"use strict";
let alreadyRun = false;
function normalizeText(text) {
console.log("Normalizing text:", text);
return text
.replace(/’/g, "'")
.replace(/–/g, "-")
.replace(/“/g, '"')
.replace(/”/g, '"');
}
// find a quiz key that matches part of the question
function matchKey(question, quiz) {
for (let key in quiz) {
if (question.includes(key)) {
console.log("Found matching key:", key);
return key;
}
}
console.log("No matching key found");
return null;
}
// Main logic to run quiz helper
function runHelper() {
if (alreadyRun) {
console.log("Script already ran, skipping execution");
return;
}
alreadyRun = true;
const questionElement = document.querySelector(
'[data-itemtype="question"]',
);
if (!questionElement) {
console.log("Question element not found");
return;
}
let question = questionElement.innerText.trim();
question = normalizeText(question);
// Find a quiz key that matches part of the question
const matchingKey = matchKey(question, quiz);
if (!matchingKey) {
console.log("No matching question in quiz");
return;
}
const ans = quiz[matchingKey];
const elements = document.querySelectorAll("#item_answer seq");
ans.forEach((value) => {
for (let element of elements) {
let text = element.innerText.trim();
text = normalizeText(text);
if (text === value) {
element.style.backgroundColor = "#00ff00";
console.log("Highlight answer:", value);
break;
}
}
});
}
// Observe DOM changes
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
alreadyRun = false; // Reset the alreadyRun flag
console.log("New content loaded, running quiz logic");
runHelper();
}
});
});
// Start observing the body for DOM changes
observer.observe(document.body, { childList: true, subtree: true });
// Run the script when the page is loaded
window.addEventListener("load", function () {
console.log("Page loaded, running quiz logic");
runHelper();
});
})();