Skip to content

Commit

Permalink
Fix chess.com variants
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakorr committed Apr 7, 2024
1 parent 893ddaa commit 45ee721
Showing 1 changed file with 6 additions and 47 deletions.
53 changes: 6 additions & 47 deletions acas.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ Object.values(configKeys).forEach(key => {

let BoardDrawer = null;
let chessBoardElem = null;
let chesscomVariantBoardCoordsTable = null;
let chesscomVariantPlayerColorsTable = null;
let activeSiteMoveHighlights = [];
let inactiveGuiMoveMarkings = [];
Expand Down Expand Up @@ -716,35 +715,6 @@ function getElemCoordinatesFromLeftTopPixels(elem) {
}
}

function updateChesscomVariantBoardCoordsTable() {
chesscomVariantBoardCoordsTable = {};

const boardElem = getBoardElem();
const [boardWidth, boardHeight] = getBoardDimensions();
const boardOrientation = getBoardOrientation();

const squareElems = getSquareElems(boardElem);

let squareIndex = 0;

for(let x = 0; x < boardWidth; x++) {
for(let y = boardHeight; y > 0; y--) {
const squareElem = squareElems[squareIndex];
const id = squareElem?.dataset?.theme;

if(id) {
if(boardOrientation === 'b') {
chesscomVariantBoardCoordsTable[id] = [boardWidth - (x + 1), boardHeight - y];
} else {
chesscomVariantBoardCoordsTable[id] = [x, y - 1];
}
}

squareIndex++;
}
}
}

function updateChesscomVariantPlayerColorsTable() {
let colors = [];

Expand Down Expand Up @@ -1323,6 +1293,10 @@ addSupportedChessSite('chess.com', {

pieceColor = chesscomVariantPlayerColorsTable[pieceElem?.dataset?.color];
pieceName = pieceElem?.dataset?.piece;

if(pieceName?.length > 1) {
pieceName = pieceName[0];
}
} else {
const pieceStr = [...pieceElem.classList].find(x => x.match(/^(b|w)[prnbqk]{1}$/));

Expand All @@ -1337,18 +1311,7 @@ addSupportedChessSite('chess.com', {
const pieceElem = obj.pieceElem;

if(pathname?.includes('/variants')) {
if(!chesscomVariantBoardCoordsTable) {
updateChesscomVariantBoardCoordsTable();
}

const pieceBoundary = pieceElem.getBoundingClientRect();
const elementsBehindPieceElem = document.elementsFromPoint(pieceBoundary.x, pieceBoundary.y);

const squareElem = elementsBehindPieceElem?.find(x => x?.classList?.contains('square'));

//console.log(squareElem?.dataset?.theme, chesscomVariantBoardCoordsTable[squareElem?.dataset?.theme]);

const coords = chesscomVariantBoardCoordsTable[squareElem?.dataset?.theme];
const coords = getElemCoordinatesFromTransform(pieceElem);

return coords;
}
Expand Down Expand Up @@ -1380,8 +1343,6 @@ addSupportedChessSite('chess.com', {
}
});

//console.log([ranks, files]);

return [ranks, files];
} else {
return [8, 8];
Expand Down Expand Up @@ -2585,8 +2546,6 @@ addSupportedChessSite('chessfriends.com', {
'pieceElemCoords': obj => {
const pieceElem = obj.pieceElem;
//console.log(getElemCoordinatesFromLeftTopPixels(pieceElem));
return getElemCoordinatesFromLeftTopPixels(pieceElem);
},
Expand Down Expand Up @@ -2746,4 +2705,4 @@ if(typeof GM_registerMenuCommand === 'function') {
}
}

setInterval(initializeIfSiteReady, 1000);
setInterval(initializeIfSiteReady, 1000);

0 comments on commit 45ee721

Please sign in to comment.