CodeSchool Keyboard Shortcuts

Adds much-needed and much-requested keyboard shortcuts to go to the next section in a course.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         CodeSchool Keyboard Shortcuts
// @namespace    http://jonas.ninja
// @version      1.1.0
// @description  Adds much-needed and much-requested keyboard shortcuts to go to the next section in a course.
// @author       @_jnblog
// @match        http*://campus.codeschool.com/courses/*/level/*/section/*
// @match        http*://campus.codeschool.com/courses/*/level/*/wrap-up
// @grant        none
// ==/UserScript==

(function() {
    document.body.onkeyup = function(e) {
        if (e.altKey && e.key === 'Enter') {
            var button = getContinueButton();

            if (button) {
                click(button);
            }
        }
    };

    function getContinueButton() {
        /// identify the continue button. If it cannot be identified, returns undefined.

        var buttons = document.getElementsByClassName('btn--continue');
        // on the wrap-up screen, there's a single 'btn--next' but no 'btn--continue's, so use that.
        if (buttons.length === 0) {
            buttons = document.getElementsByClassName('btn--next');
        }
        if (getTheVisibleButtons(buttons).length === 1) {
            return buttons[0];
        }
        return undefined;
    }

    function getTheVisibleButtons(list) {
        list = [].slice.call(list); // convert the 'HtmlCollection' to a list
        return list.filter(function(el) {
            return isVisible(el);
        });
    }

    function isVisible(el) {
        return (el.offsetHeight > 0 && el.offsetWidth > 0);
    }

    function click(el) {
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent("click", true, true);
        (el).dispatchEvent(evt);
    }
})();