Ivanq / Informatics Task Status

// ==UserScript==
// @name         Informatics Task Status
// @namespace    https://openuserjs.org/users/Ivanq
// @version      0.1
// @description  Add ticks to solved tasks on informatics
// @copyright    2019, Ivanq (https://openuserjs.org/users/Ivanq)
// @author       Ivanq
// @license      MIT
// @match        https://informatics.msk.ru/mod/statements/view*
// @grant        none
// ==/UserScript==

// ==OpenUserJS==
// @author Ivanq
// ==/OpenUserJS==


'use strict';

const userId = document.querySelector("#user_id").innerHTML;

const reload = () => {
    for(const node of document.querySelectorAll(".statements_toc_alpha li > *")) {
        let statusNode = node.querySelector(".status");
        if(!statusNode) {
            statusNode = document.createElement("span");
            statusNode.className = "status";
            node.appendChild(statusNode);
        }

        let problemId;
        if(node.tagName === "STRONG") {
            problemId = document.querySelector("#problem_id").innerHTML;
        } else {
            problemId = node.href.match(/chapterid=(\d+)/)[1];
        }
        (async () => {
            const url = `https://informatics.msk.ru/py/problem/${problemId}/filter-runs?user_id=${userId}&status_id=0&page=0`;
            const res = await (await fetch(url)).json();

            statusNode.innerHTML = res.metadata.count > 0 ? " ✔️" : "";
        })();
    }
};

reload();
setInterval(reload, 10000);