Memrise Easy Quiz Input

Select special characters for Memrise quizzes using keyboard shortcuts 1 to 0.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Memrise Easy Quiz Input
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Select special characters for Memrise quizzes using keyboard shortcuts 1 to 0.
// @author       Cezille07
// @match        https://www.memrise.com/course/*
// @grant        none
// @license      GNU GPLv3
// ==/UserScript==

(function() {
    'use strict';
    var $ = window.jQuery;
    var selector = '.keyboard a.shiny-box';

    $(document).ready(function () {
        console.log('[MEQI] Initializing Memrize Easy Quiz Input');
        var boxes = document.getElementById('boxes');
        if (boxes) {
            var observer = new MutationObserver(function(mutationRecords) {
                var inputs = $(selector);
                if (inputs.length) {
                    console.log('[MEQI] Adding Numbers', inputs);
                    for (var i = 0; i < inputs.length; i++) {
                        console.log('Adding help', i, inputs[i]);
                        inputs[i].innerText = (i + 1) + ': ' + inputs[i].innerText;
                    }
                } else {
                    console.log('[MEQI] No shortcuts to add hints to');
                }
            });
            observer.observe(boxes, {
                childList: true
            });
        }
    });

    window.addEventListener('keydown', function (e) {
        var inputs = $(selector);
        var ZERO = 48;
        var NUM_ZERO = 96;

        if (inputs.length > 0 &&
            (e.keyCode >= ZERO && e.keyCode < ZERO + 10) ||
            (e.keyCode >= NUM_ZERO && e.keyCode < NUM_ZERO + 10)
        ) {
            var map = {};
            for (var i = 0; i < 10; i++) {
                map[i + ZERO] = i - 1;
                map[i + NUM_ZERO] = i - 1;
            }
            // Zero is the 10th item
            map[ZERO] = 9;
            map[NUM_ZERO] = 9;

            console.log('[MEQI] Clicking', e.code);
            document.activeElement.blur();
            setTimeout(function () {
                var el = inputs[map[e.keyCode]];
                // This condition prevents inputting a 9 (for instance) if less than 9 boxes are available
                if (el) {
                    var colIdx = el.innerText.indexOf(': ');
                    var num = el.innerText.slice(0, colIdx);
                    el.innerText = el.innerText.slice(colIdx + 2);
                    el.click();
                    el.innerText = num + ': ' + el.innerText;
                }
            }, 0);
        }
    });
})();