JEE Mains Marks after capturing the answers

This script calculates the marks after you give the captured answers.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @license      MIT
// @name         JEE Mains Marks after capturing the answers 
// @match        https://examinationservices.nic.in/*/KeyChallange/AnswerKey.aspx
// @description  This script calculates the marks after you give the captured answers.
// @version 0.0.1.20220804185237
// @namespace https://greasyfork.org/users/941655
// ==/UserScript==

function parseAnswerKey() {
    let table = document.querySelector('#ctl00_LoginContent_grAnswerKey.table.table-bordered.table-condensed tbody')
    const content = table = table.querySelectorAll('tr:not(tr:nth-child(1))')
    correct_answers = {}
    for (let row of content) {
        let question_id = row.querySelector('span[id$=QuestionNo]').textContent
        let answer = row.querySelector('span[id$=Answer]').textContent
        correct_answers[question_id] = answer
    }
    return correct_answers;
}

/**
 * @param {{id: string; subject: "Chemistry" | "Physics" | "Maths"; section: "A" | "B"; givenAnswer: string}[]}  givenAnswers Description
 */
function calculateMarks(givenAnswers) {
    const correctAnswers = parseAnswerKey();
    let total = 0;
    for (const givenAnswer of givenAnswers) {
        if (!givenAnswer.givenAnswer.includes("Not Attempted")) {
            if (correctAnswers[givenAnswer.id] === givenAnswer.givenAnswer) total += 4;
            else total--;
        }
    }
    alert(`The total marks are ${total}`)
}

const main = () => {

    const mapForm = document.createElement("form");

    const mapInput = document.createElement("input");
    mapInput.type = "text";
    mapInput.name = "answers";
    mapInput.placeholder = 'Paste captured answers';

    const submitBtn = document.createElement("button");
    submitBtn.type = 'submit'
    submitBtn.innerText = "Get marks"

    mapForm.appendChild(mapInput);
    mapForm.appendChild(submitBtn);
    document.body.prepend(mapForm);

    mapForm.onsubmit = e => {
        e.preventDefault();
        try {
            calculateMarks(JSON.parse(mapInput.value));
        } catch (e) {
            alert(e)
        }
    }
}

main()