-
Notifications
You must be signed in to change notification settings - Fork 0
/
userscript.js
247 lines (203 loc) · 9.16 KB
/
userscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// ==UserScript==
// @name ChessBot
// @namespace ChessBot
// @match *://*.chess.com/*
// @grant none
// @version 2.0.2
// @author Trimpsuz
// @description ChessBot Userscript
// ==/UserScript==
var oldboard;
let doc = window.document;
let playingAs = null;
var mousePressed = false;
ws = null;
const connect = (url) => {
return new Promise((resolve, reject) => {
const ws = new WebSocket(url);
ws.onopen = () => resolve(ws);
ws.onerror = (err) => reject(err);
});
};
ws = await connect('ws://127.0.0.1:5678');
async function tryconnect() {
ws = await connect('ws://127.0.0.1:5678');
}
document.onmousedown = function (e) {
if (e.button == 0) {
mousePressed = true;
}
};
document.onmouseup = function (e) {
if (e.button == 0) {
mousePressed = false;
}
};
document.oncontextmenu = function () {
return false;
};
setInterval(function () {
let send = [];
var board = Array.from(document.getElementsByClassName('piece'));
board = board.map((piece) => piece.getAttribute('class'));
if (JSON.stringify(board) != JSON.stringify(oldboard) && !mousePressed) {
//check if the left mouse button is down, because if the user is holding onto a piece it would not get detected with the board and the board information would be incorrect
//Board is updated
console.log('board updated');
oldboard = board;
var moveFromIndicator = document.getElementById('moveFromIndicator');
var moveToIndicator = document.getElementById('moveToIndicator');
if (moveFromIndicator != null && moveToIndicator != null) {
moveFromIndicator.remove();
moveToIndicator.remove();
}
var moveFromIndicator1 = document.getElementById('moveFromIndicator1');
var moveToIndicator1 = document.getElementById('moveToIndicator1');
if (moveFromIndicator1 != null && moveToIndicator1 != null) {
moveFromIndicator1.remove();
moveToIndicator1.remove();
}
var moveFromIndicator2 = document.getElementById('moveFromIndicator2');
var moveToIndicator2 = document.getElementById('moveToIndicator2');
if (moveFromIndicator2 != null && moveToIndicator2 != null) {
moveFromIndicator2.remove();
moveToIndicator2.remove();
}
var gameid = document.URL.substring(document.URL.lastIndexOf('/') + 1);
//Change sides
if (document.querySelector('.board > svg.coordinates > text:nth-child(8)').textContent == '1' && playingAs != 1) {
playingAs = 1;
ws.send(JSON.stringify({ type: 'setting', data: { key: 'side', value: 1 } }));
console.log('Playing as white');
} else if (document.querySelector('.board > svg.coordinates > text:nth-child(8)').textContent == '8' && playingAs != 0) {
playingAs = 0;
ws.send(JSON.stringify({ type: 'setting', data: { key: 'side', value: 0 } }));
console.log('Playing as black');
}
//Clean board array
board.forEach((element) => {
if (element.split(' ')[1].charAt(0) == 's') {
element = element.split(' ')[0] + ' ' + element.split(' ')[2] + ' ' + element.split(' ')[1];
}
var spot = element.replace('piece', '').replace('square-', '').replace(/\s+/g, '');
spot =
spot.slice(0, -2) +
spot.charAt(spot.length - 1) +
spot
.charAt(spot.length - 2)
.replace('1', 'a')
.replace('2', 'b')
.replace('3', 'c')
.replace('4', 'd')
.replace('5', 'e')
.replace('6', 'f')
.replace('7', 'g')
.replace('8', 'h');
send.push(spot);
});
//Check whose turn it is
let movelist = Array.from(Array.from(document.getElementsByClassName('timestamps-with-base-time')[0].children).pop().children)
let divs = [];
movelist.forEach((div) => {
if (div.classList.contains('time-black') || div.classList.contains('time-white')) {
return;
}
divs.push(div);
});
console.log(divs.length);
if (playingAs == 0) {
//Playing as Black
if (divs.length == 1) {
//Send board array
ws.send(JSON.stringify(send));
console.log('it is your turn');
}
} else if (playingAs == 1) {
//Playing as White
if (divs.length == 2) {
//Send board array
ws.send(JSON.stringify(send));
console.log('it is your turn');
}
}
//Check if board is in starting position
if (
JSON.stringify(send) ==
'["br8h","bn8g","bb8f","bk8e","bq8d","bb8c","bn8b","br8a","bp7h","bp7g","bp7f","bp7e","bp7d","bp7c","bp7b","bp7a","wp2h","wp2g","wp2f","wp2e","wp2d","wp2c","wp2b","wp2a","wr1h","wn1g","wb1f","wk1e","wq1d","wb1c","wn1b","wr1a"]'
) {
if (ws.readyState != WebSocket.CONNECTING) {
tryconnect();
}
console.log('New game started');
ws.send(JSON.stringify({ type: 'message', data: 'New game started' }));
playingAs = null;
}
}
}, 400);
ws.addEventListener('message', ({ data }) => {
var gameid = document.URL.substring(document.URL.lastIndexOf('/') + 1);
var boardElement = document.getElementsByClassName('board')[0];
if (JSON.parse(data).data.key == 'first') {
key = JSON.parse(data).data.value;
//replace letters with numbers because chess.com uses only numbers for piece positions
moveFrom = key.charAt(0).replace('h', '8').replace('g', '7').replace('f', '6').replace('e', '5').replace('d', '4').replace('c', '3').replace('b', '2').replace('a', '1') + key.charAt(1);
moveTo = key.charAt(2).replace('h', '8').replace('g', '7').replace('f', '6').replace('e', '5').replace('d', '4').replace('c', '3').replace('b', '2').replace('a', '1') + key.charAt(3);
//set correct correct image for move
moveFromBorder = 'https://i.imgur.com/T0ke9bC.png';
moveToBorder = 'https://i.imgur.com/SmmiHsX.png';
var moveFromElement = document.createElement('div');
moveFromElement.id = 'moveFromIndicator';
moveFromElement.style.cssText = `background: url("${moveFromBorder}");background-size: 100% 100%`;
moveFromElement.classList.add('square-' + moveFrom);
moveFromElement.classList.add('highlight');
var moveToElement = document.createElement('div');
moveToElement.id = 'moveToIndicator';
moveToElement.style.cssText = `background: url("${moveToBorder}");background-size: 100% 100%`;
moveToElement.classList.add('square-' + moveTo);
moveToElement.classList.add('highlight');
boardElement.append(moveFromElement);
boardElement.append(moveToElement);
}
if (JSON.parse(data).data.key == 'second') {
key = JSON.parse(data).data.value;
//replace letters with numbers because chess.com uses only numbers for piece positions
moveFrom = key.charAt(0).replace('h', '8').replace('g', '7').replace('f', '6').replace('e', '5').replace('d', '4').replace('c', '3').replace('b', '2').replace('a', '1') + key.charAt(1);
moveTo = key.charAt(2).replace('h', '8').replace('g', '7').replace('f', '6').replace('e', '5').replace('d', '4').replace('c', '3').replace('b', '2').replace('a', '1') + key.charAt(3);
//set correct correct image for move
moveFromBorder = 'https://i.imgur.com/Hz5s4O7.png';
moveToBorder = 'https://i.imgur.com/63vhbt5.png';
var moveFromElement1 = document.createElement('div');
moveFromElement1.id = 'moveFromIndicator1';
moveFromElement1.style.cssText = `background: url("${moveFromBorder}");background-size: 100% 100%`;
moveFromElement1.classList.add('square-' + moveFrom);
moveFromElement1.classList.add('highlight');
var moveToElement1 = document.createElement('div');
moveToElement1.id = 'moveToIndicator1';
moveToElement1.style.cssText = `background: url("${moveToBorder}");background-size: 100% 100%`;
moveToElement1.classList.add('square-' + moveTo);
moveToElement1.classList.add('highlight');
boardElement.append(moveFromElement1);
boardElement.append(moveToElement1);
}
if (JSON.parse(data).data.key == 'third') {
key = JSON.parse(data).data.value;
//replace letters with numbers because chess.com uses only numbers for piece positions
moveFrom = key.charAt(0).replace('h', '8').replace('g', '7').replace('f', '6').replace('e', '5').replace('d', '4').replace('c', '3').replace('b', '2').replace('a', '1') + key.charAt(1);
moveTo = key.charAt(2).replace('h', '8').replace('g', '7').replace('f', '6').replace('e', '5').replace('d', '4').replace('c', '3').replace('b', '2').replace('a', '1') + key.charAt(3);
//set correct correct image for move
moveFromBorder = 'https://i.imgur.com/qdEqbeE.png';
moveToBorder = 'https://i.imgur.com/bWw4LZc.png';
var moveFromElement2 = document.createElement('div');
moveFromElement2.id = 'moveFromIndicator2';
moveFromElement2.style.cssText = `background: url("${moveFromBorder}");background-size: 100% 100%`;
moveFromElement2.classList.add('square-' + moveFrom);
moveFromElement2.classList.add('highlight');
var moveToElement2 = document.createElement('div');
moveToElement2.id = 'moveToIndicator2';
moveToElement2.style.cssText = `background: url("${moveToBorder}");background-size: 100% 100%`;
moveToElement2.classList.add('square-' + moveTo);
moveToElement2.classList.add('highlight');
boardElement.append(moveFromElement2);
boardElement.append(moveToElement2);
}
});