r/chess•
nbdy

chatGPT and I made a userstyle that adds annotations to every square

Resource

The script is below. You need tampermonkey or similar plugin for your browser.

hope it helps people learn the squares, which I am trying to do. And if anyone has suggestions to improve it, please let em be known

// ==UserScript==
// @name         Dynamic Chessboard Annotations
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Update annotations dynamically when resizing the chessboard.
// @author       You
// @match        https://www.chess.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const files = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
    const ranks = ['8', '7', '6', '5', '4', '3', '2', '1'];

    function addAnnotationStyles() {
        const style = document.createElement('style');
        style.textContent = `
            .annotation {
                position: absolute;
                color: black;
                font-size: 16px;
                pointer-events: none;
                z-index: 100;
            }
            .annotation-cell {
                position: absolute;
                z-index: 99;
            }
        `;
        document.head.appendChild(style);
    }

    function annotateBoard(board, boardSize) {
        // Remove existing annotations if any
        board.querySelectorAll('.annotation-cell').forEach(cell => cell.remove());

        const squareSize = boardSize / 8;
        for (let rank = 0; rank < 8; rank++) {
            for (let file = 0; file < 8; file++) {
                const annotation = files[file] + ranks[rank];
                const cellDiv = document.createElement('div');
                cellDiv.className = 'annotation-cell';
                cellDiv.style.width = `${squareSize}px`;
                cellDiv.style.height = `${squareSize}px`;
                cellDiv.style.left = `${file * squareSize}px`;
                cellDiv.style.top = `${rank * squareSize}px`;

                const label = document.createElement('div');
                label.className = 'annotation';
                label.textContent = annotation;
                cellDiv.appendChild(label);

                board.appendChild(cellDiv);
            }
        }
    }

    function setupBoardAnnotations() {
        const chessboard = document.querySelector('wc-chess-board');
        if (!chessboard) {
            console.warn('Chessboard not found.');
            return;
        }

        addAnnotationStyles();

        // Initialize annotations
        annotateBoard(chessboard, chessboard.clientWidth);

        // Setup ResizeObserver to update annotations on resize
        const resizeObserver = new ResizeObserver(entries => {
            for (let entry of entries) {
                const {width} = entry.contentRect;
                annotateBoard(entry.target, width);
            }
        });

        resizeObserver.observe(chessboard);
    }

    // Make sure the DOM is fully loaded
    window.addEventListener('load', setupBoardAnnotations);
})();

Posted May 18, 2024, 12:56 AM UTC

10

Comments aren't archived yet

Comments for this period haven't been loaded into the archive yet.