simplegarfield

Create a Microsoft Edge browser extension for chess.com that highlights the best move for the player when they click on the GUI start. The extension should visually indicate the optimal move with an arrow pointing to the target square, without automatically moving the pieces. Implement options for users to customize the color and style of the arrows for better visibility.

Kodari AI
KodariToday at 6:15 pm 3
ChessMoveGuide
ChessMoveGuide is an Edge extension for chess.com that highlights the best move on demand. Users click a GUI start button to trigger analysis, which visually marks the optimal move with a customizable arrow pointing to the target square. Pieces are never moved automatically. Options let users adjust arrow color and style (solid, dashed, or dotted) for enhanced visibility and personal preference.

Created an Edge-compatible extension that analyzes the board when you click Start and draws a customizable arrow without moving pieces. Its built-in analysis is heuristic, not a guarantee of the optimal move; castling and en passant are not analyzed.

Load the exported folder through Edge’s edge://extensions page with Developer mode enabled. Use assistance only where Chess.com’s rules permit it.

simplegarfield

think for a moment but not too long and make it better optimization-wise and so that it's legit

Kodari AI
KodariToday at 6:19 pm 5
  • Limit suggestions to Chess.com analysis pages and make the fair-play boundary clear.

  • Move the search off the page thread and bound its work.

  • Let you choose the side to move, while retaining the arrow settings.

Improved performance by moving the bounded search off the page thread. You can now select the side to move.

Suggestions are restricted to Chess.com’s standalone /analysis page and blocked while Chess.com game or play tabs are open. This check can’t detect games on other devices, so don’t use it to analyze an ongoing game. Suggestions remain approximate, not guaranteed best moves.

Kodari can make mistakes. Review important code before using.
Files
background.js
content.css
content.js
manifest.json
popup.css
popup.html
popup.js
Code
7 566
background.js
const values = { p: 100, n: 320, b: 330, r: 500, q: 900, k: 20000 };
const knightSteps = [[1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1], [-2, 1], [-1, 2]];
const diagonal = [[1, 1], [1, -1], [-1, 1], [-1, -1]];
const straight = [[1, 0], [-1, 0], [0, 1], [0, -1]];
const kingSteps = diagonal.concat(straight);

function onBoard(x, y) {
  return x >= 0 && x < 8 && y >= 0 && y < 8;
}

function attacked(board, square, by) {
  const x = square % 8;
  const y = Math.floor(square / 8);
  const pawnY = y - (by === "w" ? 1 : -1);
  for (const dx of [-1, 1]) {
    if (onBoard(x + dx, pawnY) && board[pawnY * 8 + x + dx] === by + "p") return true;
  }
  for (const [dx, dy] of knightSteps) {
    if (onBoard(x + dx, y + dy) && board[(y + dy) * 8 + x + dx] === by + "n") return true;
  }
  for (const [directions, types] of [[diagonal, "bq"], [straight, "rq"]]) {
    for (const [dx, dy] of directions) {
      for (let step = 1; step < 8; step++) {
        const nx = x + dx * step;
        const ny = y + dy * step;
        if (!onBoard(nx, ny)) break;
        const piece = board[ny * 8 + nx];
        if (!piece) continue;
        if (piece[0] === by && (types.includes(piece[1]) || (step === 1 && piece[1] === "k"))) return true;
        break;
      }
    }
  }
  return false;
}

function makeMove(board, move) {
  const next = board.slice();
  const piece = next[move.from];
  next[move.from] = null;
  next[move.to] = piece[1] === "p" && (move.to < 8 || move.to >= 56) ? piece[0] + "q" : piece;
  return next;
}

function legalMoves(board, side) {
  const moves = [];
  const other = side === "w" ? "b" : "w";
  function add(from, x, y) {
    if (!onBoard(x, y)) return false;
    const to = y * 8 + x;
    const target = board[to];
    if (target && (target[0] === side || target[1] === "k")) return false;
    const move = { from, to, capture: target ? values[target[1]] : 0 };
    const next = makeMove(board, move);
    const king = next.indexOf(side + "k");
    if (king !== -1 && !attacked(next, king, other)) moves.push(move);
    return !target;
  }
  for (let from = 0; from < 64; from++) {
    const piece = board[from];
    if (!piece || piece[0] !== side) continue;
    const x = from % 8;
    const y = Math.floor(from / 8);
    const type = piece[1];
    if (type === "p") {
      const dy = side === "w" ? 1 : -1;
      if (onBoard(x, y + dy) && !board[(y + dy) * 8 + x]) {
        add(from, x, y + dy);
        if (y === (side === "w" ? 1 : 6) && !board[(y + 2 * dy) * 8 + x]) add(from, x, y + 2 * dy);
      }
      for (const dx of [-1, 1]) {
        if (onBoard(x + dx, y + dy)) {
          const target = board[(y + dy) * 8 + x + dx];
          if (target && target[0] === other) add(from, x + dx, y + dy);
        }
      }
    } else if (type === "n" || type === "k") {
      for (const [dx, dy] of type === "n" ? knightSteps : kingSteps) add(from, x + dx, y + dy);
    } else {
      const directions = type === "b" ? diagonal : type === "r" ? straight : kingSteps;
      for (const [dx, dy] of directions) {
        for (let step = 1; step < 8; step++) {
          if (!add(from, x + dx * step, y + dy * step)) break;
        }
      }
    }
  }
  return moves.sort((a, b) => b.capture - a.capture);
}

