BGA Shortcuts

Keyboard shortcuts for the Atlas

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name       BGA Shortcuts
// @namespace  BGA Shortcuts
// @version    0.2.1
// @description  Keyboard shortcuts for the Atlas
// @include     http://*.boardgameatlas.*/*
// @include     http://boardgameatlas.*/*
// @include     https://*.boardgameatlas.*/*
// @include     https://boardgameatlas.*/*
// @copyright  2019+, JB McMichael
// ==/UserScript==

/*
 * CHANGLOG::
 * ============================================
 * 0.2.1 - Change how the focus works
 * 0.2.0 - Add / to jump to search
 * 0.1.3 - Fix copypasta error
 * 0.1.2 - Adding a changelog
 * 0.1.1 - Auto run the main function
 * 0.1.0 - First version
 */

(function () {
    "use strict";
    console.log('Loaded BGA Shortcuts');

    /** Keyboard Shortcuts **/
    document.body.addEventListener('keydown', function (e) {
        let active = document.activeElement.tagName.toLowerCase(),
            badElements = ['input', 'textarea', 'select'];

        // ignore shortcuts if we are in some form of input
        if (badElements.indexOf(active) === -1) {
            // Search box jump /
            if (e.key === '?') {
                let searchbox = document.querySelector('#bg-search');
                document.body.scrollTop = 0;
                window.setTimeout(function () {
                    searchbox.value = '';
                    searchbox.focus();
                }, 10);
            }
        }
    });

    /**
     * If we are on the edit page, do some things
     */
    if (window.location.search.search(/search\/game\/[a-zA-Z0-9]+\/edit$/)) {
        makeLabelsClickable();
    }

    /** Functions **/

    /**
     * Add IDs to the checkboxes, and then set the label's FOR attribute to allow clicking on the labels
     * to click the checkbox
     */
    function makeLabelsClickable() {
        let labels = document.querySelectorAll('.form-inline label');
        labels.forEach((label) => {
            let node = label.previousSibling;

            if (node !== null && node.matches('[type="checkbox"]')) {
                if (node.getAttribute('id') === null) {
                    node.setAttribute('id', node.value);
                    label.setAttribute('for', node.value);
                }
            }
        });
    }
}
());