-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchess.com.js
More file actions
33 lines (25 loc) · 929 Bytes
/
chess.com.js
File metadata and controls
33 lines (25 loc) · 929 Bytes
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
function getBoardPosition() {
let pgn = ''
for (const el of Array.from(document.querySelectorAll('[data-whole-move-number]'))) {
pgn += `${el.dataset.wholeMoveNumber}. `
for (const node of Array.from(el.querySelectorAll('[data-node]'))) {
const figurine = node.querySelector('[data-figurine]')?.dataset.figurine
pgn += `${figurine || ''}${node.textContent.trim()} `
if (node.querySelector('.selected')) {
// return pgn
}
}
}
return pgn
}
// this is lazy, just use a MutationObserver
setInterval(() => {
// If the board is flipped, we are playing black
const black = document.querySelector('wc-chess-board')?.classList.contains('flipped') || false
// extract the PGN position from the move list
let position = getBoardPosition()
if (position) {
// send everything to the engine for evaluation
chrome.runtime.sendMessage({ black, position })
}
}, 75)