[GC] - Solitaire Enhancements

See your win rate as a percentage, and prevent accidentally double-clicking and drawing two cards.

// ==UserScript==
// @name         [GC] - Solitaire Enhancements
// @namespace    https://greasyfork.org/en/users/1225524-kaitlin
// @match        https://www.grundos.cafe/games/sakhmet_solitaire/
// @match        https://www.grundos.cafe/games/pyramids/
// @version      2.1
// @license      MIT
// @author       Cupkait
// @icon         https://i.imgur.com/4Hm2e6z.png
// @description  See your win rate as a percentage, and prevent accidentally double-clicking and drawing two cards.
// ==/UserScript==


const deck = document.querySelector(".deck");

if (deck) {
    const f = deck.onclick;

    deck.onclick = function() {
      // Credit to Bankde for initial double-click protection script, used/tweaked with permission.
        let clickTimeout;

        function firstClick() {
            f.call(deck);

            deck.onclick = function() {
                clearTimeout(clickTimeout);
                clickTimeout = setTimeout(function() {
                    alert("Oops! Double-click detected and prevented. If the page doesn't automatically load your new card, refresh and try again.");
                }, 1000);
            };
        }

        firstClick();
    };

const collectBtn = document.querySelector('.margin-1 > input:nth-child(3)');
const movesLeft = document.querySelector('.flex.center-items.justify-center:nth-child(5)').textContent



const originalClickHandler = collectBtn.onclick;
function firstClickHandler(event) {
    event.preventDefault();
    event.stopPropagation();
    collectBtn.value = "Click again to confirm.";
    collectBtn.removeEventListener('click', firstClickHandler);
    collectBtn.addEventListener('click', originalClickHandler);
}
if ((movesLeft !== 'Last Round: 0') && (collectBtn.value === 'Collect Winnings') && (deck.alt !== 'deck_empty')) {
collectBtn.addEventListener('click', firstClickHandler);
}

} else {
    const winDataElement = document.querySelector("#page_content > .center > p:nth-child(5)");
    const gamesPlayed = parseInt(document.querySelector("#page_content > .center > p:nth-child(5) > strong:nth-child(1)").textContent.replace(/,/g, ''));
    const gamesWon = parseInt(document.querySelector("#page_content > .center > p:nth-child(5) > strong:nth-child(2)").textContent.replace(/,/g, ''));

    const winRate = `<p>Your current win rate is <strong>${parseFloat(((gamesWon / gamesPlayed) * 100).toFixed(2))}%</strong>.</p>`;
    winDataElement.innerHTML += " " + winRate;
}