Focus on Force Exam Keyboard Controls

Configurable keyboard shortcuts for answering questions and navigating Topic Exams on Focus on Force

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Focus on Force Exam Keyboard Controls
// @namespace    https://focusonforce.com/
// @version      1.0
// @description  Configurable keyboard shortcuts for answering questions and navigating Topic Exams on Focus on Force
// @match        https://focusonforce.com/exams/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    const KEY_CONFIG = { // Maping button or answer number to Keyboard's key
        CHECK: ['Enter', ' '], // Check button
        FINISH: ['\\'], // Finish button

        ANSWER_1: ['1', 'u'],
        ANSWER_2: ['2', 'i'],
        ANSWER_3: ['3', 'o'],
        ANSWER_4: ['4', 'p'],
        ANSWER_5: ['5', '['],
        ANSWER_6: ['6', ']']
    };

    const KEY_TO_ACTION = Object.entries(KEY_CONFIG).reduce((acc, [action, keys]) => {
        keys.forEach(key => acc[key] = action);
        return acc;
    }, {});

    document.addEventListener('keydown', function (e) {
        const action = KEY_TO_ACTION[e.key];
        if (!action) return;

        // CHECK
        if (action === 'CHECK') {
            const checkButton = document.querySelector(
                'input.wdm_validate[name="wdm_validate"]'
            );
            if (checkButton && !checkButton.disabled) {
                checkButton.click();
            }
            return;
        }

        // FINISH
        if (action === 'FINISH') {
            const finishButton = document.querySelector(
                'input.wpProQuiz_button[name="endQuizSummary"]'
            );
            if (finishButton) {
                const parent = finishButton.closest('div.wpProQuiz_checkPage');
                if (parent && getComputedStyle(parent).display !== 'none') {
                    finishButton.click();
                }
            }
            return;
        }

        // ANSWER_X
        if (action.startsWith('ANSWER_')) {
            const answerIndex = parseInt(action.split('_')[1], 10) - 1;

            //Without the filter, it will answer ALL question at once
            const visibleQuestions = Array.from(
                document.querySelectorAll('li.wpProQuiz_listItem')
            ).filter(li => getComputedStyle(li).display !== 'none');

            visibleQuestions.forEach(li => {
                const options = li.querySelectorAll(
                    '.wpProQuiz_questionListItem input:not([disabled])'
                );
                if (answerIndex < options.length) {
                    options[answerIndex].click();
                }
            });
        }
    });

})();