Advertisement
dreifachpunkt

Hedge Streak Counter by quarksauce

Aug 23rd, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Hedge Streak Counter (Automated)
  3. // @version      1.1
  4. // @include      /^(https?)?(\:)?(\/\/)?([^\/]*\.)?geoguessr\.com($|\/.*)/
  5. // @description  Adds a hedge streak counter to the GeoGuessr website
  6. // @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  7. // @author       quarksauce
  8. // @license      MIT
  9. // ==/UserScript==
  10.  
  11. let streak = parseInt(sessionStorage.getItem("HedgeStreak"), 10);
  12. let lastGameId = sessionStorage.getItem("HedgeLastGameId");
  13. let lostStreakAt = 0;
  14. let checked = false;
  15.  
  16. if (sessionStorage.getItem("HedgeStreak") == null) {
  17.     sessionStorage.setItem("HedgeStreak", 0);
  18.     streak = 0;
  19. }
  20.  
  21. function updateStreak() {
  22.     if (document.getElementById("hedge-streak") != null) {
  23.         document.getElementById("hedge-streak").innerHTML = `<div id="hedge-streak"><div class="status_value__xZMNY">${streak}</div></div>`;
  24.     }
  25.     if (document.getElementById("hedge-streak2") != null && !!document.querySelector('.standard-final-result_section__B3ne')) {
  26.         document.getElementById("hedge-streak2").innerHTML = `<h2><i>Hedge Streak: ${streak}</i></h2>`;
  27.     }
  28. };
  29.  
  30. function addCounterDelay() {
  31.     var addCounterInterval = setInterval(function() {
  32.         if (document.getElementsByClassName("status_section__8uP8o").length > 0) {
  33.             addCounter();
  34.             clearInterval(addCounterInterval);
  35.         }
  36.     }, 250);
  37.  
  38.     // reset interval after 1s
  39.     setTimeout(function() {
  40.         clearInterval(addCounterInterval);
  41.     }, 1000);
  42. };
  43.  
  44. function addCounter() {
  45.     if (document.getElementsByClassName("status_section__8uP8o").length == 3) {
  46.         let newDiv = document.createElement("div");
  47.         newDiv.className = 'status_section__8uP8o';
  48.         document.getElementsByClassName("status_inner__1eytg")[0].appendChild(newDiv);
  49.         newDiv.innerHTML = `<div class="status_label__SNHKT">Hedge</div><div id="hedge-streak"><div class="status_value__xZMNY">${streak}</div></div>`;
  50.     }
  51.     if (document.getElementsByClassName("status_section__8uP8o").length == 4 && document.getElementsByClassName("status_label__SNHKT")[3].innerText == "TIME LEFT") {
  52.         let newDiv = document.createElement("div");
  53.         newDiv.className = 'status_section__8uP8o';
  54.         document.getElementsByClassName("status_inner__1eytg")[0].appendChild(newDiv);
  55.         newDiv.innerHTML = `<div class="status_label__SNHKT">Hedge</div><div id="hedge-streak"><div class="status_value__xZMNY">${streak}</div></div>`;
  56.     }
  57. };
  58.  
  59. function addFinalDivDelay() {
  60.     var addFinalDivInterval = setInterval(function() {
  61.         if (document.getElementById("hedge-streak2") == null && !!document.querySelector('.standard-final-result_section___B3ne') && location.pathname.startsWith("/game/")) {
  62.             addFinalDiv();
  63.             clearInterval(addFinalDivInterval);
  64.         }
  65.     }, 250);
  66.  
  67.     // reset interval after 3s
  68.     setTimeout(function() {
  69.         clearInterval(addFinalDivInterval);
  70.     }, 3000);
  71. }
  72.  
  73. function addFinalDiv() {
  74.     let newDiv2 = document.createElement("div");
  75.     document.getElementsByClassName("progress-bar_background__A6ZDS progress-bar_expandHeight__W_59W")[0].appendChild(newDiv2);
  76.     newDiv2.innerHTML = `<div id="hedge-streak2" style="text-align:center"><br><h2><i>Hedge Streak: ${streak}</i></h2></div>`;
  77.  
  78.     if (lostStreakAt >= 1) {
  79.         let endText = lostStreakAt == 1 ? "seed" : "seeds";
  80.         document.getElementById("hedge-streak2").innerHTML = `<br><i>Your hedge streak ended after <div class="round-result_distanceLabel__tWGP5"><div class="styles_root__eoNIJ styles_variantWhiteTransparent__20ADs styles_roundnessSmall__ZbRvs"><div class="styles_start__u_cL2 styles_right__hZg0u"></div><div class="round-result_distanceValue__ijRit">${lostStreakAt}</div><div class="styles_end__euu3r styles_right__hZg0u"></div></div></div> ${endText}.`;
  81.         // reset 'lost streak at' after showing to user
  82.         lostStreakAt = 0;
  83.     }
  84. };
  85.  
  86. function preCheck() {
  87.     if (!!document.querySelector('.result-layout_root__NfX12') && location.pathname.startsWith("/game/") && checked == false){
  88.         // avoid multiple requests to GeoGuessr website after guessing a location
  89.         checked = true;
  90.         check();
  91.     }
  92.     else if (!document.querySelector('.result-layout_root__NfX12') && location.pathname.startsWith("/game/") && checked == true) {
  93.         // reset flag for next round
  94.         checked = false;
  95.     }
  96. };
  97.  
  98. function check() {
  99.     const gameId = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
  100.  
  101.     // check only if this game was not the last game of your streak
  102.     if (gameId != lastGameId) {
  103.  
  104.         let apiUrl = "";
  105.         if (location.pathname.startsWith("/game/")) {
  106.             apiUrl = "https://www.geoguessr.com/api/v3/games/" + gameId
  107.         };
  108.         fetch(apiUrl)
  109.             .then(res => res.json())
  110.             .then((out) => {
  111.  
  112.             // check only finished seeds for total score
  113.             if (out.state == "finished") {
  114.                 if (parseInt(out.player.totalScore.amount, 10) >= 20000) {
  115.                     streak++;
  116.                     updateStreak();
  117.                     // write this game id in Session storage to prevent counting on refresh (F5)
  118.                     lastGameId = gameId;
  119.                     sessionStorage.setItem("HedgeLastGameId", lastGameId);
  120.                     sessionStorage.setItem("HedgeStreak", streak);
  121.                 } else {
  122.                     lostStreakAt = streak;
  123.                     streak = 0;
  124.                     updateStreak();
  125.                     sessionStorage.setItem("HedgeStreak", streak);
  126.                 }
  127.  
  128.                 addFinalDivDelay();
  129.             };
  130.  
  131.         }).catch(err => { throw err; });
  132.  
  133.     } else { addFinalDivDelay(); }
  134.  
  135. };
  136.  
  137. document.addEventListener('keypress', (e) => {
  138.     switch (e.key) {
  139.         case '1':
  140.             streak++;
  141.             updateStreak();
  142.             sessionStorage.setItem("HedgeStreak", streak);
  143.             break;
  144.         case '2':
  145.             streak--;
  146.             updateStreak();
  147.             sessionStorage.setItem("HedgeStreak", streak);
  148.             break;
  149.         case '0':
  150.             streak = 0;
  151.             updateStreak();
  152.             sessionStorage.setItem("HedgeStreak", streak);
  153.             break;
  154.     }
  155. });
  156.  
  157. document.addEventListener('click', preCheck, false);
  158. document.addEventListener('click', addCounterDelay, false);
  159. document.addEventListener('load', addCounterDelay(), false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement