-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.js
152 lines (137 loc) · 4.88 KB
/
code.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
// ==UserScript==
// @name Quill.org Cheat
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Get answers for Quill.org
// @author Caden Finkelstein
// @match https://www.quill.org/connect/*
// @grant none
// ==/UserScript==
(function () {
"use strict";
// Function to clear existing content
function clearContent() {
const contentDiv = document.querySelector("main");
const styles = document.createElement("style");
styles.innerText = `
.questions {
color: red;
background: black;
z-index: 99;
position: absolute;
}
`
document.body.appendChild(styles);
if (contentDiv) {
console.log("Content container found.");
} else {
console.error("Content container not found.");
}
}
// Function to get URL parameters
function getUrlParams(url) {
const match = url.match(/\/lesson\/([^?#/]+)/);
if (match) {
const id = match[1];
return { id };
}
return { id: null }; // Return null if ID is not found
}
// Function to display questions and answers
function displayQuestionsAndAnswers(questions, responses) {
const contentDiv = document.querySelector("main");
if (contentDiv) {
questions.forEach((question, index) => {
const questionDiv = document.createElement("div");
questionDiv.className = "question";
questionDiv.textContent = `Question: ${question.key}`;
contentDiv.appendChild(questionDiv);
const responseDiv = document.createElement("div");
responseDiv.className="answer";
responseDiv.textContent = `Response: ${responses[index]}`;
contentDiv.appendChild(responseDiv);
const separator = document.createElement("hr");
contentDiv.appendChild(separator);
});
} else {
console.error("Content container not found.");
}
}
// Function to fetch responses for questions
async function fetchResponses(questions) {
const responses = [];
for (const question of questions) {
const questionId = question.key;
try {
const responseUrl = `https://cms.quill.org/questions/${questionId}/responses`;
const response = await fetch(responseUrl);
const responseData = await response.json();
// Check if responseData is an array of objects
if (
Array.isArray(responseData) &&
responseData.length > 0 &&
typeof responseData[0] === "object"
) {
// Extract relevant information from each object and concatenate into a string
// const responseText = responseData.map((obj) => obj.text).join(", ");
// responses.push(responseText);
responses.push(responseData[0].text);
} else {
console.error(
`Invalid response data format for question ${questionId}`
);
responses.push("Error fetching response");
}
} catch (error) {
console.error(
`Error fetching responses for question ${questionId}:`,
error
);
responses.push("Error fetching response");
}
}
return responses;
}
// Function to start the script
async function start() {
clearContent(); // Clear existing content
const currentUrl = window.location.href;
const { id } = getUrlParams(currentUrl);
const jsonUrl = `https://www.quill.org/api/v1/lessons/${id}.json`;
try {
const response = await fetch(jsonUrl);
const jsonData = await response.json();
const questions = jsonData.questions;
const responses = await fetchResponses(questions);
displayQuestionsAndAnswers(questions, responses);
} catch (error) {
console.error("Error fetching JSON data:", error);
}
}
// Function to initialize the script
function initialize() {
const quillButton = document.querySelector(".quill-button");
if (quillButton) {
quillButton.addEventListener("click", start);
} else {
console.error("Quill button not found, waiting for it to appear.");
waitForQuillButton();
}
}
// Function to wait for the Quill button
function waitForQuillButton() {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
const quillButton = document.querySelector(".quill-button");
if (quillButton) {
observer.disconnect();
quillButton.addEventListener("click", start);
console.log("Quill button found and event listener attached.");
}
});
});
observer.observe(document.body, { childList: true, subtree: true });
}
// Call the initialize function
initialize();
})();