Skip to content

Working fix #97

@GIJOE68

Description

@GIJOE68

I asked gpt to fix code and it does work. If someone could just verify that it ok.

function findAPI(win) {
    var attempts = 0;
    var maxAttempts = 500;
    while ((typeof win.API_1484_11 === 'undefined' || win.API_1484_11 === null) 
        && win.parent 
        && win !== win.parent 
        && attempts < maxAttempts) {
        win = win.parent;
        attempts++;
    }
    return win.API_1484_11 || null;
}

function getAPI() {
    var API = findAPI(window);
    if (!API && window.opener) {
        API = findAPI(window.opener);
    }
    if (!API) {
        console.error("SCORM API not found.");
        return null;
    }
    return API;
}

function executeSCORMCommands() {
    var API = getAPI();
    if (API) {
        try {
            var success = API.SetValue('cmi.completion_status', 'completed');
            if (success === "true") {
                console.log("SCORM completion status set successfully.");
            } else {
                console.error("Failed to set SCORM completion status.");
            }

            success = API.Commit('');
            if (success === "true") {
                console.log("SCORM data committed successfully.");
            } else {
                console.error("Failed to commit SCORM data.");
            }

            var courseHeader = document.getElementsByName("courseheader")[0];
            if (courseHeader && courseHeader.contentDocument) {
                var submitButton = courseHeader.contentDocument.getElementById("c");
                if (submitButton) {
                    submitButton.submit();
                    console.log("Form submitted.");
                } else {
                    console.warn("Submit button not found.");
                }
            } else {
                console.warn("Course header iframe not found.");
            }
        } catch (error) {
            console.error("Error executing SCORM commands:", error);
        }
    } else {
        console.error("API object is undefined.");
    }
}

// Delay execution to ensure the LMS loads the SCORM API
setTimeout(executeSCORMCommands, 2000);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions