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.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

Advertisement:

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

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