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.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

Advertisement:

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

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