Chess.com Bot/Cheat

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

当前为 2023-03-01 提交的版本,查看 最新版本

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