IT/Software/Applications/Browser Extensions/Tampermonkey

From msgwiki
Revision as of 17:20, 9 April 2024 by Itvte (talk | contribs) (Create Tampermonkey page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Tampermonkey is a browser extension for Chrome and Firefox that allows you to install userscripts: Javascript files that execute when a page matches certain criteria. This allows JS files to be written to automate tasks, to add functionality, or to remove elements from a page, along with many other possibilities.

Userscripts

VocabTest.com exporter

This script runs on the domain https://www.vocabtest.com/user_tests_completed.php and adds an element to copy all scores for the logged in student as a CSV file for pasting into LibreOffice Calc.

// ==UserScript==
// @name         VocabTest Exporter
// @namespace    https://msgeducation.com/
// @version      2024-04-09
// @description  Export CSV of VocabTest.com tests
// @author       Micah Henney
// @match        https://www.vocabtest.com/user_tests_completed.php
// @icon         https://www.google.com/s2/favicons?sz=64&domain=vocabtest.com
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';
    const csvFile = [...document.querySelectorAll("div.lh > b")].map(el => {
        const [_, nominator, denominator] = el.nextElementSibling.innerText.match(/(\d+)\/(\d+)/);
        const value = denominator === "20" ? nominator : (parseInt(nominator) / parseInt(denominator) * 20);
        const allFields = ([
            el.innerText,
            value,
            el.nextElementSibling.nextElementSibling.nextElementSibling.innerText.replace(/.* on /, "")
        ]);
        return allFields.join("\t");
    }).join("\n");

    const inputOption = document.createElement("div");
    inputOption.innerHTML = 'Completed tests list detected! Click here to copy a CSV version of the scores to your clipboard for easy pasting into LibreOffice Calc. <button id="downloadCSV">Copy</button>';
    inputOption.style = "background-color: yellow;";
    document.querySelector("div.rh").appendChild(inputOption);
    document.getElementById("downloadCSV").onclick = function() {
        GM_setClipboard("=SUBSTITUTE(PROPER(\"" + document.querySelector("#contentHolder > p > b").innerText + "\"), \"_\", \" \")\nTest\tScore /20\tDate\n" + csvFile);
        this.innerHTML = "Done!";
        setTimeout(() => {this.innerHTML = "Copy";}, 3000);
    };
})();