iMathAS Question Code Saver

Enables you to download the source code for iMathAS Questions which are in Editor mode. A PHP file will be saved for the Common Control code and a HTML file will be saved for the Question Text code. Works on MOER, MyOpenMath, and other iMathAS sites.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Advertisement:

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

Advertisement:

// ==UserScript==
// @name         iMathAS Question Code Saver
// @namespace    http://tampermonkey.net/
// @version      2.3
// @description  Enables you to download the source code for iMathAS Questions which are in Editor mode. A PHP file will be saved for the Common Control code and a HTML file will be saved for the Question Text code. Works on MOER, MyOpenMath, and other iMathAS sites.
// @author       Gemini in consulation with Kent Slack
// @match        *://*/course/moddataset.php*
// @grant        unsafeWindow
// @license      GPL-2.0-only
// ==/UserScript==

(function() {
    'use strict';

    function downloadFile(filename, text) {
        const blob = new Blob([text], { type: 'text/plain' });
        const url = window.URL.createObjectURL(blob);
        const element = document.createElement('a');
        element.href = url;
        element.download = filename;
        document.body.appendChild(element);
        element.click();
        window.URL.revokeObjectURL(url);
        element.remove();
    }

    function handleSave() {
        // Force Sync TinyMCE (Visual Editor)
        const tinymce = unsafeWindow.tinyMCE || window.tinyMCE;
        if (tinymce) {
            tinymce.triggerSave();
        }

        // Get ID from URL
        const urlParams = new URLSearchParams(window.location.search);
        const pageId = urlParams.get('id') || 'no_id';

        // Grab textareas
        const areas = document.querySelectorAll('textarea');

        // Logic based on verified indices:
        // Index 1: PHP Code
        // Index 3: HTML / Question Text
        if (areas.length >= 4) {
            const phpRaw = areas[1].value;
            const htmlContent = areas[3].value;

            // Wrap PHP content in tags for syntax highlighting
            const phpWrapped = `<?php\n${phpRaw}\n?>`;

            if (!phpRaw && !htmlContent) {
                alert("The code areas appear to be empty!");
                return;
            }

            // Save PHP (.php) and HTML (.html)
            downloadFile(`${pageId}.php`, phpWrapped);
            downloadFile(`${pageId}.html`, htmlContent);

            console.log(`iMathAS Export Success: ID ${pageId}`);
        } else {
            alert(`Error: Found ${areas.length} textareas. Expected at least 4.`);
        }
    }

    // UI Button - Positioned further down to avoid header bars
    const btn = document.createElement('button');
    btn.innerHTML = '💾 Export Question Code';
    btn.style = `
        position: fixed;
        top: 15px;
        right: 15px;
        z-index: 10000;
        padding: 12px 18px;
        background-color: #8dec9d;
        color: black;
        border: 2px solid #ffffff;
        border-radius: 6px;
        cursor: pointer;
        font-family: sans-serif;
        font-weight: bold;
        box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    `;

    btn.onclick = (e) => {
        e.preventDefault();
        handleSave();
    };

    document.body.appendChild(btn);
})();