GC - Pick Your Own - Keyboard Controls

Add keyboard navigation to GC's Pick Your Own.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         GC - Pick Your Own - Keyboard Controls
// @namespace    https://greasyfork.org/en/users/1175371
// @version      0.4
// @description  Add keyboard navigation to GC's Pick Your Own.
// @author       sanjix
// @match        https://www.grundos.cafe/medieval/pickyourown/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=grundos.cafe
// @grant        none
// @license      MIT
// ==/UserScript==

var start = document.querySelector('input[value="Click to Play!"]');
var left = document.querySelector('input#pyo-left-arrow');
var right = document.querySelector('input#pyo-right-arrow');
var up = document.querySelector('input#pyo-up-arrow');
var down = document.querySelector('input#pyo-down-arrow');
var map = document.querySelector('form[action="/medieval/process_pickyourown/?pick=1"] input[type="image"]');
var collect = document.querySelector('input[value="Collect Berries and Leave Farm"]');

document.addEventListener("keydown", ((event) => {
    switch (event.keyCode) {
        case 38: //up-arrow
        case 87: //w
            {
                if (up != null) {
                    event.preventDefault();
                    up.click();
                }
            }
            break;
        case 37: //left-arrow
        case 65: //a
            {
                if (left != null) {
                    event.preventDefault();
                    left.click();
                }
            }
            break;
        case 40: //down-arrow
        case 83: //s
            {
                if (down != null) {
                    event.preventDefault();
                    down.click();
                }
            }
            break;
        case 39: //right-arrow
        case 68: //d
            {
                if (right != null) {
                    event.preventDefault();
                    right.click();
                }
            }
            break;
        case 13: //enter
            {
                if (start != null) {
                    start.click();
                } else if (map != null) {
                    map.click();
                } else if (collect != null) {
                    collect.click();
                }
            }
            break;
	}
}));