-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserscript.js
222 lines (192 loc) · 6.52 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
// ==UserScript==
// @name Blindfolded Chess
// @namespace http://tampermonkey.net/
// @version 2024-04-09
// @description Blindfolded play on chess.com
// @author https;//github.com/baroxyton/chess.com-blindfolded-play
// @match https://*.chess.com/game/*
// @match https://*.chess.com/play/online/*
// @match https://*.chess.com/play/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=chess.com
// @grant none
// ==/UserScript==
const blindfolded_user = "YOUR_BLINDFOLDED_ACCOUNT";
(function() {
if(!document.body.innerHTML.includes(blindfolded_user)){
return;
}
setTimeout(()=>{
(()=>{
const scriptEl = document.createElement("script");
scriptEl.src = "https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.3/chess.min.js";
document.body.appendChild(scriptEl);
scriptEl.onload = initBlindfold;
})()
function getMoves(){
return Array.from(document.querySelectorAll(".node")).map(move=>{
const hasPiece = move.querySelector("span[data-figurine]");
if(hasPiece){
return (hasPiece.getAttribute("data-figurine") + move.innerText).replaceAll(/\s+/g, "")
}
return move.innerText.replaceAll(/\s+/g, "")
});
}
function initBlindfold(){
const boardEl = document.querySelector(".board");
boardEl.style.visibility = "hidden";
const boardParent = document.querySelector("#board-layout-chessboard");
const blindfoldInterface = document.createElement("div");
blindfoldInterface.innerHTML = `<h1 style="color:#2255AA">Blindfolded chess!</h1>
<input id="blindfold-input"><button id="blindfold-play">Play</button>`;
boardParent.appendChild(blindfoldInterface);
document.querySelector("#blindfold-play").addEventListener("click", triggerPlay);
const inputField = document.getElementById('blindfold-input');
inputField.addEventListener('keypress', function(event) {
if (event.keyCode === 13 || event.key === 'Enter') {
triggerPlay();
}
});
setInterval(()=>{
if(document.querySelector("#toggleboard")){
return
}
// Selecting the .player-tagline element
const playerTagline = document.querySelector('.user-tagline-component');
// Creating a toggle button
const toggleButton = document.createElement('input');
toggleButton.id = "toggleboard";
toggleButton.type = 'checkbox';
// Creating a label for the toggle button
const label = document.createElement('a');
label.innerHTML = 'Show board?';
label.style.color = "white"
// Appending the toggle button and label to the .player-tagline element
playerTagline.appendChild(toggleButton);
playerTagline.appendChild(label);
// Selecting the .board element
const board = document.querySelector('.board');
// Adding event listener to toggle button
toggleButton.addEventListener('change', function() {
// If toggle button is checked, hide the board, otherwise show it
board.style.visibility = this.checked ? 'visible' : 'hidden';
});
}, 2000)
}
function sqToArr(square){
let rank = Number(square[1]) - 1;
let file = ["a", "b", "c", "d", "e", "f", "g", "h"].indexOf(square[0]);
return [file, rank];
}
function applyEvent(x, y, isUp){
const eventType = isUp?"pointerup":"pointerdown";
const mouseUpEvent = new MouseEvent(eventType, {
bubbles: true,
cancelable: true,
clientX: x,
clientY: y
});
document.querySelector(".board").dispatchEvent(mouseUpEvent)
}
function applyMove(dimensions, isWhite, start, end){
const boardx = dimensions.left;
const boardy = dimensions.top;
const sidel = dimensions.width;
const sqSize = sidel/8;
let x1, x2, y1, y2;
if(isWhite){
x1 = boardx + (sqSize * start[0]) + sqSize/2;
x2 = boardx + (sqSize * end[0]) + sqSize/2;
y1 = boardy + sidel - (sqSize * start[1]) - (sqSize/2)
y2 = boardy + sidel - (sqSize * end[1]) - (sqSize/2)
}
else{
x1 = boardx + sidel - (sqSize * start[0]) - (sqSize/2);
x2 = boardx + sidel - (sqSize * end[0]) - (sqSize/2);
y1 = boardy + (sqSize * start[1]) + sqSize/2;
y2 = boardy + (sqSize * end[1]) + sqSize/2;
}
applyEvent(x1, y1);
setTimeout(()=>{applyEvent(x2, y2, true)}, 50)
}
function playMove(move){
const movesPlayed = getMoves();
const boardEl = document.querySelector(".board");
const dimensions = boardEl.getBoundingClientRect();
const isWhite = movesPlayed.length % 2 == 0;
const chessBoard = new Chess();
chessBoard.load_pgn(movesPlayed.join(" "));
const userMove = chessBoard.move(move);
if(!userMove){
say("Invalid move: " + move)
return;
}
const startSquare = sqToArr(userMove.from);
const endMove = sqToArr(userMove.to);
applyMove(dimensions, isWhite, startSquare, endMove);
}
function triggerPlay(){
const move = document.querySelector("#blindfold-input").value;
document.querySelector("#blindfold-input").value = "";
playMove(move);
}
function transformNotation(notation) {
let result = '';
if (notation === 'O-O') {
result = 'kingside castles';
} else if (notation === 'O-O-O') {
result = 'queenside castles';
} else if (notation.endsWith('+')) {
// Checking notation
const strippedNotation = notation.slice(0, -1);
result = `Check: ${transformNotation(strippedNotation)}`;
} else if (notation.endsWith('#')) {
// Checkmate notation
const strippedNotation = notation.slice(0, -1);
result = `Checkmate: ${transformNotation(strippedNotation)}`;
} else {
const pieceMap = {
'N': 'knight',
'B': 'bishop',
'R': 'rook',
'Q': 'queen',
'K': 'king'
};
const actionMap = {
'x': 'captures',
'-': 'moves to'
};
let piece = 'pawn';
let action = 'moves to';
let target = '';
for (let i = 0; i < notation.length; i++) {
if (pieceMap[notation[i]]) {
piece = pieceMap[notation[i]];
} else if (actionMap[notation[i]]) {
action = actionMap[notation[i]];
} else {
target += notation[i];
}
}
result = `${piece} ${action} ${target}`;
}
return result;
}
function sayMove(move){
say(transformNotation(move))
}
function say(words){
const utterance = new SpeechSynthesisUtterance(words);
utterance.lang = 'en-UK';
speechSynthesis.speak(utterance);
}
window.numMoves = 0;
setInterval(()=>{
const moves = getMoves();
const currMoveCount = moves.length
if(currMoveCount != window.numMoves){
window.numMoves = currMoveCount;
sayMove(moves[moves.length - 1]);
}
}, 100)
}, 2000);
})();