Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] Interactive Question and Answer Sheet Generation #13

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extension/html/question_generation.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<title>Question Generation- EduAid</title>
<link href='https://fonts.googleapis.com/css?family=Inter' rel='stylesheet'>
<link rel="stylesheet" href="../styles/question_generation.css">
<script src="../js/pdf-lib.js"></script>
<script src="../js/download.js"></script>
</head>

<body>
Expand All @@ -14,9 +16,11 @@ <h1>EduAid</h1>

<main>
<h3>Questions have been generated!</h3>
<textarea id="title-input" placeholder="Paste your title here"></textarea>
<div id="button-container">
<button id="view-questions-button">View</button>
<button id="save-button">Save</button>
<button id="answer-button">Answers</button>
</div>
<dialog data-modal>
<ul id="modal-question-list"></ul>
Expand Down
159 changes: 159 additions & 0 deletions extension/js/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.download = factory();
}
}(this, function () {

return function download(data, strFileName, strMimeType) {

var self = window, // this script is only for browsers anyway...
defaultMime = "application/octet-stream", // this default mime also triggers iframe downloads
mimeType = strMimeType || defaultMime,
payload = data,
url = !strFileName && !strMimeType && payload,
anchor = document.createElement("a"),
toString = function(a){return String(a);},
myBlob = (self.Blob || self.MozBlob || self.WebKitBlob || toString),
fileName = strFileName || "download",
blob,
reader;
myBlob= myBlob.call ? myBlob.bind(self) : Blob ;

if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback
payload=[payload, mimeType];
mimeType=payload[0];
payload=payload[1];
}


if(url && url.length< 2048){ // if no filename and no mime, assume a url was passed as the only argument
fileName = url.split("/").pop().split("?")[0];
anchor.href = url; // assign href prop to temp anchor
if(anchor.href.indexOf(url) !== -1){ // if the browser determines that it's a potentially valid url path:
var ajax=new XMLHttpRequest();
ajax.open( "GET", url, true);
ajax.responseType = 'blob';
ajax.onload= function(e){
download(e.target.response, fileName, defaultMime);
};
setTimeout(function(){ ajax.send();}, 0); // allows setting custom ajax headers using the return:
return ajax;
} // end if valid url?
} // end if url?


//go ahead and download dataURLs right away
if(/^data:([\w+-]+\/[\w+.-]+)?[,;]/.test(payload)){

if(payload.length > (1024*1024*1.999) && myBlob !== toString ){
payload=dataUrlToBlob(payload);
mimeType=payload.type || defaultMime;
}else{
return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs:
navigator.msSaveBlob(dataUrlToBlob(payload), fileName) :
saver(payload) ; // everyone else can save dataURLs un-processed
}

}else{//not data url, is it a string with special needs?
if(/([\x80-\xff])/.test(payload)){
var i=0, tempUiArr= new Uint8Array(payload.length), mx=tempUiArr.length;
for(i;i<mx;++i) tempUiArr[i]= payload.charCodeAt(i);
payload=new myBlob([tempUiArr], {type: mimeType});
}
}
blob = payload instanceof myBlob ?
payload :
new myBlob([payload], {type: mimeType}) ;


function dataUrlToBlob(strUrl) {
var parts= strUrl.split(/[:;,]/),
type= parts[1],
decoder= parts[2] == "base64" ? atob : decodeURIComponent,
binData= decoder( parts.pop() ),
mx= binData.length,
i= 0,
uiArr= new Uint8Array(mx);

for(i;i<mx;++i) uiArr[i]= binData.charCodeAt(i);

return new myBlob([uiArr], {type: type});
}

function saver(url, winMode){

if ('download' in anchor) { //html5 A[download]
anchor.href = url;
anchor.setAttribute("download", fileName);
anchor.className = "download-js-link";
anchor.innerHTML = "downloading...";
anchor.style.display = "none";
document.body.appendChild(anchor);
setTimeout(function() {
anchor.click();
document.body.removeChild(anchor);
if(winMode===true){setTimeout(function(){ self.URL.revokeObjectURL(anchor.href);}, 250 );}
}, 66);
return true;
}

// handle non-a[download] safari as best we can:
if(/(Version)\/(\d+)\.(\d+)(?:\.(\d+))?.*Safari\//.test(navigator.userAgent)) {
if(/^data:/.test(url)) url="data:"+url.replace(/^data:([\w\/\-\+]+)/, defaultMime);
if(!window.open(url)){ // popup blocked, offer direct download:
if(confirm("Displaying New Document\n\nUse Save As... to download, then click back to return to this page.")){ location.href=url; }
}
return true;
}

//do iframe dataURL download (old ch+FF):
var f = document.createElement("iframe");
document.body.appendChild(f);

if(!winMode && /^data:/.test(url)){ // force a mime that will download:
url="data:"+url.replace(/^data:([\w\/\-\+]+)/, defaultMime);
}
f.src=url;
setTimeout(function(){ document.body.removeChild(f); }, 333);

}//end saver




if (navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL)
return navigator.msSaveBlob(blob, fileName);
}

if(self.URL){ // simple fast and modern way using Blob and URL:
saver(self.URL.createObjectURL(blob), true);
}else{
// handle non-Blob()+non-URL browsers:
if(typeof blob === "string" || blob.constructor===toString ){
try{
return saver( "data:" + mimeType + ";base64," + self.btoa(blob) );
}catch(y){
return saver( "data:" + mimeType + "," + encodeURIComponent(blob) );
}
}

// Blob but not URL support:
reader=new FileReader();
reader.onload=function(e){
saver(this.result);
};
reader.readAsDataURL(blob);
}
return true;
}; /* end download() */
}));
3 changes: 3 additions & 0 deletions extension/js/pdf-lib.js

