Geoguessr average score display

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

Από την 12/11/2023. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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