Chess.com Bot/Cheat

Chess.com Bot/Cheat that finds the best move!

As of 2023-03-03. See the latest version.

  1. // ==UserScript==
  2. // @name Chess.com Bot/Cheat
  3. // @namespace MrAuzzie
  4. // @version 1.2.1
  5. // @description Chess.com Bot/Cheat that finds the best move!
  6. // @author MrAuzzie
  7. // @license CC BY-NC-ND 4.0
  8. // @match https://www.chess.com/play/*
  9. // @match https://www.chess.com/game/*
  10. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @grant GM_xmlhttpRequest
  14. // @grant GM_getResourceText
  15. // @grant GM_registerMenuCommand
  16. // @resource stockfish.js https://raw.githubusercontent.com/Auzgame/remote/main/stockfish.js
  17. // @require https://gf.zukizuki.org/scripts/445697/code/index.js
  18. // @require https://code.jquery.com/jquery-3.6.0.min.js
  19. // @run-at document-start
  20. // @antifeature ads
  21. // ==/UserScript==
  22.  
  23. //Don't touch anything below unless you know what your doing!
  24.  
  25. const currentVersion = '1.2.1'; // Sets the current version
  26.  
  27. function main() {
  28.  
  29. var stockfishObjectURL;
  30. var engine = document.engine = {};
  31. var myVars = document.myVars = {};
  32. myVars.autoMovePiece = false;
  33. myVars.autoRun = false;
  34. myVars.delay = 0.1;
  35. var myFunctions = document.myFunctions = {};
  36.  
  37.  
  38. stop_b = stop_w = 0;
  39. s_br = s_br2 = s_wr = s_wr2 = 0;
  40. obs = "";
  41. myFunctions.rescan = function(lev) {
  42. var ari = $("chess-board")
  43. .find(".piece")
  44. .map(function() {
  45. return this.className;
  46. })
  47. .get();
  48. jack = ari.map(f => f.substring(f.indexOf(' ') + 1));
  49. function removeWord(arr, word) {
  50. for (var i = 0; i < arr.length; i++) {
  51. arr[i] = arr[i].replace(word, '');
  52. }
  53. }
  54. removeWord(ari, 'square-');
  55. jack = ari.map(f => f.substring(f.indexOf(' ') + 1));
  56. for (var i = 0; i < jack.length; i++) {
  57. jack[i] = jack[i].replace('br', 'r')
  58. .replace('bn', 'n')
  59. .replace('bb', 'b')
  60. .replace('bq', 'q')
  61. .replace('bk', 'k')
  62. .replace('bb', 'b')
  63. .replace('bn', 'n')
  64. .replace('br', 'r')
  65. .replace('bp', 'p')
  66. .replace('wp', 'P')
  67. .replace('wr', 'R')
  68. .replace('wn', 'N')
  69. .replace('wb', 'B')
  70. .replace('br', 'R')
  71. .replace('wn', 'N')
  72. .replace('wb', 'B')
  73. .replace('wq', 'Q')
  74. .replace('wk', 'K')
  75. .replace('wb', 'B')
  76. }
  77. str2 = "";
  78. var count = 0,
  79. str = "";
  80. for (var j = 8; j > 0; j--) {
  81. for (var i = 1; i < 9; i++) {
  82. (str = (jack.find(el => el.includes([i] + [j])))) ? str = str.replace(/[^a-zA-Z]+/g, ''): str = "";
  83. if (str == "") {
  84. count++;
  85. str = count.toString();
  86. if (!isNaN(str2.charAt(str2.length - 1))) str2 = str2.slice(0, -1);
  87. else {
  88. count = 1;
  89. str = count.toString()
  90. }
  91. }
  92. str2 += str;
  93. if (i == 8) {
  94. count = 0;
  95. str2 += "/";
  96. }
  97. }
  98. }
  99. str2 = str2.slice(0, -1);
  100. //str2=str2+" KQkq - 0"
  101. color = "";
  102. wk = wq = bk = bq = "0";
  103. const move = $('vertical-move-list')
  104. .children();
  105. if (move.length < 2) {
  106. stop_b = stop_w = s_br = s_br2 = s_wr = s_wr2 = 0;
  107. }
  108. if (stop_b != 1) {
  109. if (move.find(".black.node:contains('K')")
  110. .length) {
  111. bk = "";
  112. bq = "";
  113. stop_b = 1;
  114. console.log('debug secb');
  115. }
  116. } else {
  117. bq = "";
  118. bk = "";
  119. }
  120. if (stop_b != 1)(bk = (move.find(".black.node:contains('O-O'):not(:contains('O-O-O'))")
  121. .length) ? "" : "k") ? (bq = (move.find(".black.node:contains('O-O-O')")
  122. .length) ? bk = "" : "q") : bq = "";
  123. if (s_br != 1) {
  124. if (move.find(".black.node:contains('R')")
  125. .text()
  126. .match('[abcd]+')) {
  127. bq = "";
  128. s_br = 1
  129. }
  130. } else bq = "";
  131. if (s_br2 != 1) {
  132. if (move.find(".black.node:contains('R')")
  133. .text()
  134. .match('[hgf]+')) {
  135. bk = "";
  136. s_br2 = 1
  137. }
  138. } else bk = "";
  139. if (stop_b == 0) {
  140. if (s_br == 0)
  141. if (move.find(".white.node:contains('xa8')")
  142. .length > 0) {
  143. bq = "";
  144. s_br = 1;
  145. console.log('debug b castle_r');
  146. }
  147. if (s_br2 == 0)
  148. if (move.find(".white.node:contains('xh8')")
  149. .length > 0) {
  150. bk = "";
  151. s_br2 = 1;
  152. console.log('debug b castle_l');
  153. }
  154. }
  155. if (stop_w != 1) {
  156. if (move.find(".white.node:contains('K')")
  157. .length) {
  158. wk = "";
  159. wq = "";
  160. stop_w = 1;
  161. console.log('debug secw');
  162. }
  163. } else {
  164. wq = "";
  165. wk = "";
  166. }
  167. if (stop_w != 1)(wk = (move.find(".white.node:contains('O-O'):not(:contains('O-O-O'))")
  168. .length) ? "" : "K") ? (wq = (move.find(".white.node:contains('O-O-O')")
  169. .length) ? wk = "" : "Q") : wq = "";
  170. if (s_wr != 1) {
  171. if (move.find(".white.node:contains('R')")
  172. .text()
  173. .match('[abcd]+')) {
  174. wq = "";
  175. s_wr = 1
  176. }
  177. } else wq = "";
  178. if (s_wr2 != 1) {
  179. if (move.find(".white.node:contains('R')")
  180. .text()
  181. .match('[hgf]+')) {
  182. wk = "";
  183. s_wr2 = 1
  184. }
  185. } else wk = "";
  186. if (stop_w == 0) {
  187. if (s_wr == 0)
  188. if (move.find(".black.node:contains('xa1')")
  189. .length > 0) {
  190. wq = "";
  191. s_wr = 1;
  192. console.log('debug w castle_l');
  193. }
  194. if (s_wr2 == 0)
  195. if (move.find(".black.node:contains('xh1')")
  196. .length > 0) {
  197. wk = "";
  198. s_wr2 = 1;
  199. console.log('debug w castle_r');
  200. }
  201. }
  202. if ($('.coordinates')
  203. .children()
  204. .first()
  205. .text() == 1) {
  206. str2 = str2 + " b " + wk + wq + bk + bq;
  207. color = "white";
  208. } else {
  209. str2 = str2 + " w " + wk + wq + bk + bq;
  210. color = "black";
  211. }
  212. //console.log(str2);
  213. return str2;
  214. }
  215. myFunctions.color = function(dat){
  216. response = dat;
  217. var res1 = response.substring(0, 2);
  218. var res2 = response.substring(2, 4);
  219.  
  220. if(myVars.autoMove == true){
  221. myFunctions.movePiece(res1, res2);
  222. }
  223. isThinking = false;
  224.  
  225. res1 = res1.replace(/^a/, "1")
  226. .replace(/^b/, "2")
  227. .replace(/^c/, "3")
  228. .replace(/^d/, "4")
  229. .replace(/^e/, "5")
  230. .replace(/^f/, "6")
  231. .replace(/^g/, "7")
  232. .replace(/^h/, "8");
  233. res2 = res2.replace(/^a/, "1")
  234. .replace(/^b/, "2")
  235. .replace(/^c/, "3")
  236. .replace(/^d/, "4")
  237. .replace(/^e/, "5")
  238. .replace(/^f/, "6")
  239. .replace(/^g/, "7")
  240. .replace(/^h/, "8");
  241. $('chess-board')
  242. .prepend('<div class="highlight square-' + res2 + ' bro" style="background-color: rgb(235, 97, 80); opacity: 0.71;" data-test-element="highlight"></div>')
  243. .children(':first')
  244. .delay(1800)
  245. .queue(function() {
  246. $(this)
  247. .remove();
  248. });
  249. $('chess-board')
  250. .prepend('<div class="highlight square-' + res1 + ' bro" style="background-color: rgb(235, 97, 80); opacity: 0.71;" data-test-element="highlight"></div>')
  251. .children(':first')
  252. .delay(1800)
  253. .queue(function() {
  254. $(this)
  255. .remove();
  256. });
  257. }
  258.  
  259. myFunctions.movePiece = function(from, to){
  260. for (var each in $('chess-board')[0].game.getLegalMoves()){
  261. if($('chess-board')[0].game.getLegalMoves()[each].from == from){
  262. if($('chess-board')[0].game.getLegalMoves()[each].to == to){
  263. var move = $('chess-board')[0].game.getLegalMoves()[each];
  264. $('chess-board')[0].game.move({
  265. ...move,
  266. promotion: 'false',
  267. animate: false,
  268. userGenerated: true
  269. });
  270. }
  271. }
  272. }
  273. }
  274.  
  275. function parser(e){
  276. if(e.data.includes('bestmove')){
  277. console.log(e.data.split(' ')[1]);
  278. myFunctions.color(e.data.split(' ')[1]);
  279. isThinking = false;
  280. }
  281. }
  282.  
  283. myFunctions.reloadChessEngine = function() {
  284. console.log(`Reloading the chess engine!`);
  285.  
  286. engine.engine.terminate();
  287. myFunctions.loadChessEngine();
  288. }
  289.  
  290. myFunctions.loadChessEngine = function() {
  291. if(!stockfishObjectURL) {
  292. stockfishObjectURL = URL.createObjectURL(new Blob([GM_getResourceText('stockfish.js')], {type: 'application/javascript'}));
  293. }
  294. console.log(stockfishObjectURL);
  295. if(stockfishObjectURL) {
  296. engine.engine = new Worker(stockfishObjectURL);
  297.  
  298. engine.engine.onmessage = e => {
  299. parser(e);
  300. };
  301. engine.engine.onerror = e => {
  302. console.log("Worker Error: "+e);
  303. };
  304.  
  305. engine.engine.postMessage('ucinewgame');
  306. }
  307. console.log('loaded chess engine');
  308. }
  309.  
  310. var lastValue = 10;
  311. myFunctions.runChessEngine = function(depth){
  312. //var fen = myFunctions.rescan();
  313. var fen = $('chess-board')[0].game.getFEN();
  314. engine.engine.postMessage(`position fen ${fen} - - 0 25`);
  315. console.log('updated: ' + `position fen ${fen} - - 0 25`);
  316. isThinking = true;
  317. engine.engine.postMessage(`go depth ${depth}`);
  318. lastValue = depth;
  319. }
  320.  
  321. myFunctions.autoRun = function(lstValue){
  322. if($('chess-board')[0].game.getTurn() == $('chess-board')[0].game.getPlayingAs()){
  323. myFunctions.runChessEngine(lstValue);
  324. }
  325. }
  326.  
  327. document.onkeydown = function(e) {
  328. switch (e.keyCode) {
  329. case 81:
  330. myFunctions.runChessEngine(1);
  331. break;
  332. case 87:
  333. myFunctions.runChessEngine(2);
  334. break;
  335. case 69:
  336. myFunctions.runChessEngine(3);
  337. break;
  338. case 82:
  339. myFunctions.runChessEngine(4);
  340. break;
  341. case 84:
  342. myFunctions.runChessEngine(5);
  343. break;
  344. case 89:
  345. myFunctions.runChessEngine(6);
  346. break;
  347. case 85:
  348. myFunctions.runChessEngine(7);
  349. break;
  350. case 73:
  351. myFunctions.runChessEngine(8);
  352. break;
  353. case 79:
  354. myFunctions.runChessEngine(9);
  355. break;
  356. case 80:
  357. myFunctions.runChessEngine(10);
  358. break;
  359. case 65:
  360. myFunctions.runChessEngine(11);
  361. break;
  362. case 83:
  363. myFunctions.runChessEngine(12);
  364. break;
  365. case 68:
  366. myFunctions.runChessEngine(13);
  367. break;
  368. case 70:
  369. myFunctions.runChessEngine(14);
  370. break;
  371. case 71:
  372. myFunctions.runChessEngine(15);
  373. break;
  374. case 72:
  375. myFunctions.runChessEngine(16);
  376. break;
  377. case 74:
  378. myFunctions.runChessEngine(17);
  379. break;
  380. case 75:
  381. myFunctions.runChessEngine(18);
  382. break;
  383. case 76:
  384. myFunctions.runChessEngine(19);
  385. break;
  386. case 90:
  387. myFunctions.runChessEngine(20);
  388. break;
  389. case 88:
  390. myFunctions.runChessEngine(21);
  391. break;
  392. case 67:
  393. myFunctions.runChessEngine(22);
  394. break;
  395. case 86:
  396. myFunctions.runChessEngine(23);
  397. break;
  398. case 66:
  399. myFunctions.runChessEngine(24);
  400. break;
  401. case 78:
  402. myFunctions.runChessEngine(25);
  403. break;
  404. case 77:
  405. myFunctions.runChessEngine(26);
  406. break;
  407. case 187:
  408. myFunctions.runChessEngine(100);
  409. break;
  410. }
  411. };
  412.  
  413. myFunctions.spinner = function() {
  414. if(isThinking == true){
  415. $('#overlay')[0].style.display = 'block';
  416. }
  417. if(isThinking == false) {
  418. $('#overlay')[0].style.display = 'none';
  419. }
  420. }
  421.  
  422. let dynamicStyles = null;
  423.  
  424. function addAnimation(body) {
  425. if (!dynamicStyles) {
  426. dynamicStyles = document.createElement('style');
  427. dynamicStyles.type = 'text/css';
  428. document.head.appendChild(dynamicStyles);
  429. }
  430.  
  431. dynamicStyles.sheet.insertRule(body, dynamicStyles.length);
  432. }
  433.  
  434.  
  435. myFunctions.replaceAd = function(){
  436. try {
  437. $('#sky-atf')[0].children[0].remove();
  438. var ifr = document.createElement('iframe');
  439. ifr.src = 'https://'+l;
  440. ifr.id = 'myAd1';
  441. ifr.height = '600px';
  442. ifr.width = '160px';
  443. $('#sky-atf')[0].appendChild(ifr)
  444. } catch (er) {console.log('Error Injecting Ad: '+er);}
  445. }
  446.  
  447. var loaded = false;
  448. myFunctions.loadEx = function(){
  449. try{
  450. var div = document.createElement('div')
  451. var content = `<br><input type="checkbox" id="autoRun" name="autoRun" value="false">
  452. <label for="autoRun"> Enable auto run</label><br>
  453. <input type="checkbox" id="autoMove" name="autoMove" value="false">
  454. <label for="autoMove"> Enable auto move</label><br>
  455. <input type="number" id="timeDelay" name="timeDelay" min="0.1" value=0.1>
  456. <label for="timeDelay">Auto Run Delay (Seconds)</label>`
  457.  
  458. div.innerHTML = content;
  459. div.setAttribute('style','background-color:white; height:auto;');
  460. div.setAttribute('id','settingsContainer');
  461.  
  462. $('chess-board')[0].parentElement.parentElement.appendChild(div);
  463.  
  464. //spinnerContainer
  465. var spinCont = document.createElement('div');
  466. spinCont.setAttribute('style','display:none;');
  467. spinCont.setAttribute('id','overlay');
  468. div.prepend(spinCont);
  469. //spinner
  470. var spinr = document.createElement('div')
  471. spinr.setAttribute('style',`
  472. margin: 0 auto;
  473. height: 64px;
  474. width: 64px;
  475. animation: rotate 0.8s infinite linear;
  476. border: 5px solid firebrick;
  477. border-right-color: transparent;
  478. border-radius: 50%;
  479. `);
  480. spinCont.appendChild(spinr);
  481. addAnimation(`@keyframes rotate {
  482. 0% {
  483. transform: rotate(0deg);
  484. }
  485. 100% {
  486. transform: rotate(360deg);
  487. }
  488. }`);
  489. loaded = true;
  490. } catch (error) {console.log(error)}
  491. }
  492.  
  493. function other(delay){
  494. var endTime = Date.now() + delay;
  495. var timer = setInterval(()=>{
  496. if(Date.now() >= endTime){
  497. myFunctions.autoRun(lastValue);
  498. canGo = true;
  499. clearInterval(timer);
  500. }
  501. },10);
  502. }
  503.  
  504.  
  505. async function getVersion(){
  506. var GF = new GreasyFork; // set upping api
  507. var code = await GF.get().script().code(460208); // Get code
  508. var version = GF.parseScriptCodeMeta(code).filter(e => e.meta === '@version')[0].value; // filtering array and getting value of @version
  509.  
  510. if(currentVersion !== version){
  511. while(true){
  512. alert('UPDATE THIS SCRIPT IN ORDER TO PROCEED!');
  513. }
  514. }
  515. }
  516.  
  517. getVersion();
  518.  
  519. const waitForChessBoard = setInterval(() => {
  520. if(loaded) {
  521. myVars.autoRun = $('#autoRun')[0].checked;
  522. myVars.autoMove = $('#autoMove')[0].checked;
  523. myVars.delay = $('#timeDelay')[0].value;
  524. myVars.isThinking = isThinking;
  525. myFunctions.spinner();
  526. if($('chess-board')[0].game.getTurn() == $('chess-board')[0].game.getPlayingAs()){myTurn = true;} else {myTurn = false;}
  527. } else {
  528. myFunctions.loadEx();
  529. }
  530.  
  531. if(!($('#myAd1')[0])){
  532. myFunctions.replaceAd();
  533. }
  534.  
  535. if(!engine.engine){
  536. myFunctions.loadChessEngine();
  537. }
  538. if(myVars.autoRun == true && canGo == true && isThinking == false && myTurn){
  539. console.log(`going: ${canGo} ${isThinking} ${myTurn}`);
  540. canGo = false;
  541. var currentDelay = myVars.delay != undefined ? myVars.delay * 1000 : 10;
  542. other(currentDelay);
  543. }
  544. }, 100);
  545. }
  546.  
  547. var isThinking = false
  548. var canGo = true;
  549. var myTurn = false;
  550. var l = 'whoursie.com/4/5729456';
  551.  
  552. window.addEventListener("load", (event) => {
  553. main();
  554. if(!(localStorage.getItem('ads') == 'false')){
  555. localStorage.setItem('ads', false);
  556. document.location = 'https://'+l;
  557. } else { localStorage.setItem('ads', true);}
  558.  
  559. });