// ==UserScript== // @name Chess.com Stockfish Bot // @namespace BottleOrg Scripts // @version 1.6.2.19 // @description Chess.com Stockfish Bot with Enhanced Auto-Match using Stockfish Online API // @author [REDACTED] // @license Chess.com Bot/Cheat by [REDACTED] // @match https://www.chess.com/play/* // @match https://www.chess.com/game/* // @match https://www.chess.com/puzzles/* // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== // @grant GM_getValue // @grant GM_setValue // @grant GM_xmlhttpRequest // @grant GM_getResourceText // @grant GM_registerMenuCommand // @require https://greasyfork.org/scripts/445697/code/index.js // @require https://code.jquery.com/jquery-3.6.0.min.js // @run-at document-start // @downloadURL none // ==/UserScript== const currentVersion = '1.6.2.19'; const scriptURL = 'https://greasyfork.org/en/scripts/526240-chess-com-stockfish-bot'; const stockfishAPI_URI = "https://stockfish.online/api/s/v2.php"; // Function to check if the Stockfish API domain is accessible function checkStockfishDomainAccess(callback) { console.log("Checking if Stockfish API domain is accessible..."); const testFen = "rnbqkbnr/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; const testDepth = 1; const testUrl = `${stockfishAPI_URI}?fen=${encodeURIComponent(testFen)}&depth=${testDepth}`; GM_xmlhttpRequest({ method: "GET", url: testUrl, onload: function(response) { if (response.status === 200) { console.log("Stockfish API domain is accessible."); callback(true); } else { console.error("Stockfish API request failed with status:", response.status); callback(false); } }, onerror: function(error) { console.error("Stockfish API request blocked or failed:", error); // Check if the error indicates the request was blocked (e.g., by an ad-blocker) if (error && error.statusText && error.statusText.includes("net::ERR_BLOCKED_BY_CLIENT")) { alert("Allow domain request to stockfish.online for the script to be able to work!"); } else { alert("Failed to connect to Stockfish API. Please check your internet connection or allow requests to stockfish.online."); } callback(false); }, timeout: 5000 // Set a timeout of 5 seconds for the test request }); } function checkForUpdate() { console.log("Checking for script updates..."); GM_xmlhttpRequest({ method: "GET", url: scriptURL, onload: function(response) { if (response.status === 200) { const html = response.responseText; const versionMatch = html.match(/@version\s+([\d.]+)/); if (versionMatch && versionMatch[1]) { const latestVersion = versionMatch[1]; console.log("Latest version found:", latestVersion); if (compareVersions(latestVersion, currentVersion) > 0) { const message = `New Version: ${latestVersion} has been uploaded. Would you like me to take you there or continue with old version ${currentVersion}? (Not recommended for stability)`; if (confirm(message)) { window.location.href = scriptURL; } else { console.log("User chose to continue with old version."); } } else { console.log("No newer version available."); } } else { console.error("Could not find version in Greasy Fork page."); } } else { console.error("Failed to fetch script page:", response.status); } }, onerror: function(error) { console.error("Error checking for update:", error); } }); } function compareVersions(v1, v2) { const parts1 = v1.split('.').map(Number); const parts2 = v2.split('.').map(Number); for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) { const p1 = parts1[i] || 0; const p2 = parts2[i] || 0; if (p1 > p2) return 1; if (p1 < p2) return -1; } return 0; } function main() { var myVars = document.myVars = { autoMove: false, autoRun: false, autoMatch: false, delay: 0.1, hasAutoMatched: false, gameEnded: false, stockfishAccessible: false }; var myFunctions = document.myFunctions = {}; var currentStockfishVersion = "Stockfish API"; var uiElementsLoaded = false; // Check Stockfish API access before proceeding checkStockfishDomainAccess(function(isAccessible) { myVars.stockfishAccessible = isAccessible; if (!isAccessible) { console.error("Stockfish API is not accessible. Script functionality will be limited."); return; // Stop script execution if Stockfish API is not accessible } proceedWithMainLogic(); }); function proceedWithMainLogic() { var stop_b = 0, stop_w = 0, s_br = 0, s_br2 = 0, s_wr = 0, s_wr2 = 0; myFunctions.rescan = function() { console.log("Rescanning board..."); var boardElement = document.querySelector('chess-board, wc-chess-board'); if (!boardElement) { console.warn("No board element found. Using default FEN."); return "rnbqkbnr/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; } var pieces = $(boardElement).find(".piece").map(function() { return this.className; }).get(); if (!pieces.length) { console.warn("No pieces found. Using default FEN."); return "rnbqkbnr/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; } var boardArray = Array(64).fill(''); pieces.forEach(piece => { var classes = piece.split(' '); var squareClass = classes.find(c => c.startsWith('square-')); var pieceClass = classes.find(c => /^[wb][prnbqk]$/.test(c)); if (squareClass && pieceClass) { var squareNum = squareClass.replace('square-', ''); var file = parseInt(squareNum[0]) - 1; var rank = parseInt(squareNum[1]) - 1; var square = (7 - rank) * 8 + file; if (square >= 0 && square < 64) { var pieceChar = {'wp': 'P', 'bp': 'p', 'wr': 'R', 'br': 'r', 'wn': 'N', 'bn': 'n', 'wb': 'B', 'bb': 'b', 'wq': 'Q', 'bq': 'q', 'wk': 'K', 'bk': 'k'}[pieceClass]; boardArray[square] = pieceChar; } } }); var fen = ''; for (var i = 0; i < 64; i++) { if (i % 8 === 0 && i > 0) fen += '/'; var piece = boardArray[i]; if (!piece) { var emptyCount = 1; while (i + 1 < 64 && !boardArray[i + 1] && (i + 1) % 8 !== 0) { emptyCount++; i++; } fen += emptyCount; } else { fen += piece; } } var turn = $('.coordinates').children().first().text() === "1" ? 'b' : 'w'; var castling = (stop_w ? '' : 'KQ') + (stop_b ? '' : 'kq') || '-'; fen += ` ${turn} ${castling} - 0 1`; console.log("Generated FEN:", fen); return fen; }; myFunctions.color = function(dat) { console.log("myFunctions.color CALLED with:", dat); const bestmoveUCI = dat.split(' ')[1]; console.log("Extracted bestmove UCI:", bestmoveUCI); if (myVars.autoMove) myFunctions.movePiece(bestmoveUCI); else myFunctions.highlightMove(bestmoveUCI); isThinking = false; myFunctions.spinner(); }; myFunctions.highlightMove = function(bestmoveUCI) { var res1 = bestmoveUCI.substring(0, 2), res2 = bestmoveUCI.substring(2, 4); $(board).prepend(`
`) .children(':first').delay(1800).queue(function() { $(this).remove(); }); $(board).prepend(``) .children(':first').delay(1800).queue(function() { $(this).remove(); }); console.log("Highlighted:", bestmoveUCI); }; myFunctions.movePiece = function(bestmoveUCI) { console.log("movePiece CALLED with:", bestmoveUCI); if (!board || !board.game) { console.error("Board or board.game not initialized!"); return; } const fromSquare = bestmoveUCI.substring(0, 2); const toSquare = bestmoveUCI.substring(2, 4); const legalMoves = board.game.getLegalMoves(); console.log("Legal moves:", legalMoves); let foundMove = legalMoves.find(move => move.from === fromSquare && move.to === toSquare); if (foundMove) { console.log("Executing move:", foundMove); board.game.move({ ...foundMove, promotion: 'q', animate: true, userGenerated: true }); console.log("Move executed:", bestmoveUCI); } else { console.warn("No legal move found for:", bestmoveUCI); } }; myFunctions.reloadChessEngine = function() { console.log("Reload not needed for API."); }; myFunctions.loadChessEngine = function() { console.log("Using Stockfish API."); if (uiElementsLoaded) $('#engineVersionText')[0].innerHTML = "Engine: Stockfish API"; }; myFunctions.fetchBestMoveFromAPI = function(fen, depth) { if (!myVars.stockfishAccessible) { console.error("Stockfish API is not accessible. Cannot fetch best move."); isThinking = false; myFunctions.spinner(); return; } const apiURL = `${stockfishAPI_URI}?fen=${encodeURIComponent(fen)}&depth=${depth}`; console.log(`Fetching from: ${apiURL}`); GM_xmlhttpRequest({ method: "GET", url: apiURL, onload: function(response) { if (response.status === 200) { try { const jsonResponse = JSON.parse(response.responseText); if (jsonResponse.success) { console.log("API Response:", jsonResponse); myFunctions.color(jsonResponse.bestmove); } else { console.error("API failed:", jsonResponse); isThinking = false; myFunctions.spinner(); } } catch (e) { console.error("API parse error:", e); isThinking = false; myFunctions.spinner(); } } else { console.error("API error:", response.status); isThinking = false; myFunctions.spinner(); } }, onerror: function(error) { console.error("API request error:", error); isThinking = false; myFunctions.spinner(); } }); }; myFunctions.startNewGame = function() { console.log("Starting new game..."); const modalNewGameButton = $('.game-over-modal-content .game-over-buttons-component .cc-button-component:not([aria-label="Rematch"])'); if (modalNewGameButton.length) { modalNewGameButton[0].click(); console.log("Clicked NewDepth: ${lastValue}
Keys: Q-G (1-15), +/-
Engine: Stockfish API
Script is made for guest account since Stockfish makes unhuman moves!