Chess.com Move Suggester

Chess.com move suggestion bot using Stockfish

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @license MIT
// @description      Chess.com move suggestion bot using Stockfish
// @name             Chess.com Move Suggester
// @namespace        https://your-unique-namespace.com
// @match            https://www.chess.com/*
// @run-at           document-end
// @grant            none
// @version          1.0
// ==/UserScript==

(async function() {
    const stockfish = new Worker("https://raw.githubusercontent.com/official-stockfish/Stockfish-scripts/main/stockfish.js");
    let boardFEN = "";
    
    function getBoardFEN() {
        const fenElement = document.querySelector("[data-cy='board-controls']");
        return fenElement ? fenElement.getAttribute("data-fen") : null;
    }
    
    function suggestMove(fen) {
        return new Promise((resolve) => {
            stockfish.postMessage("uci");
            stockfish.postMessage("position fen " + fen);
            stockfish.postMessage("go depth 15");
            
            stockfish.onmessage = (event) => {
                if (event.data.includes("bestmove")) {
                    resolve(event.data.split(" ")[1]);
                }
            };
        });
    }
    
    function createButton() {
        const button = document.createElement("button");
        button.innerText = "Suggest Move";
        button.style.position = "fixed";
        button.style.bottom = "10px";
        button.style.right = "10px";
        button.style.padding = "10px";
        button.style.background = "blue";
        button.style.color = "white";
        button.style.border = "none";
        button.style.cursor = "pointer";
        
        button.onclick = async () => {
            boardFEN = getBoardFEN();
            if (boardFEN) {
                const bestMove = await suggestMove(boardFEN);
                alert("Best move: " + bestMove);
            } else {
                alert("Could not retrieve board state.");
            }
        };
        
        document.body.appendChild(button);
    }
    
    createButton();
})();