viethung0823 / PTIT Tools

// ==UserScript==
// @namespace   https://openuserjs.org/users/viethung0823
// @name PTIT       Tools
// @version      0.2
// @description  Log answers from question blocks
// @author       You
// @match        https://lms.pttc1.edu.vn/mod/quiz/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=edu.vn
// @grant        GM_setClipboard
// @license MIT

// ==/UserScript==

// =lOpenUserJS==
// @author viethung0823
// ==/0penUserJS==

if (window.location.href.includes("mod/quiz/review.php?attempt")) {
  // Get all question blocks
  const questionBlocks = document.querySelectorAll(".que");

  let logContent = "";

  // Loop over each question block
  Array.from(questionBlocks).forEach((questionBlock) => {

    // Get the question
    const question = questionBlock.querySelector(".qtext p")?.innerText;
    const slugId = question.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/(^-|-$)/g, "");

    // Get the correct answer
    const correctAnswer = questionBlock.querySelector(".rightanswer p")?.innerText;

    // Add the question ID, question and correct answer to the log content
    logContent += `"${slugId}", "${question}", "${correctAnswer}"\n`;
  });

  // Use GM_setClipboard to set the clipboard content
  GM_setClipboard(logContent);

  alert("Content copied to clipboard");
}
else if (window.location.href.includes("mod/quiz/attempt.php?attempt")) {
  // log questions
  const questionBlocks = document.querySelectorAll(".que");
  let logContent = "";

  // Loop over each question block
  Array.from(questionBlocks).forEach((questionBlock, index) => {
    const questionElement = questionBlock.querySelector(".qtext p");
    const question = questionBlock.querySelector(".qtext p")?.innerText;
    const slugId = question.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/(^-|-$)/g, "");
    if (questionElement) {
      // Create a new anchor element
      const link = document.createElement("a");

      // Set the link's properties
      link.textContent = "Check Answer";
      // TODO: update the link to https://ptit-pool-data.vercel.app/question/${encodeURIComponent(slugId)}
      link.href = `https://ptit-pool-data.vercel.app/api/question/${encodeURIComponent(slugId)}`;
      link.style.marginLeft = "10px"; // Add some left margin for spacing

      // Append the link to the question element
      questionElement.appendChild(link);
    }
    const answerElements = questionBlock.querySelectorAll(".answer .d-flex");
    const answers = Array.from(answerElements).map((answerElement) => {
      const answerNumber = answerElement.querySelector(".answernumber").innerText;
      const answerText = answerElement.querySelector(".flex-fill p").innerText;
      return `${answerNumber} ${answerText}`;
    });

    // Add the question ID, question and correct answer to the log content
    logContent += `Question ${index + 1}: ${question}\n\n${answers.join("\n")}\n\n`;
  });
  // Use GM_setClipboard to set the clipboard content
  GM_setClipboard(logContent);

  alert("Content copied to clipboard");
}

// Copy the log content to the clipboard
// let textarea = document.createElement("textarea");
// textarea.value = logContent;
// document.body.appendChild(textarea);
// textarea.select();
// document.execCommand("copy");
// document.body.removeChild(textarea);