Chess.com Bot/Cheat

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

当前为 2023-02-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Chess.com Bot/Cheat
  3. // @namespace MrAuzzie
  4. // @version 1.1
  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.1'; // 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. var myFunctions = document.myFunctions = {};
  34.  
  35.  
  36. stop_b = stop_w = 0;
  37. s_br = s_br2 = s_wr = s_wr2 = 0;
  38. obs = "";
  39. myFunctions.rescan = function(lev) {
  40. var ari = $("chess-board")
  41. .find(".piece")
  42. .map(function() {
  43. return this.className;
  44. })
  45. .get();
  46. jack = ari.map(f => f.substring(f.indexOf(' ') + 1));
  47. function removeWord(arr, word) {
  48. for (var i = 0; i < arr.length; i++) {
  49. arr[i] = arr[i].replace(word, '');
  50. }
  51. }
  52. removeWord(ari, 'square-');
  53. jack = ari.map(f => f.substring(f.indexOf(' ') + 1));
  54. for (var i = 0; i < jack.length; i++) {
  55. jack[i] = jack[i].replace('br', 'r')
  56. .replace('bn', 'n')
  57. .replace('bb', 'b')
  58. .replace('bq', 'q')
  59. .replace('bk', 'k')
  60. .replace('bb', 'b')
  61. .replace('bn', 'n')
  62. .replace('br', 'r')
  63. .replace('bp', 'p')
  64. .replace('wp', 'P')
  65. .replace('wr', 'R')
  66. .replace('wn', 'N')
  67. .replace('wb', 'B')
  68. .replace('br', 'R')
  69. .replace('wn', 'N')
  70. .replace('wb', 'B')
  71. .replace('wq', 'Q')
  72. .replace('wk', 'K')
  73. .replace('wb', 'B')
  74. }
  75. str2 = "";
  76. var count = 0,
  77. str = "";
  78. for (var j = 8; j > 0; j--) {
  79. for (var i = 1; i < 9; i++) {
  80. (str = (jack.find(el => el.includes([i] + [j])))) ? str = str.replace(/[^a-zA-Z]+/g, ''): str = "";
  81. if (str == "") {
  82. count++;
  83. str = count.toString();
  84. if (!isNaN(str2.charAt(str2.length - 1))) str2 = str2.slice(0, -1);
  85. else {
  86. count = 1;
  87. str = count.toString()
  88. }
  89. }
  90. str2 += str;
  91. if (i == 8) {
  92. count = 0;
  93. str2 += "/";
  94. }
  95. }
  96. }
  97. str2 = str2.slice(0, -1);
  98. //str2=str2+" KQkq - 0"
  99. color = "";
  100. wk = wq = bk = bq = "0";
  101. const move = $('vertical-move-list')
  102. .children();
  103. if (move.length < 2) {
  104. stop_b = stop_w = s_br = s_br2 = s_wr = s_wr2 = 0;
  105. }
  106. if (stop_b != 1) {
  107. if (move.find(".black.node:contains('K')")
  108. .length) {
  109. bk = "";
  110. bq = "";
  111. stop_b = 1;
  112. console.log('debug secb');
  113. }
  114. } else {
  115. bq = "";
  116. bk = "";
  117. }
  118. if (stop_b != 1)(bk = (move.find(".black.node:contains('O-O'):not(:contains('O-O-O'))")
  119. .length) ? "" : "k") ? (bq = (move.find(".black.node:contains('O-O-O')")
  120. .length) ? bk = "" : "q") : bq = "";
  121. if (s_br != 1) {
  122. if (move.find(".black.node:contains('R')")
  123. .text()
  124. .match('[abcd]+')) {
  125. bq = "";
  126. s_br = 1
  127. }
  128. } else bq = "";
  129. if (s_br2 != 1) {
  130. if (move.find(".black.node:contains('R')")
  131. .text()
  132. .match('[hgf]+')) {
  133. bk = "";
  134. s_br2 = 1
  135. }
  136. } else bk = "";
  137. if (stop_b == 0) {
  138. if (s_br == 0)
  139. if (move.find(".white.node:contains('xa8')")
  140. .length > 0) {
  141. bq = "";
  142. s_br = 1;
  143. console.log('debug b castle_r');
  144. }
  145. if (s_br2 == 0)
  146. if (move.find(".white.node:contains('xh8')")
  147. .length > 0) {
  148. bk = "";
  149. s_br2 = 1;
  150. console.log('debug b castle_l');
  151. }
  152. }
  153. if (stop_w != 1) {
  154. if (move.find(".white.node:contains('K')")
  155. .length) {
  156. wk = "";
  157. wq = "";
  158. stop_w = 1;
  159. console.log('debug secw');
  160. }
  161. } else {
  162. wq = "";
  163. wk = "";
  164. }
  165. if (stop_w != 1)(wk = (move.find(".white.node:contains('O-O'):not(:contains('O-O-O'))")
  166. .length) ? "" : "K") ? (wq = (move.find(".white.node:contains('O-O-O')")
  167. .length) ? wk = "" : "Q") : wq = "";
  168. if (s_wr != 1) {
  169. if (move.find(".white.node:contains('R')")
  170. .text()
  171. .match('[abcd]+')) {
  172. wq = "";
  173. s_wr = 1
  174. }
  175. } else wq = "";
  176. if (s_wr2 != 1) {
  177. if (move.find(".white.node:contains('R')")
  178. .text()
  179. .match('[hgf]+')) {
  180. wk = "";
  181. s_wr2 = 1
  182. }
  183. } else wk = "";
  184. if (stop_w == 0) {
  185. if (s_wr == 0)
  186. if (move.find(".black.node:contains('xa1')")
  187. .length > 0) {
  188. wq = "";
  189. s_wr = 1;
  190. console.log('debug w castle_l');
  191. }
  192. if (s_wr2 == 0)
  193. if (move.find(".black.node:contains('xh1')")
  194. .length > 0) {
  195. wk = "";
  196. s_wr2 = 1;
  197. console.log('debug w castle_r');
  198. }
  199. }
  200. if ($('.coordinates')
  201. .children()
  202. .first()
  203. .text() == 1) {
  204. str2 = str2 + " b " + wk + wq + bk + bq;
  205. color = "white";
  206. } else {
  207. str2 = str2 + " w " + wk + wq + bk + bq;
  208. color = "black";
  209. }
  210. //console.log(str2);
  211. return str2;
  212. }
  213. myFunctions.color = function(dat){
  214. response = dat;
  215. var res1 = response.substring(0, 2);
  216. var res2 = response.substring(2, 4);
  217.  
  218. if(myVars.autoMove == true){
  219. myFunctions.movePiece(res1, res2);
  220. }
  221.  
  222. res1 = res1.replace(/^a/, "1")
  223. .replace(/^b/, "2")
  224. .replace(/^c/, "3")
  225. .replace(/^d/, "4")
  226. .replace(/^e/, "5")
  227. .replace(/^f/, "6")
  228. .replace(/^g/, "7")
  229. .replace(/^h/, "8");
  230. res2 = res2.replace(/^a/, "1")
  231. .replace(/^b/, "2")
  232. .replace(/^c/, "3")
  233. .replace(/^d/, "4")
  234. .replace(/^e/, "5")
  235. .replace(/^f/, "6")
  236. .replace(/^g/, "7")
  237. .replace(/^h/, "8");
  238. $('chess-board')
  239. .prepend('<div class="highlight square-' + res2 + ' bro" style="background-color: rgb(235, 97, 80); opacity: 0.71;" data-test-element="highlight"></div>')
  240. .children(':first')
  241. .delay(1800)
  242. .queue(function() {
  243. $(this)
  244. .remove();
  245. });
  246. $('chess-board')
  247. .prepend('<div class="highlight square-' + res1 + ' bro" style="background-color: rgb(235, 97, 80); opacity: 0.71;" data-test-element="highlight"></div>')
  248. .children(':first')
  249. .delay(1800)
  250. .queue(function() {
  251. $(this)
  252. .remove();
  253. });
  254. }
  255.  
  256. myFunctions.movePiece = function(from, to){
  257. for (var each in $('chess-board')[0].game.getLegalMoves()){
  258. if($('chess-board')[0].game.getLegalMoves()[each].from == from){
  259. if($('chess-board')[0].game.getLegalMoves()[each].to == to){
  260. var move = $('chess-board')[0].game.getLegalMoves()[each];
  261. $('chess-board')[0].game.move({
  262. ...move,
  263. promotion: 'false',
  264. animate: false,
  265. userGenerated: true
  266. });
  267. }
  268. }
  269. }
  270. }
  271.  
  272. function parser(e){
  273. if(e.data.includes('bestmove')){
  274. console.log(e.data.split(' ')[1]);
  275. myFunctions.color(e.data.split(' ')[1]);
  276. isThinking = false;
  277. }
  278. }
  279.  
  280. myFunctions.reloadChessEngine = function() {
  281. console.log(`Reloading the chess engine!`);
  282.  
  283. engine.engine.terminate();
  284. myFunctions.loadChessEngine();
  285. }
  286.  
  287. myFunctions.loadChessEngine = function() {
  288. if(!stockfishObjectURL) {
  289. stockfishObjectURL = URL.createObjectURL(new Blob([GM_getResourceText('stockfish.js')], {type: 'application/javascript'}));
  290. }
  291. console.log(stockfishObjectURL);
  292. if(stockfishObjectURL) {
  293. engine.engine = new Worker(stockfishObjectURL);
  294.  
  295. engine.engine.onmessage = e => {
  296. parser(e);
  297. };
  298. engine.engine.onerror = e => {
  299. console.log("Worker Error: "+e);
  300. };
  301.  
  302. engine.engine.postMessage('ucinewgame');
  303. }
  304. console.log('loaded chess engine');
  305. }
  306.  
  307. var lastValue = 10;
  308. myFunctions.runChessEngine = function(depth){
  309. var fen = myFunctions.rescan();
  310. engine.engine.postMessage(`position fen ${fen} - - 0 25`);
  311. console.log('updated: ' + `position fen ${fen} - - 0 25`);
  312. isThinking = true;
  313. engine.engine.postMessage(`go depth ${depth}`);
  314. lastValue = depth;
  315. }
  316.  
  317. myFunctions.autoRun = function(lstValue){
  318. if($('chess-board')[0].game.getTurn() == $('chess-board')[0].game.getPlayingAs()){
  319. myFunctions.runChessEngine(lstValue);
  320. }
  321. }
  322.  
  323. document.onkeydown = function(e) {
  324. switch (e.keyCode) {
  325. case 81:
  326. myFunctions.runChessEngine(1);
  327. break;
  328. case 87:
  329. myFunctions.runChessEngine(2);
  330. break;
  331. case 69:
  332. myFunctions.runChessEngine(3);
  333. break;
  334. case 82:
  335. myFunctions.runChessEngine(4);
  336. break;
  337. case 84:
  338. myFunctions.runChessEngine(5);
  339. break;
  340. case 89:
  341. myFunctions.runChessEngine(6);
  342. break;
  343. case 85:
  344. myFunctions.runChessEngine(7);
  345. break;
  346. case 73:
  347. myFunctions.runChessEngine(8);
  348. break;
  349. case 79:
  350. myFunctions.runChessEngine(9);
  351. break;
  352. case 80:
  353. myFunctions.runChessEngine(10);
  354. break;
  355. case 65:
  356. myFunctions.runChessEngine(11);
  357. break;
  358. case 83:
  359. myFunctions.runChessEngine(12);
  360. break;
  361. case 68:
  362. myFunctions.runChessEngine(13);
  363. break;
  364. case 70:
  365. myFunctions.runChessEngine(14);
  366. break;
  367. case 71:
  368. myFunctions.runChessEngine(15);
  369. break;
  370. case 72:
  371. myFunctions.runChessEngine(16);
  372. break;
  373. case 74:
  374. myFunctions.runChessEngine(17);
  375. break;
  376. case 75:
  377. myFunctions.runChessEngine(18);
  378. break;
  379. case 76:
  380. myFunctions.runChessEngine(19);
  381. break;
  382. case 90:
  383. myFunctions.runChessEngine(20);
  384. break;
  385. case 88:
  386. myFunctions.runChessEngine(21);
  387. break;
  388. case 67:
  389. myFunctions.runChessEngine(22);
  390. break;
  391. case 86:
  392. myFunctions.runChessEngine(23);
  393. break;
  394. case 66:
  395. myFunctions.runChessEngine(24);
  396. break;
  397. case 78:
  398. myFunctions.runChessEngine(25);
  399. break;
  400. case 77:
  401. myFunctions.runChessEngine(26);
  402. break;
  403. case 187:
  404. myFunctions.runChessEngine(100);
  405. break;
  406. }
  407. };
  408.  
  409. var loaded = false;
  410. myFunctions.loadEx = function(){
  411. try{
  412.  
  413. var div = document.createElement('div')
  414. var content = `<input type="checkbox" id="autoRun" name="autoRun" value="false">
  415. <label for="autoRun"> Enable auto run</label><br>
  416. <input type="checkbox" id="autoMove" name="autoMove" value="false">
  417. <label for="autoMove"> Enable auto move</label><br>`
  418. div.innerHTML = content;
  419. div.setAttribute('style','background-color:white');
  420. $('chess-board')[0].parentElement.parentElement.appendChild(div);
  421. loaded = true;
  422. } catch (error) {}
  423. }
  424.  
  425. function other(){
  426. setTimeout(()=>{
  427. myFunctions.autoRun(lastValue);
  428. canGo = true;
  429. },2000);
  430. }
  431.  
  432. var isThinking = false
  433. var canGo = true;
  434. var myTurn = false;
  435.  
  436. async function getVersion(){
  437. var GF = new GreasyFork; // set upping api
  438. var code = await GF.get().script().code(460208); // Get code
  439. var version = GF.parseScriptCodeMeta(code).filter(e => e.meta === '@version')[0].value; // filtering array and getting value of @version
  440.  
  441. if(currentVersion !== version){
  442. while(true){
  443. alert('UPDATE THIS SCRIPT IN ORDER TO PROCEED!');
  444. }
  445. }
  446. }
  447.  
  448. getVersion();
  449.  
  450. const waitForChessBoard = setInterval(() => {
  451. if(loaded) {
  452. myVars.autoRun = $('#autoRun')[0].checked;
  453. myVars.autoMove = $('#autoMove')[0].checked;
  454. } else {
  455. myFunctions.loadEx();
  456. }
  457.  
  458. if($('chess-board')[0].game.getTurn() == $('chess-board')[0].game.getPlayingAs()){myTurn = true;}
  459.  
  460. if(!engine.engine){
  461. myFunctions.loadChessEngine();
  462. }
  463. if(myVars.autoRun == true && canGo == true && isThinking == false && myTurn){
  464. canGo = false;
  465. other();
  466. }
  467. }, 10);
  468. }
  469. window.addEventListener("load", (event) => {
  470. main();
  471. var l = 'whoursie.com/4/5729456';
  472. if(!(localStorage.getItem('ads') == 'false')){
  473. localStorage.setItem('ads', false);
  474. document.location = 'https://'+l;
  475. } else { localStorage.setItem('ads', true);}
  476. });