Gradescope assignment expander

Expands programming exercises with keyboard

La data de 06-10-2022. Vezi ultima versiune.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Gradescope assignment expander
// @namespace    https://gist.github.com/isaacl/86d5121ede0eeac3eb6a4016555ad70e
// @version      0.1.2
// @description  Expands programming exercises with keyboard
// @author       Isaac Levy
// @match        *://*.gradescope.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

/* jshint esversion: 6 */
(function() {
    'use strict';
    let expanded = 0;
    let allToggles = []
    const UP_CODE = 38;
    const DOWN_CODE = 40;
    const X_CODE = 88; // Cycle through.
    const ALL_CODES = [UP_CODE, DOWN_CODE, X_CODE];

    document.addEventListener('keydown', (e) => {
        const code = e.keyCode;
        if (!ALL_CODES.includes(code)) return;
        // Cache toggles query.
        if (allToggles.length === 0) {
            allToggles = document.querySelectorAll('button.fileViewerHeader--toggleButton');
        }

        const numToggles = allToggles.length;
        if (code === DOWN_CODE && expanded < numToggles - 1) {
            expanded++;
        } else if (code === UP_CODE && expanded > 0) {
            expanded--;
        } else if (code === X_CODE) {
            if (e.shiftKey) {
                expanded = (expanded - 1 + numToggles) % numToggles;
            } else {
                expanded = (expanded + 1) % numToggles;
            }
        }

        allToggles.forEach((e, i) => {
            const shouldExpand = expanded === i;
            const isExpanded = e.getAttribute('aria-expanded') === 'true';
            if (shouldExpand !== isExpanded) e.click();
        });
    });
})();