Geoguessr average score display

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

Fra 12.11.2023. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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!");
    }
  }
})();