-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
206 lines (183 loc) · 6.63 KB
/
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
import { Database } from "./database.js";
let iframeInject;
fetch("iframe-inject.js")
.then(response => response.text())
.then(response => iframeInject = response);
Database.load((stories) => {
renderGraphs(stories.quizzes);
renderQuizLinks(document.getElementById("quizzes"), stories.quizzes)
renderOtherLinks(document.getElementById("three-strikes"), stories.threeStrikes);
renderOtherLinks(document.getElementById("hard-words"), stories.hardWords);
contextMenuMeme(document.getElementsByTagName('a'));
document.getElementById("close").addEventListener('click', () => closeQuiz());
document.getElementById("fullscreen").addEventListener('click', () => enterFullScreen());
document.getElementById("random").addEventListener('click', () => randomChoice());
document.getElementById("fiftyFifty").addEventListener('click', () => fiftyFifty());
window.addEventListener('message', message => {
if (message.data.type === 'quizFinished') {
const input = document.getElementById(message.data.id).querySelector(`input[type='number']`);
input.value = message.data.score;
input.dispatchEvent(new Event('input', {
bubbles: true,
}));
const checkbox = document.getElementById(message.data.id).querySelector(`input[type='checkbox']`)
checkbox.checked = true;
checkbox.dispatchEvent(new Event('input', {
bubbles: true,
}));
}
});
});
function renderGraphs(quizzes) {
const filtered = quizzes.filter(quiz => quiz.complete).reverse();
new roughViz.Bar({
element: '#graphs',
data: {
values: filtered.map(quiz => quiz.score),
labels: filtered.map(quiz => quiz.title),
},
roughness: 0,
margin: {
top: 20,
bottom: 5,
right: 0,
left: 20
}
});
}
function enterFullScreen() {
if (!document.fullscreenElement) {
const quizViewer = document.getElementById("quiz-viewer");
quizViewer.requestFullscreen();
} else {
document.exitFullscreen();
}
}
function openQuiz(id, link) {
const quizViewer = document.getElementById("quiz-viewer");
const quizIframe = quizViewer.querySelector("iframe");
const url = new URL(link);
const embedURL = 'https://riddle-proxy.viethungax-cloudflare.workers.dev' + url.pathname;
quizIframe.src = embedURL;
quizViewer.style.display = 'block';
quizIframe.onload = () => {
quizIframe.contentWindow.postMessage({ script: `window.quizID="${id}"` }, "*");
quizIframe.contentWindow.postMessage({ script: iframeInject }, "*");
};
}
function closeQuiz() {
const quizViewer = document.getElementById("quiz-viewer");
const quizIframe = quizViewer.querySelector("iframe");
quizIframe.src = "data:text/html, <body style='background: white;display: flex; align-items: center;'><h1 style='text-align: center; width: 100%;'>LOADING...</h1></body>";
quizViewer.style.display = 'none';
document.exitFullscreen();
}
function randomChoice() {
const quizViewer = document.getElementById("quiz-viewer");
const quizIframe = quizViewer.querySelector("iframe");
quizIframe.contentWindow.postMessage({ script: `randomChoice()` }, "*");
}
function fiftyFifty() {
const quizViewer = document.getElementById("quiz-viewer");
const quizIframe = quizViewer.querySelector("iframe");
quizIframe.contentWindow.postMessage({ script: `fiftyFifty()` }, "*");
}
function renderQuizRow(id, title, link, complete = false, score = 0) {
const date = /:\s(?<date>[a-z]+\s[0-9]+,\s[0-9]+)/i.exec(title)?.groups?.date;
const name = /:\s(?<name>(?:Morning|Afternoon)\s[a-z\s]+):/i.exec(title)?.groups?.name;
const row = document.createElement("tr");
row.id = id;
const td1 = document.createElement("td");
const label = document.createElement("label");
label.setAttribute("for", `${id}-done`);
const checkbox = document.createElement("input");
checkbox.setAttribute("type", "checkbox");
checkbox.checked = complete;
checkbox.id = `${id}-done`;
checkbox.name = `${id}-done`;
checkbox.oninput = () => { void Database.markQuizComplete(id, checkbox.checked); };
label.appendChild(checkbox);
td1.appendChild(label);
const td2 = document.createElement("td");
const anchor = document.createElement("a");
anchor.href = link;
anchor.target = "_blank";
anchor.innerText = name;
anchor.onclick = (e) => {
e.preventDefault();
openQuiz(id, link);
};
td2.appendChild(anchor);
const td3 = document.createElement("td");
td3.innerText = date;
const td4 = document.createElement("td");
const input = document.createElement("input");
input.setAttribute("type", "number");
input.setAttribute("step", "1");
input.value = score;
input.oninput = debounce(() => Database.setQuizScore(id, parseInt(input.value)), 2000);
td4.appendChild(input);
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
row.appendChild(td4);
return row;
}
function renderQuizLinks(container, quizzes) {
const list = document.createDocumentFragment();
for (const quiz of quizzes) {
const iframeSrc = getIframeSrc(quiz.html_assets[0].data_content);
const quizLink = renderQuizRow(quiz.id, quiz.title, iframeSrc, quiz.complete, quiz.score);
list.appendChild(quizLink);
}
container.innerHTML = '';
container.append(list);
}
function renderOtherLinks(container, quizzes) {
const list = document.createDocumentFragment();
for (const quiz of quizzes) {
const iframeSrc = getIframeSrc(quiz.html_assets[0].data_content);
const quizLink = document.createElement("a");
quizLink.href = iframeSrc;
quizLink.target = "_blank";
quizLink.textContent = quiz.title;
list.appendChild(quizLink);
}
container.innerHTML = '';
container.append(list);
}
function contextMenuMeme(tags) {
const message = 'Use left click!!!';
const warningElement = document.createElement('div');
warningElement.textContent = message;
warningElement.classList.add('context-menu-warning');
for (const tag of tags) {
tag.addEventListener('contextmenu', (() => rightClickWarning(warningElement)));
}
}
let timeoutRef;
function rightClickWarning(warningElement) {
if (timeoutRef) {
clearTimeout(timeoutRef);
}
warningElement.style.left = `${(window.innerWidth) - (1600) / 2}px`;
warningElement.style.top = '100px';
document.body.appendChild(warningElement);
timeoutRef = setTimeout(() => {
document.body.removeChild(warningElement);
}, 3000);
}
function getIframeSrc(htmlContent) {
const regex = /iframe.*src="([^?]+)/;
const match = regex.exec(htmlContent);
return match ? match[1] : '';
}
function debounce(callback, delay) {
let timeoutId;
return function (...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
callback(...args);
}, delay);
};
}