Geoguessr average score display

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

当前为 2023-11-12 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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