Last active
July 5, 2023 13:47
-
-
Save Andreal2000/8952529ca69afa0e5fbc7bb37993f841 to your computer and use it in GitHub Desktop.
This plugin adds a button next to the chess board allowing to analyze the game on lichess.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Analize on Lichess | |
// @namespace Andreal2000 | |
// @match https://www.chess.com/game/* | |
// @run-at document-end | |
// @grant none | |
// @version 1.0.0 | |
// @author Andreal2000 | |
// @icon https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://lichess.org&size=128 | |
// @description This plugin adds a button next to the chess board allowing to analyze the game on lichess.org | |
// @installURL https://gist.github.com/Andreal2000/8952529ca69afa0e5fbc7bb37993f841/raw/ | |
// @downloadURL https://gist.github.com/Andreal2000/8952529ca69afa0e5fbc7bb37993f841/raw/ | |
// @updateURL https://gist.github.com/Andreal2000/8952529ca69afa0e5fbc7bb37993f841/raw/ | |
// @homepageURL https://gist.github.com/Andreal2000/ | |
// ==/UserScript== | |
// (function () { | |
'use strict'; | |
// Declare a global variable to store the formatted moves | |
let savedMoves = ''; | |
function formatMoves() { | |
// Find the vertical-move-list element | |
const moveList = document.querySelector('vertical-move-list[data-cy="move-list"]'); | |
// Get all the move elements inside the move list | |
const moveElements = moveList.querySelectorAll('.move'); | |
// Create an array to store the formatted moves | |
const formattedMoves = []; | |
// Iterate over each move element and extract the move text | |
moveElements.forEach((moveElement) => { | |
const moveText = Array.from(moveElement.querySelectorAll('.node')).map((node) => { | |
const figure = node.querySelector('[data-figurine]'); | |
return figure ? figure.getAttribute('data-figurine') + node.textContent.trim() : node.textContent.trim(); | |
}).join('_'); | |
formattedMoves.push(moveText); | |
}); | |
// Join the formatted moves using '_' | |
const formattedString = formattedMoves.join('_'); | |
// Update the savedMoves variable with the latest formatted moves | |
savedMoves = formattedString; | |
// Return the final formatted string | |
return formattedString; | |
} | |
// Updating the savedMoves variable when the vertical move list is modified | |
// DOMSubtreeModified is deprecated | |
document.addEventListener('DOMSubtreeModified', () => { | |
if (document.querySelector('vertical-move-list[data-cy="move-list"]')) { | |
savedMoves = formatMoves(); | |
} | |
}); | |
// Function to add the hyperlink when the div is loaded | |
function addButtonToDiv() { | |
const divElement = document.querySelector('.live-game-buttons-component'); | |
// const divElement = document.querySelector('.evaluation-settings-component'); | |
if (divElement) { | |
const button = document.createElement('button'); | |
button.classList.add('button-class'); // Add your desired button class | |
button.style.border = 'none'; // Remove the button border | |
button.style.padding = '0'; // Remove the padding | |
button.style.background = 'transparent'; // Make the button background transparent | |
// const icon = 'https://lichess1.org/assets/_Uy6AAV/logo/lichess-favicon-512.png' | |
const icon = 'https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://lichess.org&size=128' | |
button.innerHTML = '<img src="' + icon + '" width="24" height="24">'; // Replace with the image HTML | |
button.addEventListener('click', function () { | |
// window.open('https://lichess.org/analysis/pgn/' + formatMoves()); | |
window.open('https://lichess.org/analysis/pgn/' + savedMoves); | |
}); | |
divElement.insertBefore(button, divElement.firstChild); | |
console.log('Button added at the start of live-game-buttons-component:', button); | |
} else { | |
console.log('Div not found.'); | |
} | |
} | |
// Attempt to observe the div after a delay | |
function observeDiv() { | |
console.log('Waiting for the div to be added...'); | |
const divElement = document.querySelector('.live-game-buttons-component'); | |
if (divElement) { | |
console.log('Div already present.'); | |
addButtonToDiv(); | |
} else { | |
const observer = new MutationObserver(function (mutations) { | |
mutations.forEach(function (mutation) { | |
if (mutation.addedNodes) { | |
for (let i = 0; i < mutation.addedNodes.length; i++) { | |
const node = mutation.addedNodes[i]; | |
if (node.classList && node.classList.contains('live-game-buttons-component')) { | |
console.log('Div added to document:', node); | |
addButtonToDiv(); | |
observer.disconnect(); | |
return; | |
} | |
} | |
} | |
}); | |
}); | |
observer.observe(document, { | |
childList: true, | |
subtree: true | |
}); | |
} | |
} | |
setTimeout(observeDiv, 2000); // Adjust the delay as needed | |
console.log('Script loaded. Waiting 2 seconds for the div to be added.'); | |
// })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment