Geoguessr average score display

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

Version vom 12.11.2023. Aktuellste Version

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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.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";  
  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__"]';

  let elementDetected = false;

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

      if (targetElements.length > 0) {
        if (!elementDetected) {
          const childText = targetElements[0].innerText;

          const scoreValue = parseInt(childText, 10);

          score.push(scoreValue);

          if (score.length > maxEntries) {
            score.shift(); 
          }

          GM_setValue('savedScores', score);

          totalScore = score.reduce((sum, value) => sum + value, 0);

          const averageScore = totalScore / score.length;

          displayAverageScore(averageScore);

          elementDetected = true;
        }
      } else {
        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) {

      const averageScoreDiv = document.createElement('div');
      averageScoreDiv.style.textAlign = "center";
      averageScoreDiv.innerHTML = `Average Score: ${averageScore.toFixed(2)}<br>(over ${maxEntries} rounds)`;

      parentElement.appendChild(averageScoreDiv);
    } else {
      console.error("Parent element not found!");
    }
  }
})();