Chess.com Move Suggester

Chess.com move suggestion bot using Stockfish

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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();
})();