Skip to content

Instantly share code, notes, and snippets.

@yshalsager
Last active February 26, 2023 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yshalsager/a688dd2b31722a4a8c3c6489479cddf2 to your computer and use it in GitHub Desktop.
Save yshalsager/a688dd2b31722a4a8c3c6489479cddf2 to your computer and use it in GitHub Desktop.
Binaa Manhaji Download Questions as CSV
// ==UserScript==
// @name Binaa Manhaji Download Questions as CSV
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds a download button to binaamanhaji quiz pages to get all questions as CSV file, so they can be imported to Anki.
// @author yshalsager
// @match *://*.binaamanhaji.com/usercontrol/excercise?lesson_id=*
// @match *://*.binaamanhaji.com/usercontrol/exam*
// @icon https://www.google.com/s2/favicons?sz=64&domain=binaamanhaji.com
// @grant none
// ==/UserScript==
"use strict";
function downloadExamQuestions() {
const questions = Array.from(document.querySelectorAll(".the-question"))
.map(question => {
let correctAnswers = "";
const questionAnswers = [];
for (const questionAnswer of question.querySelectorAll('div[role="radio"]')) {
questionAnswers.push(questionAnswer.textContent.trim());
correctAnswers += questionAnswer.parentElement.classList.contains("correct") ? "1" : "0";
}
// const questionAnswers = Array.from(question.querySelectorAll(".q-radio__label")).map(a => a.textContent);
return [
question.querySelector(".text-bold").textContent.trim(),
"2",
correctAnswers.split(/(?!^)/).join(" "),
...questionAnswers,
];
})
.map(i => i.join(","))
.join("\n");
const examTitleEl = document.querySelector(".text-h6");
const downloadExamBtn = document.createElement("a");
downloadExamBtn.setAttribute("class", "q-btn br bg-primary text-white");
downloadExamBtn.setAttribute("href", `data:application/octet-stream,${encodeURIComponent(questions)}`);
downloadExamBtn.setAttribute("download", `${examTitleEl.textContent.trim()}.csv`);
downloadExamBtn.setAttribute("style", "padding: 6px; margin: 0px 8px 0px 8px;");
downloadExamBtn.innerText = "تحميل الاختبار";
examTitleEl.appendChild(downloadExamBtn);
}
setTimeout(() => downloadExamQuestions(), 2500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment