IT/Software/Applications/Browser Extensions/Tampermonkey

From msgwiki
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.

Installing

To install the extension, just go to the corresponding link for the browser (Chrome or Firefox) and install it.

Userscripts have several sources. They can come from a web link, where they can be automatically updated, or they can be created locally where they have to be manually changed.

To create a local userscript, click the Tampermonkey extension and click "Create a new script...", then remove all boilerplate code (Ctrl-A and delete) and replace it with the userscript you've chosen below.

To install from a web link, just go to the link with the extension installed and click the install button.

Userscripts

Here are some useful userscripts. Each one should be installed as a web link if possible, because of automatic updating and because it takes fewer clicks.

VocabTest.com exporter

Weblink

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);
    };
})();