必应拼图小游戏键盘控件

让你现在就能用上拼图游戏里的键盘控件,不必再等待慵懒的微软员工,使用键盘控件玩拼图小游戏实在是泰裤辣!

// ==UserScript==
// @name         必应拼图小游戏键盘控件
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  让你现在就能用上拼图游戏里的键盘控件,不必再等待慵懒的微软员工,使用键盘控件玩拼图小游戏实在是泰裤辣!
// @author       Haze
// @match        https://cn.bing.com/spotlight/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bing.com
// @grant        none
// ==/UserScript==

(function () {
    'use strict';
    let tiles;
    const entry = () => {
        loadElements();
        window.onkeyup = function (e) {
            if (e.key == 'ArrowUp') {
                inputArrowUp();
            }
            if (e.key == 'ArrowDown') {
                inputArrowDown();
            }
            if (e.key == 'ArrowLeft') {
                inputArrowLeft();
            }
            if (e.key == 'ArrowRight') {
                inputArrowRight();
            }
        }
    };
    const loadElements = () => {
        tiles = document.getElementById("tiles").children;
    };
    const inputArrowUp = () => {
        for (let i = 0; i < tiles.length; i++) {
            const next = i - 3;
            if (checkTileByIndex(next)) {
                tiles[i].click();
                break;
            }
        }
    };
    const inputArrowDown = () => {
        for (let i = 0; i < tiles.length; i++) {
            const next = i + 3;
            if (checkTileByIndex(next)) {
                tiles[i].click();
                break;
            }
        }
    };
    const inputArrowLeft = () => {
        for (let i = 0; i < tiles.length; i++) {
            const next = i - 1;
            if (checkTileByIndex(next)) {
                tiles[i].click();
                break;
            }
        }
    };
    const inputArrowRight = () => {
        for (let i = 0; i < tiles.length; i++) {
            const next = i + 1;
            if (checkTileByIndex(next)) {
                tiles[i].click();
                break;
            }
        }
    };
    const checkTileByIndex = (index) => {
        if (index < 0 || index >= tiles.length) {
            return false;
        }
        const targetChildren = tiles[index].children;
        if (targetChildren.length == 0) {
            return true;
        }
    };
    setTimeout(entry, 500);
})();