GeoGuessr Highscore Date mockup

Adds game date for highscores

  1. // ==UserScript==
  2. // @name GeoGuessr Highscore Date mockup
  3. // @description Adds game date for highscores
  4. // @version 0.1
  5. // @author Tyow#3742
  6. // @match *://*.geoguessr.com/*
  7. // @license MIT
  8. // @namespace https://greasyfork.org/users/1011193
  9. // @grant GM_addStyle
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // ==/UserScript==
  13.  
  14. // Function to fetch game data from the API and extract the start date
  15. async function fetchGameDate(gameId) {
  16. const url = `https://www.geoguessr.com/api/v3/games/${gameId}`;
  17.  
  18. try {
  19. const response = await fetch(url);
  20.  
  21. // If the response is successful
  22. if (response.ok) {
  23. const gameData = await response.json();
  24.  
  25. // Extract the startTime of the first round
  26. const startTime = gameData.rounds[0].startTime;
  27.  
  28. // Convert the startTime to a Date object and format it as yyyy-mm-dd
  29. const date = new Date(startTime).toISOString().split('T')[0];
  30.  
  31. return date;
  32. } else {
  33. console.error(`Error fetching game data: ${response.status}`);
  34. }
  35. } catch (error) {
  36. console.error(`Error: ${error}`);
  37. }
  38.  
  39. return null; // Return null if there is an error
  40. }
  41.  
  42. const fetchIds = async () => {
  43.  
  44.  
  45. // Select all the rows
  46. const rows = document.querySelectorAll('[class^="table_tr__"]');
  47.  
  48. // Loop through each row and extract the gameid
  49. rows.forEach(async row => {
  50. // Find the link element within the row
  51. if (!row.classList.contains('highscoredate')) {
  52. const resultLink = row.querySelector('a[title="View results"]');
  53.  
  54. // Extract the href attribute
  55. if (resultLink) {
  56. const href = resultLink.getAttribute('href');
  57.  
  58. // Use a regular expression to match and extract the gameid
  59. const gameIdMatch = href.match(/\/results\/([a-zA-Z0-9]+)/);
  60.  
  61.  
  62. if (gameIdMatch && gameIdMatch[1]) {
  63. row.classList.add('highscoredate')
  64. const gameId = gameIdMatch[1];
  65.  
  66. const gameDate = await fetchGameDate(gameId);
  67.  
  68. if (gameDate) {
  69. // Create a new td element
  70. const newTd = document.createElement('td');
  71.  
  72. // Add the same classes as the other td elements in the row
  73. newTd.classList.add(
  74. 'table_td__Z9pLI',
  75. 'table_textAlignLeft__8vcyr',
  76. 'table_noWrap__L4__t',
  77. 'map-highscore_colBody__Jp0zE'
  78. );
  79.  
  80. // Set the text content to the extracted date
  81. newTd.textContent = gameDate;
  82.  
  83. // Append the new td to the current row
  84. row.appendChild(newTd);
  85.  
  86. }
  87. }
  88. }}});
  89.  
  90. }
  91.  
  92.  
  93.  
  94. new MutationObserver(async (mutations) => {
  95. fetchIds()
  96. }).observe(document.body, { subtree: true, childList: true });