Skip to content

Commit

Permalink
Update code.js
Browse files Browse the repository at this point in the history
  • Loading branch information
MysticalMike60t committed May 16, 2024
1 parent 513ad6d commit f88527c
Showing 1 changed file with 107 additions and 99 deletions.
206 changes: 107 additions & 99 deletions code.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,123 +9,131 @@
// ==/UserScript==

(function () {
"use strict";

// Function to clear existing content
function clearContent() {
const contentDiv = document.querySelector(".student-container");
if (contentDiv) {
contentDiv.innerHTML = ""; // Clear the content
} else {
console.error("Content container not found.");
}
"use strict";

// Function to clear existing content
function clearContent() {
const contentDiv = document.querySelector(".student-container");
if (contentDiv) {
contentDiv.innerHTML = ""; // Clear the content
} else {
console.error("Content container not found.");
}
}

// Function to get URL parameters
function getUrlParams(url) {
// Function to get URL parameters
function getUrlParams(url) {
const match = url.match(/\/lesson\/([^?#/]+)/);
if (match) {
const id = match[1];
return { id };
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(".student-container");
if (contentDiv) {
questions.forEach((question, index) => {
const questionDiv = document.createElement("div");
questionDiv.textContent = `Question: ${question.key}`;
contentDiv.appendChild(questionDiv);

const responseDiv = document.createElement("div");
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 display questions and answers
function displayQuestionsAndAnswers(questions, responses) {
const contentDiv = document.querySelector(".student-container");
if (contentDiv) {
questions.forEach((question, index) => {
const questionDiv = document.createElement("div");
questionDiv.textContent = `Question: ${question.key}`;
contentDiv.appendChild(questionDiv);

const responseDiv = document.createElement("div");
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) {
// 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);
} 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");
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);
} 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 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 initialize the script
function initialize() {
// 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) {
quillButton.addEventListener("click", start);
} else {
console.error("Quill button not found, waiting for it to appear.");
waitForQuillButton();
observer.disconnect();
quillButton.addEventListener("click", start);
console.log("Quill button found and event listener attached.");
}
}

// 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();
observer.observe(document.body, { childList: true, subtree: true });
}

// Call the initialize function
initialize();
})();

0 comments on commit f88527c

Please sign in to comment.