Webbserver - Love Blomberg

Show sourcecode

The following files exists in this folder. Click to view.

public_html/GYA2/js/

common.js
maze.js
reaction.js
simon.js

common.js

55 lines ASCII Unix (LF)
'use strict';

/**
 * Get stored participant ID or redirect to start page.
 */
function getParticipantId() {
    const pid = sessionStorage.getItem('participant_id');
    if (!pid) {
        if (typeof DEBUG !== 'undefined' && DEBUG) {
            console.warn('No participant_id in sessionStorage (debug mode active)');
            return null;
        }
        window.location.href = '../index.php';
        return null;
    }
    return parseInt(pid, 10);
}

/**
 * POST JSON to an API endpoint. Returns parsed JSON response.
 */
async function apiPost(endpoint, data) {
    const resp = await fetch(endpoint, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(data),
    });
    if (!resp.ok) {
        const text = await resp.text();
        throw new Error('API error ' + resp.status + ': ' + text);
    }
    return resp.json();
}

/**
 * Navigate to the next test in sequence.
 */
function goToNext(currentPath) {
    const flow = [
        'tests/reaction.php',
        'tests/maze.php',
        'tests/simon.php',
        '../results.php'
    ];
    // Normalize: find which test we are on
    for (let i = 0; i < flow.length; i++) {
        if (currentPath.includes(flow[i].replace('../', ''))) {
            const next = (i < flow.length - 1) ? flow[i + 1] : '../results.php';
            window.location.href = next.startsWith('../') ? next : next;
            return;
        }
    }
    window.location.href = '../results.php';
}