function evaluate(board, side) {
  let score = 0;
  for (let i = 0; i < 64; i++) {
    const piece = board[i];
    if (!piece) continue;
    const x = i % 8;
    const y = Math.floor(i / 8);
    const center = 3.5 - (Math.abs(x - 3.5) + Math.abs(y - 3.5)) / 2;
    let bonus = piece[1] === "n" || piece[1] === "b" ? center * 8 : 0;
    if (piece[1] === "p") bonus += (piece[0] === "w" ? y : 7 - y) * 5;
    score += (piece[0] === side ? 1 : -1) * (values[piece[1]] + bonus);
  }
  return score;
}

function search(board, side, depth, alpha, beta, state, ply) {
  state.nodes++;
  if (state.nodes >= 60000 || performance.now() >= state.deadline) {
    state.stopped = true;
    return 0;
  }
  if (depth === 0) return evaluate(board, side);
  const moves = legalMoves(board, side);
  const other = side === "w" ? "b" : "w";
  if (!moves.length) {
    const king = board.indexOf(side + "k");
    return king !== -1 && attacked(board, king, other) ? -100000 + ply : 0;
  }
  let best = -Infinity;
  for (const move of moves) {
    const score = -search(makeMove(board, move), other, depth - 1, -beta, -alpha, state, ply + 1);
    if (state.stopped) return 0;
    best = Math.max(best, score);
    alpha = Math.max(alpha, score);
    if (alpha >= beta) break;
  }
  return best;
}

function bestMove(board, side) {
  const moves = legalMoves(board, side);
  if (!moves.length) return { move: null, depth: 0 };
  const other = side === "w" ? "b" : "w";
  const state = { nodes: 0, deadline: performance.now() + 700, stopped: false };
  let best = moves[0];
  let completedDepth = 0;
  for (let depth = 1; depth <= 4; depth++) {
    let iterationBest = null;
    let bestScore = -Infinity;
    let alpha = -Infinity;
    for (const move of moves) {
      const score = -search(makeMove(board, move), other, depth - 1, -Infinity, -alpha, state, 1);
      if (state.stopped) break;
      if (score > bestScore) {
        bestScore = score;
        iterationBest = move;
      }
      alpha = Math.max(alpha, score);
    }
    if (state.stopped) break;
    best = iterationBest;
    completedDepth = depth;
    moves.sort((a, b) => (b === best ? 1 : 0) - (a === best ? 1 : 0));
  }
  return { move: { from: best.from, to: best.to }, depth: completedDepth };
}

async function eligibility(sender) {
  if (sender.id !== chrome.runtime.id || !sender.tab || !sender.url) {
    return { allowed: false, message: "Open the standalone Chess.com analysis board." };
  }
  let url;
  try {
    url = new URL(sender.url);
  } catch (error) {
    console.error("Invalid sender URL:", error);
    return { allowed: false, message: "Could not verify the analysis page." };
  }
  if (url.origin !== "https://www.chess.com" || !/^\/analysis\/?$/.test(url.pathname)) {
    return { allowed: false, message: "Use the standalone Chess.com analysis board at /analysis, not a game page." };
  }
  const tabs = await chrome.tabs.query({});
  const gameTab = tabs.some(tab => {
    if (!tab.url) return false;
    try {
      const other = new URL(tab.url);
      return other.origin === "https://www.chess.com" && /^\/(?:play|game|live|daily|variants)(?:\/|$)/i.test(other.pathname);
    } catch (error) {
      console.error("Invalid tab URL:", error);
      return false;
    }
  });
  if (gameTab) return { allowed: false, message: "Close Chess.com game and play tabs before using analysis. Never analyze an ongoing game." };
  return { allowed: true };
}

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action !== "calculate" && message.action !== "checkEligibility") return;
  (async () => {
    const allowed = await eligibility(sender);
    if (!allowed.allowed || message.action === "checkEligibility") return allowed;
    if (!Array.isArray(message.position) || message.position.length !== 64 ||
        !message.position.every(piece => piece === null || (typeof piece === "string" && /^[wb][pnbrqk]$/.test(piece))) ||
        message.position.filter(piece => piece === "wk").length !== 1 ||
        message.position.filter(piece => piece === "bk").length !== 1 || !["w", "b"].includes(message.side)) {
      return { allowed: false, message: "Invalid board position or side to move." };
    }
    const result = bestMove(message.position, message.side);
    const stillAllowed = await eligibility(sender);
    return stillAllowed.allowed ? { allowed: true, ...result } : stillAllowed;