Geoguessr average score display

Calculates average score from your last x amount of games. Restart of browser or refresh clears the currently saved scores.

Versione datata 12/11/2023. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Geoguessr average score display
// @namespace    https://greasyfork.org/en/users/1080671
// @version      0.1
// @description  Calculates average score from your last x amount of games. Restart of browser or refresh clears the currently saved scores.
// @author       Lemson
// @match        https://www.geoguessr.com/game/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=geoguessr.com
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-idle
// @license MIT
// ==/UserScript==

(function () {
  "use strict";  // Load saved scores from Tampermonkey storage
  let score = GM_getValue('savedScores', []) || [];
  console.log(score);
    let totalScore = score.reduce((sum, value) => sum + value, 0);

    //Edit maxEntries to however many rounds you want the average of.
  const maxEntries = 20;
  const targetSelector = '[data-qa="final-result-score"] > div';
  const averageScoreContainerSelector = 'div[class^="result-overlay_overlayContent__"]';

  // Add a flag to track if the element has been detected
  let elementDetected = false;

  const observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      const targetElements = document.querySelectorAll(targetSelector);

      // Check if the element is present
      if (targetElements.length > 0) {
        // Check the flag to prevent multiple log entries
        if (!elementDetected) {
          // Get the text content of the child element
          const childText = targetElements[0].innerText;

          // Convert the string to a number
          const scoreValue = parseInt(childText, 10);

          // Add the score to the array
          score.push(scoreValue);

          // Limit the array to a maximum of 20 entries
          if (score.length > maxEntries) {
            score.shift(); // Remove the oldest entry
          }

          // Save the scores to Tampermonkey storage
          GM_setValue('savedScores', score);

          // Sum up all the scores in the array
          totalScore = score.reduce((sum, value) => sum + value, 0);

          // Calculate the average score
          const averageScore = totalScore / score.length;

          // Display the average score on the page
          displayAverageScore(averageScore);

          // Set the flag to true to prevent further detections
          elementDetected = true;
        }
      } else {
        // If the element is not present, reset the flag
        elementDetected = false;
      }
    });
  });

  const targetNode = document.body;

  if (targetNode) {
    observer.observe(targetNode, { childList: true, subtree: true });
  } else {
    console.error("Target node not found!");
  }

  function displayAverageScore(averageScore) {
    const parentElement = document.querySelector(averageScoreContainerSelector);

    if (parentElement) {

      // Create a new div element to display the average score
      const averageScoreDiv = document.createElement('div');
      averageScoreDiv.style.textAlign = "center";
      averageScoreDiv.innerHTML = `Average Score: ${averageScore.toFixed(2)}<br>(over ${maxEntries} rounds)`;

      // Append the new div to the parent element
      parentElement.appendChild(averageScoreDiv);
    } else {
      console.error("Parent element not found!");
    }
  }
})();