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.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

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

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Advertisement:

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

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