Chess.com Move Suggester

Chess.com move suggestion bot using Stockfish

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.

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

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==
// @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();
})();