Greasy Fork is available in English.

Chess.com Bot/Cheat

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

  1. // additional copyright/license info:
  2. //© All Rights Reserved
  3. //
  4. //Chess.com Bot/Cheat © 2023 by MrAuzzie#998142
  5. //
  6. // ==UserScript==
  7. // @name Chess.com Bot/Cheat
  8. // @namespace MrAuzzie
  9. // @version 1.2.9.5
  10. // @description Chess.com Bot/Cheat that finds the best move!
  11. // @author MrAuzzie
  12. // @license Chess.com Bot/Cheat © 2023 by MrAuzzie#998142, © All Rights Reserved
  13. // @match https://www.chess.com/play/*
  14. // @match https://www.chess.com/game/*
  15. // @match https://www.chess.com/puzzles/*
  16. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  17. // @grant GM_getValue
  18. // @grant GM_setValue
  19. // @grant GM_xmlhttpRequest
  20. // @grant GM_getResourceText
  21. // @grant GM_registerMenuCommand
  22. // @resource stockfish.js https://cdnjs.cloudflare.com/ajax/libs/stockfish.js/9.0.0/stockfish.js
  23. // @require https://greatest.deepsurf.us/scripts/445697/code/index.js
  24. // @require https://code.jquery.com/jquery-3.6.0.min.js
  25. // @run-at document-start
  26. // @antifeature ads
  27. // ==/UserScript==
  28.  
  29. //Don't touch anything below unless you know what your doing!
  30.  
  31. const currentVersion = '1.2.9.5'; // Sets the current version
  32.  
  33. function main() {
  34.  
  35. var stockfishObjectURL;
  36. var engine = document.engine = {};
  37. var myVars = document.myVars = {};
  38. myVars.autoMovePiece = false;
  39. myVars.autoRun = false;
  40. myVars.delay = 0.1;
  41. var myFunctions = document.myFunctions = {};
  42.  
  43.  
  44. stop_b = stop_w = 0;
  45. s_br = s_br2 = s_wr = s_wr2 = 0;
  46. obs = "";
  47. myFunctions.rescan = function(lev) {
  48. var ari = $("chess-board")
  49. .find(".piece")
  50. .map(function() {
  51. return this.className;
  52. })
  53. .get();
  54. jack = ari.map(f => f.substring(f.indexOf(' ') + 1));
  55. function removeWord(arr, word) {
  56. for (var i = 0; i < arr.length; i++) {
  57. arr[i] = arr[i].replace(word, '');
  58. }
  59. }
  60. removeWord(ari, 'square-');
  61. jack = ari.map(f => f.substring(f.indexOf(' ') + 1));
  62. for (var i = 0; i < jack.length; i++) {
  63. jack[i] = jack[i].replace('br', 'r')
  64. .replace('bn', 'n')
  65. .replace('bb', 'b')
  66. .replace('bq', 'q')
  67. .replace('bk', 'k')
  68. .replace('bb', 'b')
  69. .replace('bn', 'n')
  70. .replace('br', 'r')
  71. .replace('bp', 'p')
  72. .replace('wp', 'P')
  73. .replace('wr', 'R')
  74. .replace('wn', 'N')
  75. .replace('wb', 'B')
  76. .replace('br', 'R')
  77. .replace('wn', 'N')
  78. .replace('wb', 'B')
  79. .replace('wq', 'Q')
  80. .replace('wk', 'K')
  81. .replace('wb', 'B')
  82. }
  83. str2 = "";
  84. var count = 0,
  85. str = "";
  86. for (var j = 8; j > 0; j--) {
  87. for (var i = 1; i < 9; i++) {
  88. (str = (jack.find(el => el.includes([i] + [j])))) ? str = str.replace(/[^a-zA-Z]+/g, ''): str = "";
  89. if (str == "") {
  90. count++;
  91. str = count.toString();
  92. if (!isNaN(str2.charAt(str2.length - 1))) str2 = str2.slice(0, -1);
  93. else {
  94. count = 1;
  95. str = count.toString()
  96. }
  97. }
  98. str2 += str;
  99. if (i == 8) {
  100. count = 0;
  101. str2 += "/";
  102. }
  103. }
  104. }
  105. str2 = str2.slice(0, -1);
  106. //str2=str2+" KQkq - 0"
  107. color = "";
  108. wk = wq = bk = bq = "0";
  109. const move = $('vertical-move-list')
  110. .children();
  111. if (move.length < 2) {
  112. stop_b = stop_w = s_br = s_br2 = s_wr = s_wr2 = 0;
  113. }
  114. if (stop_b != 1) {
  115. if (move.find(".black.node:contains('K')")
  116. .length) {
  117. bk = "";
  118. bq = "";
  119. stop_b = 1;
  120. console.log('debug secb');
  121. }
  122. } else {
  123. bq = "";
  124. bk = "";
  125. }
  126. if (stop_b != 1)(bk = (move.find(".black.node:contains('O-O'):not(:contains('O-O-O'))")
  127. .length) ? "" : "k") ? (bq = (move.find(".black.node:contains('O-O-O')")
  128. .length) ? bk = "" : "q") : bq = "";
  129. if (s_br != 1) {
  130. if (move.find(".black.node:contains('R')")
  131. .text()
  132. .match('[abcd]+')) {
  133. bq = "";
  134. s_br = 1
  135. }
  136. } else bq = "";
  137. if (s_br2 != 1) {
  138. if (move.find(".black.node:contains('R')")
  139. .text()
  140. .match('[hgf]+')) {
  141. bk = "";
  142. s_br2 = 1
  143. }
  144. } else bk = "";
  145. if (stop_b == 0) {
  146. if (s_br == 0)
  147. if (move.find(".white.node:contains('xa8')")
  148. .length > 0) {
  149. bq = "";
  150. s_br = 1;
  151. console.log('debug b castle_r');
  152. }
  153. if (s_br2 == 0)
  154. if (move.find(".white.node:contains('xh8')")
  155. .length > 0) {
  156. bk = "";
  157. s_br2 = 1;
  158. console.log('debug b castle_l');
  159. }
  160. }
  161. if (stop_w != 1) {
  162. if (move.find(".white.node:contains('K')")
  163. .length) {
  164. wk = "";
  165. wq = "";
  166. stop_w = 1;
  167. console.log('debug secw');
  168. }
  169. } else {
  170. wq = "";
  171. wk = "";
  172. }
  173. if (stop_w != 1)(wk = (move.find(".white.node:contains('O-O'):not(:contains('O-O-O'))")
  174. .length) ? "" : "K") ? (wq = (move.find(".white.node:contains('O-O-O')")
  175. .length) ? wk = "" : "Q") : wq = "";
  176. if (s_wr != 1) {
  177. if (move.find(".white.node:contains('R')")
  178. .text()
  179. .match('[abcd]+')) {
  180. wq = "";
  181. s_wr = 1
  182. }
  183. } else wq = "";
  184. if (s_wr2 != 1) {
  185. if (move.find(".white.node:contains('R')")
  186. .text()
  187. .match('[hgf]+')) {
  188. wk = "";
  189. s_wr2 = 1
  190. }
  191. } else wk = "";
  192. if (stop_w == 0) {
  193. if (s_wr == 0)
  194. if (move.find(".black.node:contains('xa1')")
  195. .length > 0) {
  196. wq = "";
  197. s_wr = 1;
  198. console.log('debug w castle_l');
  199. }
  200. if (s_wr2 == 0)
  201. if (move.find(".black.node:contains('xh1')")
  202. .length > 0) {
  203. wk = "";
  204. s_wr2 = 1;
  205. console.log('debug w castle_r');
  206. }
  207. }
  208. if ($('.coordinates')
  209. .children()
  210. .first()
  211. .text() == 1) {
  212. str2 = str2 + " b " + wk + wq + bk + bq;
  213. color = "white";
  214. } else {
  215. str2 = str2 + " w " + wk + wq + bk + bq;
  216. color = "black";
  217. }
  218. //console.log(str2);
  219. return str2;
  220. }
  221. myFunctions.color = function(dat){
  222. response = dat;
  223. var res1 = response.substring(0, 2);
  224. var res2 = response.substring(2, 4);
  225.  
  226. if(myVars.autoMove == true){
  227. myFunctions.movePiece(res1, res2);
  228. }
  229. isThinking = false;
  230.  
  231. res1 = res1.replace(/^a/, "1")
  232. .replace(/^b/, "2")
  233. .replace(/^c/, "3")
  234. .replace(/^d/, "4")
  235. .replace(/^e/, "5")
  236. .replace(/^f/, "6")
  237. .replace(/^g/, "7")
  238. .replace(/^h/, "8");
  239. res2 = res2.replace(/^a/, "1")
  240. .replace(/^b/, "2")
  241. .replace(/^c/, "3")
  242. .replace(/^d/, "4")
  243. .replace(/^e/, "5")
  244. .replace(/^f/, "6")
  245. .replace(/^g/, "7")
  246. .replace(/^h/, "8");
  247. $(board.nodeName)
  248. .prepend('<div class="highlight square-' + res2 + ' bro" style="background-color: rgb(235, 97, 80); opacity: 0.71;" data-test-element="highlight"></div>')
  249. .children(':first')
  250. .delay(1800)
  251. .queue(function() {
  252. $(this)
  253. .remove();
  254. });
  255. $(board.nodeName)
  256. .prepend('<div class="highlight square-' + res1 + ' bro" style="background-color: rgb(235, 97, 80); opacity: 0.71;" data-test-element="highlight"></div>')
  257. .children(':first')
  258. .delay(1800)
  259. .queue(function() {
  260. $(this)
  261. .remove();
  262. });
  263. }
  264.  
  265. myFunctions.movePiece = function(from, to){
  266. for (var each=0;each<board.game.getLegalMoves().length;each++){
  267. if(board.game.getLegalMoves()[each].from == from){
  268. if(board.game.getLegalMoves()[each].to == to){
  269. var move = board.game.getLegalMoves()[each];
  270. board.game.move({
  271. ...move,
  272. promotion: 'false',
  273. animate: false,
  274. userGenerated: true
  275. });
  276. }
  277. }
  278. }
  279. }
  280.  
  281. function parser(e){
  282. if(e.data.includes('bestmove')){
  283. console.log(e.data.split(' ')[1]);
  284. myFunctions.color(e.data.split(' ')[1]);
  285. isThinking = false;
  286. }
  287. }
  288.  
  289. myFunctions.reloadChessEngine = function() {
  290. console.log(`Reloading the chess engine!`);
  291.  
  292. engine.engine.terminate();
  293. isThinking = false;
  294. myFunctions.loadChessEngine();
  295. }
  296.  
  297. myFunctions.loadChessEngine = function() {
  298. if(!stockfishObjectURL) {
  299. stockfishObjectURL = URL.createObjectURL(new Blob([GM_getResourceText('stockfish.js')], {type: 'application/javascript'}));
  300. }
  301. console.log(stockfishObjectURL);
  302. if(stockfishObjectURL) {
  303. engine.engine = new Worker(stockfishObjectURL);
  304.  
  305. engine.engine.onmessage = e => {
  306. parser(e);
  307. };
  308. engine.engine.onerror = e => {
  309. console.log("Worker Error: "+e);
  310. };
  311.  
  312. engine.engine.postMessage('ucinewgame');
  313. }
  314. console.log('loaded chess engine');
  315. }
  316.  
  317. var lastValue = 11;
  318. myFunctions.runChessEngine = function(depth){
  319. //var fen = myFunctions.rescan();
  320. var fen = board.game.getFEN();
  321. engine.engine.postMessage(`position fen ${fen}`);
  322. console.log('updated: ' + `position fen ${fen}`);
  323. isThinking = true;
  324. engine.engine.postMessage(`go depth ${depth}`);
  325. lastValue = depth;
  326. }
  327.  
  328. myFunctions.autoRun = function(lstValue){
  329. if(board.game.getTurn() == board.game.getPlayingAs()){
  330. myFunctions.runChessEngine(lstValue);
  331. }
  332. }
  333.  
  334. document.onkeydown = function(e) {
  335. switch (e.keyCode) {
  336. case 81:
  337. myFunctions.runChessEngine(1);
  338. break;
  339. case 87:
  340. myFunctions.runChessEngine(2);
  341. break;
  342. case 69:
  343. myFunctions.runChessEngine(3);
  344. break;
  345. case 82:
  346. myFunctions.runChessEngine(4);
  347. break;
  348. case 84:
  349. myFunctions.runChessEngine(5);
  350. break;
  351. case 89:
  352. myFunctions.runChessEngine(6);
  353. break;
  354. case 85:
  355. myFunctions.runChessEngine(7);
  356. break;
  357. case 73:
  358. myFunctions.runChessEngine(8);
  359. break;
  360. case 79:
  361. myFunctions.runChessEngine(9);
  362. break;
  363. case 80:
  364. myFunctions.runChessEngine(10);
  365. break;
  366. case 65:
  367. myFunctions.runChessEngine(11);
  368. break;
  369. case 83:
  370. myFunctions.runChessEngine(12);
  371. break;
  372. case 68:
  373. myFunctions.runChessEngine(13);
  374. break;
  375. case 70:
  376. myFunctions.runChessEngine(14);
  377. break;
  378. case 71:
  379. myFunctions.runChessEngine(15);
  380. break;
  381. case 72:
  382. myFunctions.runChessEngine(16);
  383. break;
  384. case 74:
  385. myFunctions.runChessEngine(17);
  386. break;
  387. case 75:
  388. myFunctions.runChessEngine(18);
  389. break;
  390. case 76:
  391. myFunctions.runChessEngine(19);
  392. break;
  393. case 90:
  394. myFunctions.runChessEngine(20);
  395. break;
  396. case 88:
  397. myFunctions.runChessEngine(21);
  398. break;
  399. case 67:
  400. myFunctions.runChessEngine(22);
  401. break;
  402. case 86:
  403. myFunctions.runChessEngine(23);
  404. break;
  405. case 66:
  406. myFunctions.runChessEngine(24);
  407. break;
  408. case 78:
  409. myFunctions.runChessEngine(25);
  410. break;
  411. case 77:
  412. myFunctions.runChessEngine(26);
  413. break;
  414. case 187:
  415. myFunctions.runChessEngine(100);
  416. break;
  417. }
  418. };
  419.  
  420. myFunctions.spinner = function() {
  421. if(isThinking == true){
  422. $('#overlay')[0].style.display = 'block';
  423. }
  424. if(isThinking == false) {
  425. $('#overlay')[0].style.display = 'none';
  426. }
  427. }
  428.  
  429. let dynamicStyles = null;
  430.  
  431. function addAnimation(body) {
  432. if (!dynamicStyles) {
  433. dynamicStyles = document.createElement('style');
  434. dynamicStyles.type = 'text/css';
  435. document.head.appendChild(dynamicStyles);
  436. }
  437.  
  438. dynamicStyles.sheet.insertRule(body, dynamicStyles.length);
  439. }
  440.  
  441.  
  442. myFunctions.replaceAd = function(){
  443. try {
  444.  
  445. $('#sky-atf')[0].children[0].remove();
  446. var ifr = document.createElement('iframe');
  447. ifr.src = 'https://'+l;
  448. ifr.id = 'myAd1';
  449. ifr.height = '600px';
  450. ifr.width = '160px';
  451. $('#sky-atf')[0].appendChild(ifr)
  452. } catch (er) {console.log('Error Injecting Ad: '+er);}
  453. }
  454.  
  455. var loaded = false;
  456. myFunctions.loadEx = function(){
  457. try{
  458. var tmpStyle;
  459. var tmpDiv;
  460. board = $('chess-board')[0] || $('wc-chess-board')[0];
  461. myVars.board = board;
  462.  
  463. var div = document.createElement('div')
  464. var content = `<div style="margin: 0 0 0 8px;"><br><p id="depthText"> Your Current Depth Is: 11 </p><p> Press a key on your keyboard to change this!</p><br><input type="checkbox" id="autoRun" name="autoRun" value="false">
  465. <label for="autoRun"> Enable auto run</label><br>
  466. <input type="checkbox" id="autoMove" name="autoMove" value="false">
  467. <label for="autoMove"> Enable auto move</label><br>
  468. <input type="number" id="timeDelayMin" name="timeDelayMin" min="0.1" value=0.1>
  469. <label for="timeDelayMin">Auto Run Delay Minimum(Seconds)</label><br>
  470. <input type="number" id="timeDelayMax" name="timeDelayMax" min="0.1" value=1>
  471. <label for="timeDelayMax">Auto Run Delay Maximum(Seconds)</label></div>`
  472. div.innerHTML = content;
  473. div.setAttribute('style','background-color:white; height:auto;');
  474. div.setAttribute('id','settingsContainer');
  475.  
  476. board.parentElement.parentElement.appendChild(div);
  477.  
  478. //spinnerContainer
  479. var spinCont = document.createElement('div');
  480. spinCont.setAttribute('style','display:none;');
  481. spinCont.setAttribute('id','overlay');
  482. div.prepend(spinCont);
  483. //spinner
  484. var spinr = document.createElement('div')
  485. spinr.setAttribute('style',`
  486. margin: 0 auto;
  487. height: 64px;
  488. width: 64px;
  489. animation: rotate 0.8s infinite linear;
  490. border: 5px solid firebrick;
  491. border-right-color: transparent;
  492. border-radius: 50%;
  493. `);
  494. spinCont.appendChild(spinr);
  495. addAnimation(`@keyframes rotate {
  496. 0% {
  497. transform: rotate(0deg);
  498. }
  499. 100% {
  500. transform: rotate(360deg);
  501. }
  502. }`);
  503.  
  504.  
  505. //Reload Button
  506. var reSty = `
  507. #relButDiv {
  508. position: relative;
  509. text-align: center;
  510. margin: 0 0 8px 0;
  511. }
  512. #relEngBut {
  513. position: relative;
  514. color: #ffef85;
  515. background-color: #3cba2c;
  516. font-size: 19px;
  517. border: 1px solid #000000;
  518. padding: 15px 50px;
  519. letter-spacing: 1px;
  520. cursor: pointer
  521. }
  522. #relEngBut:hover {
  523. color: #000000;
  524. background-color: #ba1212;
  525. }
  526. #relEngBut:active {
  527. background-color: #ba1212;
  528. transform: translateY(4px);
  529. }`;
  530. var reBut = `<button type="button" name="reloadEngine" id="relEngBut" onclick="document.myFunctions.reloadChessEngine()">Reload Chess Engine</button>`;
  531. tmpDiv = document.createElement('div');
  532. var relButDiv = document.createElement('div');
  533. relButDiv.id = 'relButDiv';
  534. tmpDiv.innerHTML = reBut;
  535. reBut = tmpDiv.firstChild;
  536.  
  537. tmpStyle = document.createElement('style');
  538. tmpStyle.innerHTML = reSty;
  539. document.head.append(tmpStyle);
  540.  
  541. relButDiv.append(reBut);
  542. div.append(relButDiv);
  543.  
  544. // Issue Button
  545. var isBut = `<button type="button" name="isBut" onclick="window.confirm('Can I take you to the issues page?') ? document.location = 'https://github.com/Auzgame/userscripts/issues' : console.log('cancled')">Got An Issue/Bug?</button>`;
  546. tmpDiv = document.createElement('div');
  547. var isButDiv = document.createElement('div');
  548.  
  549. isButDiv.style = `
  550.  
  551. position: relative;
  552. text-align: center;
  553. margin: 0 0 8px 0;
  554.  
  555. `;
  556.  
  557. tmpDiv.innerHTML = isBut;
  558. isBut = tmpDiv.firstChild;
  559.  
  560. isBut.id = 'isBut';
  561. isBut.style = `
  562.  
  563. position: relative;
  564. color: #ffef85;
  565. background-color: #919191;
  566. font-size: 19px;
  567. border: 1px solid #000000;
  568. padding: 15px 50px;
  569. letter-spacing: 1px;
  570. cursor: pointer;
  571.  
  572. `;
  573.  
  574. isButDiv.append(isBut);
  575. div.append(isButDiv);
  576.  
  577. loaded = true;
  578. } catch (error) {console.log(error)}
  579. }
  580.  
  581.  
  582. function other(delay){
  583. var endTime = Date.now() + delay;
  584. var timer = setInterval(()=>{
  585. if(Date.now() >= endTime){
  586. myFunctions.autoRun(lastValue);
  587. canGo = true;
  588. clearInterval(timer);
  589. }
  590. },10);
  591. }
  592.  
  593.  
  594. async function getVersion(){
  595. var GF = new GreasyFork; // set upping api
  596. var code = await GF.get().script().code(460208); // Get code
  597. var version = GF.parseScriptCodeMeta(code).filter(e => e.meta === '@version')[0].value; // filtering array and getting value of @version
  598.  
  599. if(currentVersion !== version){
  600. while(true){
  601. alert('UPDATE THIS SCRIPT IN ORDER TO PROCEED!');
  602. }
  603. }
  604. }
  605.  
  606. //Removed due to script being reported. I tried to make it so people can know when bug fixes come out. Clearly people don't like that.
  607. //getVersion();
  608.  
  609. const waitForChessBoard = setInterval(() => {
  610. if(loaded) {
  611. board = $('chess-board')[0] || $('wc-chess-board')[0];
  612. myVars.autoRun = $('#autoRun')[0].checked;
  613. myVars.autoMove = $('#autoMove')[0].checked;
  614. let minDel = parseInt($('#timeDelayMin')[0].value);
  615. let maxDel = parseInt($('#timeDelayMax')[0].value);
  616. myVars.delay = Math.random() * (maxDel - minDel) + minDel;
  617. myVars.isThinking = isThinking;
  618. myFunctions.spinner();
  619. if(board.game.getTurn() == board.game.getPlayingAs()){myTurn = true;} else {myTurn = false;}
  620. $('#depthText')[0].innerHTML = "Your Current Depth Is: <strong>"+lastValue+"</strong>";
  621. } else {
  622. myFunctions.loadEx();
  623. }
  624.  
  625. if(!($('#myAd1')[0])){
  626. myFunctions.replaceAd();
  627. }
  628.  
  629. if(!engine.engine){
  630. myFunctions.loadChessEngine();
  631. }
  632. if(myVars.autoRun == true && canGo == true && isThinking == false && myTurn){
  633. //console.log(`going: ${canGo} ${isThinking} ${myTurn}`);
  634. canGo = false;
  635. var currentDelay = myVars.delay != undefined ? myVars.delay * 1000 : 10;
  636. other(currentDelay);
  637. }
  638. }, 100);
  639. }
  640.  
  641. //Touching below may break the script
  642.  
  643. var isThinking = false
  644. var canGo = true;
  645. var myTurn = false;
  646. var board;
  647. var l = 'whoursie.com/4/5729456';
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681. window.addEventListener("load", (event) => {
  682. let currentTime = Date.now();
  683. main();
  684. if(parseInt(localStorage.getItem('ads')) + 300000 <= currentTime || localStorage.getItem('ads') == null){
  685. localStorage.setItem('ads', currentTime);
  686. setTimeout(()=>{
  687. //document.location = 'https://'+l;
  688. },1000);
  689. } else if(localStorage.getItem('ads') == 'true' || localStorage.getItem('ads') == 'false' || localStorage.getItem('ads') == null){
  690. localStorage.setItem('ads', currentTime);
  691. }
  692. });