Large diffs are not rendered by default.

85 changes: 60 additions & 25 deletions extension/js/question_generation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@

const { PDFDocument } = PDFLib

async function createAnswerKey() {
const titleInput = document.getElementById("title-input").value;
const pdfDoc = await PDFDocument.create()
const page = pdfDoc.addPage([550, 750])
const form = pdfDoc.getForm()
var d = new Date(Date.now());
page.drawText('EduAid generated Quiz for : ' + titleInput, { x: 50, y: 700, size: 20 })
page.drawText('Created On:' + d.toString(),{ x: 50, y: 670, size: 10 })
const qaPairs=JSON.parse(localStorage.getItem("qaPairs"));
var x =10;
var y =30;
var ct = 1;
for (const [question,answer] of Object.entries(qaPairs)){
page.drawText("Q"+ct.toString()+") "+question, { x: 50, y: 600+y,size: 15 })
page.drawText("Answer: ", { x: 50, y: 580+y,size: 15 })
const answerField = form.createTextField('question'+answer)
answerField.setText(answer)
answerField.addToPage(page, { x: 50, y: 540+y,size:10 })
y=y-120;
ct+=1;
}

const pdfBytes = await pdfDoc.save()
const ans = titleInput+ "_answerKey"+".pdf"
download(pdfBytes,ans, "application/pdf");
}

async function createQuestions() {
const titleInput = document.getElementById("title-input").value;
const pdfDoc = await PDFDocument.create()
const page = pdfDoc.addPage([550, 750])
const form = pdfDoc.getForm()
var d = new Date(Date.now());
page.drawText('EduAid generated Quiz for : ' + titleInput, { x: 50, y: 700, size: 20 })
page.drawText('Created On:' + d.toString(),{ x: 50, y: 670, size: 10 })
const qaPairs=JSON.parse(localStorage.getItem("qaPairs"));
var x =10;
var y =30;
var ct = 1;
for (const [question,answer] of Object.entries(qaPairs)){
page.drawText("Q"+ct.toString()+") "+question, { x: 50, y: 600+y,size: 15 })
page.drawText("Answer: ", { x: 50, y: 580+y,size: 15 })
const answerField = form.createTextField('question'+answer)
answerField.setText("")
answerField.addToPage(page, { x: 50, y: 540+y,size:10 })
y=y-120;
ct+=1;
}
const ques = titleInput+".pdf"
const pdfBytes = await pdfDoc.save()
download(pdfBytes,ques, "application/pdf");
}
document.addEventListener("DOMContentLoaded", function(){
const saveButton= document.getElementById("save-button");
const backButton= document.getElementById("back-button");
const answerButton= document.getElementById("answer-button");
const viewQuestionsButton = document.getElementById("view-questions-button");
const qaPairs=JSON.parse(localStorage.getItem("qaPairs"));
const modalClose= document.querySelector("[data-close-modal]");
Expand All @@ -23,31 +79,10 @@ document.addEventListener("DOMContentLoaded", function(){
modal.close();
});
saveButton.addEventListener("click", async function(){
let textContent= "EduAid Generated QnA:\n\n";

for (const [question,answer] of Object.entries(qaPairs)){
textContent+= `Question: ${question}\nAnswer: ${answer}\n\n`;
}
const blob = new Blob([textContent], { type: "text/plain" });

// Create a URL for the Blob
const blobUrl = URL.createObjectURL(blob);

// Create a temporary <a> element to trigger the download
const downloadLink = document.createElement("a");
downloadLink.href = blobUrl;
downloadLink.download = "questions_and_answers.txt";
downloadLink.style.display = "none";

// Append the <a> element to the document
document.body.appendChild(downloadLink);

// Simulate a click on the link to trigger the download
downloadLink.click();

// Clean up: remove the temporary <a> element and revoke the Blob URL
document.body.removeChild(downloadLink);
URL.revokeObjectURL(blobUrl);
createQuestions();
});
answerButton.addEventListener("click", async function(){
createAnswerKey();
});

backButton.addEventListener("click", function(){
Expand Down
4 changes: 3 additions & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"./js/text_input.js",
".js/view_questions.js",
"./js/question_generation.js",
"./assets/aossie_logo.png"
"./assets/aossie_logo.png",
"./js/pdf-lib.js",
"./js/download.js"
],
"matches": ["<all_urls>"]
}
Expand Down