Save contents of unsubmitted Google Classroom question responses

Prevents unsubmitted work in question assignments from being lost after closing the tab or browser

目前為 2025-05-28 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Save contents of unsubmitted Google Classroom question responses
// @namespace   Violentmonkey Scripts
// @match       *://classroom.google.com/*
// @grant GM_getValue
// @grant GM_setValue
// @version     1.2
// @author      CyrilSLi
// @description Prevents unsubmitted work in question assignments from being lost after closing the tab or browser
// @license     MIT
// ==/UserScript==

const spanText = "Your answer";
var textarea, span;
const retries = 100;
var retry = 0, retryTimeout = 0;
function findEls() {
    textarea = [...document.getElementsByTagName("textarea")].filter(
        t => t.placeholder === "Type your answer" && window.getComputedStyle(t).visibility !== "hidden"
    )[0];
    span = [...document.getElementsByTagName("span")].filter(
        s => s.textContent.startsWith(spanText) && window.getComputedStyle(s).visibility !== "hidden"
    )[0];

    if (textarea == null || span == null) {
        retryTimeout = setTimeout(findEls, 200);
        retry++;
        if (retry === retries) {
            console.error("Question assignment not found on page.");
            clearTimeout(retryTimeout);
            throw new Error();
        }
    } else {
        console.log("Found question elements.");
        saveQuestion();
        clearTimeout(retryTimeout);
    }
}

var pageURL = "", pageID = "";
const pageRegEx = new RegExp("\/c\/[A-Z0-9a-z]+?\/sa\/[A-Z0-9a-z]+");
const observer = new MutationObserver((muts) => {
    if (document.location.pathname !== pageURL) {
        pageURL = document.location.pathname;
        if (pageRegEx.test(document.location)) {
            removeEventListener("input", areaInput);
            clearTimeout(retryTimeout);
            console.log("Found question assignment.");
            retry = 0;
            pageID = document.location.pathname.match(pageRegEx)[0];
            setTimeout(findEls, 0);
        }
    }
});
observer.observe(document.body, {
    attributes: true
});

function setSpanTime() {
    span.textContent = `${spanText} (last updated ${new Date().toLocaleTimeString("en-US", { hour12: false })})`;
}
function areaInput(ev) {
    GM_setValue(pageID, textarea.value);
    setSpanTime();
}
function saveQuestion(ev) {
    textarea.addEventListener("input", areaInput);
    textarea.value = GM_getValue(pageID, "");
    refreshEls();
}
function refreshEls() {
    rows = 1;
    if (!(textarea.scrollHeight || textarea.clientHeight)) {
        setTimeout(refreshEls, 200);
        return;
    }
    while (textarea.scrollHeight > textarea.clientHeight) {
        textarea.setAttribute("rows", rows++);
    }
    setTimeout(setSpanTime, 1000);
}