99// ==/UserScript==
1010
1111( function ( ) {
12- "use strict" ;
13-
14- // Function to clear existing content
15- function clearContent ( ) {
16- const contentDiv = document . querySelector ( ".student-container" ) ;
17- if ( contentDiv ) {
18- contentDiv . innerHTML = "" ; // Clear the content
19- } else {
20- console . error ( "Content container not found." ) ;
12+ "use strict" ;
13+
14+ // Function to clear existing content
15+ function clearContent ( ) {
16+ const contentDiv = document . querySelector ( ".student-container" ) ;
17+ if ( contentDiv ) {
18+ console . log ( "Content container found." ) ;
19+ } else {
20+ console . error ( "Content container not found." ) ;
21+ }
2122 }
22- }
23-
24- // Function to get URL parameters
25- function getUrlParams ( url ) {
26- const match = url . match ( / \/ l e s s o n \/ ( [ ^ ? # / ] + ) / ) ;
27- if ( match ) {
28- const id = match [ 1 ] ;
29- return { id } ;
23+
24+ // Function to get URL parameters
25+ function getUrlParams ( url ) {
26+ const match = url . match ( / \/ l e s s o n \/ ( [ ^ ? # / ] + ) / ) ;
27+ if ( match ) {
28+ const id = match [ 1 ] ;
29+ return { id } ;
30+ }
31+ return { id : null } ; // Return null if ID is not found
3032 }
31- return { id : null } ; // Return null if ID is not found
32- }
33-
34- // Function to display questions and answers
35- function displayQuestionsAndAnswers ( questions , responses ) {
36- const contentDiv = document . querySelector ( ".student-container" ) ;
37- if ( contentDiv ) {
38- questions . forEach ( ( question , index ) => {
39- const questionDiv = document . createElement ( "div" ) ;
40- questionDiv . textContent = `Question: ${ question . key } ` ;
41- contentDiv . appendChild ( questionDiv ) ;
42-
43- const responseDiv = document . createElement ( "div" ) ;
44- responseDiv . textContent = `Response: ${ responses [ index ] } ` ;
45- contentDiv . appendChild ( responseDiv ) ;
46-
47- const separator = document . createElement ( "hr" ) ;
48- contentDiv . appendChild ( separator ) ;
49- } ) ;
50- } else {
51- console . error ( "Content container not found." ) ;
33+
34+ // Function to display questions and answers
35+ function displayQuestionsAndAnswers ( questions , responses ) {
36+ const contentDiv = document . querySelector ( ".student-container" ) ;
37+ if ( contentDiv ) {
38+ questions . forEach ( ( question , index ) => {
39+ const questionDiv = document . createElement ( "div" ) ;
40+ questionDiv . textContent = `Question: ${ question . key } ` ;
41+ contentDiv . appendChild ( questionDiv ) ;
42+
43+ const responseDiv = document . createElement ( "div" ) ;
44+ responseDiv . textContent = `Response: ${ responses [ index ] } ` ;
45+ contentDiv . appendChild ( responseDiv ) ;
46+
47+ const separator = document . createElement ( "hr" ) ;
48+ contentDiv . appendChild ( separator ) ;
49+ } ) ;
50+ } else {
51+ console . error ( "Content container not found." ) ;
52+ }
5253 }
53- }
54-
55- // Function to fetch responses for questions
56- async function fetchResponses ( questions ) {
57- const responses = [ ] ;
58- for ( const question of questions ) {
59- const questionId = question . key ;
60- try {
61- const responseUrl = `https://cms.quill.org/questions/${ questionId } /responses` ;
62- const response = await fetch ( responseUrl ) ;
63- const responseData = await response . json ( ) ;
64-
65- // Check if responseData is an array of objects
66- if (
67- Array . isArray ( responseData ) &&
68- responseData . length > 0 &&
69- typeof responseData [ 0 ] === "object"
70- ) {
71- // Extract relevant information from each object and concatenate into a string
72- const responseText = responseData . map ( ( obj ) => obj . text ) . join ( ", " ) ;
73- responses . push ( responseText ) ;
74- } else {
54+
55+ // Function to fetch responses for questions
56+ async function fetchResponses ( questions ) {
57+ const responses = [ ] ;
58+ for ( const question of questions ) {
59+ const questionId = question . key ;
60+ try {
61+ const responseUrl = `https://cms.quill.org/questions/${ questionId } /responses` ;
62+ const response = await fetch ( responseUrl ) ;
63+ const responseData = await response . json ( ) ;
64+
65+ // Check if responseData is an array of objects
66+ if (
67+ Array . isArray ( responseData ) &&
68+ responseData . length > 0 &&
69+ typeof responseData [ 0 ] === "object"
70+ ) {
71+ // Extract relevant information from each object and concatenate into a string
72+ const responseText = responseData . map ( ( obj ) => obj . text ) . join ( ", " ) ;
73+ responses . push ( responseText ) ;
74+ } else {
75+ console . error (
76+ `Invalid response data format for question ${ questionId } `
77+ ) ;
78+ responses . push ( "Error fetching response" ) ;
79+ }
80+ } catch ( error ) {
7581 console . error (
76- `Invalid response data format for question ${ questionId } `
82+ `Error fetching responses for question ${ questionId } :` ,
83+ error
7784 ) ;
7885 responses . push ( "Error fetching response" ) ;
7986 }
80- } catch ( error ) {
81- console . error (
82- `Error fetching responses for question ${ questionId } :` ,
83- error
84- ) ;
85- responses . push ( "Error fetching response" ) ;
8687 }
88+ return responses ;
8789 }
88- return responses ;
89- }
90-
91- // Function to start the script
92- async function start ( ) {
93- clearContent ( ) ; // Clear existing content
94- const currentUrl = window . location . href ;
95- const { id } = getUrlParams ( currentUrl ) ;
96- const jsonUrl = `https://www.quill.org/api/v1/lessons/${ id } .json` ;
97-
98- try {
99- const response = await fetch ( jsonUrl ) ;
100- const jsonData = await response . json ( ) ;
101- const questions = jsonData . questions ;
102-
103- const responses = await fetchResponses ( questions ) ;
104- displayQuestionsAndAnswers ( questions , responses ) ;
105- } catch ( error ) {
106- console . error ( "Error fetching JSON data:" , error ) ;
90+
91+ // Function to start the script
92+ async function start ( ) {
93+ clearContent ( ) ; // Clear existing content
94+ const currentUrl = window . location . href ;
95+ const { id } = getUrlParams ( currentUrl ) ;
96+ const jsonUrl = `https://www.quill.org/api/v1/lessons/${ id } .json` ;
97+
98+ try {
99+ const response = await fetch ( jsonUrl ) ;
100+ const jsonData = await response . json ( ) ;
101+ const questions = jsonData . questions ;
102+
103+ const responses = await fetchResponses ( questions ) ;
104+ displayQuestionsAndAnswers ( questions , responses ) ;
105+ } catch ( error ) {
106+ console . error ( "Error fetching JSON data:" , error ) ;
107+ }
107108 }
108- }
109-
110- // Function to initialize the script
111- function initialize ( ) {
112- const quillButton = document . querySelector ( ".quill-button" ) ;
113- if ( quillButton ) {
114- quillButton . addEventListener ( "click" , start ) ;
115- } else {
116- console . error ( "Quill button not found, waiting for it to appear." ) ;
117- waitForQuillButton ( ) ;
109+
110+ // Function to initialize the script
111+ function initialize ( ) {
112+ const quillButton = document . querySelector ( ".quill-button" ) ;
113+ if ( quillButton ) {
114+ quillButton . addEventListener ( "click" , start ) ;
115+ } else {
116+ console . error ( "Quill button not found, waiting for it to appear." ) ;
117+ waitForQuillButton ( ) ;
118+ }
118119 }
119- }
120-
121- // Function to wait for the Quill button
122- function waitForQuillButton ( ) {
123- const observer = new MutationObserver ( ( mutations ) => {
124- mutations . forEach ( ( mutation ) => {
125- const quillButton = document . querySelector ( ".quill-button" ) ;
126- if ( quillButton ) {
127- observer . disconnect ( ) ;
128- quillButton . addEventListener ( "click" , start ) ;
129- console . log ( "Quill button found and event listener attached." ) ;
130- }
120+
121+ // Function to wait for the Quill button
122+ function waitForQuillButton ( ) {
123+ const observer = new MutationObserver ( ( mutations ) => {
124+ mutations . forEach ( ( mutation ) => {
125+ const quillButton = document . querySelector ( ".quill-button" ) ;
126+ if ( quillButton ) {
127+ observer . disconnect ( ) ;
128+ quillButton . addEventListener ( "click" , start ) ;
129+ console . log ( "Quill button found and event listener attached." ) ;
130+ }
131+ } ) ;
131132 } ) ;
132- } ) ;
133-
134- observer . observe ( document . body , { childList : true , subtree : true } ) ;
135- }
136-
137- // Call the initialize function
138- initialize ( ) ;
139- } ) ( ) ;
133+
134+ observer . observe ( document . body , { childList : true , subtree : true } ) ;
135+ }
136+
137+ // Call the initialize function
138+ initialize ( ) ;
139+ } ) ( ) ;
140+
0 commit comments