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.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Advertisement:

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

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