Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Hedge Streak Counter (Automated)
- // @version 1.1
- // @include /^(https?)?(\:)?(\/\/)?([^\/]*\.)?geoguessr\.com($|\/.*)/
- // @description Adds a hedge streak counter to the GeoGuessr website
- // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
- // @author quarksauce
- // @license MIT
- // ==/UserScript==
- let streak = parseInt(sessionStorage.getItem("HedgeStreak"), 10);
- let lastGameId = sessionStorage.getItem("HedgeLastGameId");
- let lostStreakAt = 0;
- let checked = false;
- if (sessionStorage.getItem("HedgeStreak") == null) {
- sessionStorage.setItem("HedgeStreak", 0);
- streak = 0;
- }
- function updateStreak() {
- if (document.getElementById("hedge-streak") != null) {
- document.getElementById("hedge-streak").innerHTML = `<div id="hedge-streak"><div class="status_value__xZMNY">${streak}</div></div>`;
- }
- if (document.getElementById("hedge-streak2") != null && !!document.querySelector('.standard-final-result_section__B3ne')) {
- document.getElementById("hedge-streak2").innerHTML = `<h2><i>Hedge Streak: ${streak}</i></h2>`;
- }
- };
- function addCounterDelay() {
- var addCounterInterval = setInterval(function() {
- if (document.getElementsByClassName("status_section__8uP8o").length > 0) {
- addCounter();
- clearInterval(addCounterInterval);
- }
- }, 250);
- // reset interval after 1s
- setTimeout(function() {
- clearInterval(addCounterInterval);
- }, 1000);
- };
- function addCounter() {
- if (document.getElementsByClassName("status_section__8uP8o").length == 3) {
- let newDiv = document.createElement("div");
- newDiv.className = 'status_section__8uP8o';
- document.getElementsByClassName("status_inner__1eytg")[0].appendChild(newDiv);
- newDiv.innerHTML = `<div class="status_label__SNHKT">Hedge</div><div id="hedge-streak"><div class="status_value__xZMNY">${streak}</div></div>`;
- }
- if (document.getElementsByClassName("status_section__8uP8o").length == 4 && document.getElementsByClassName("status_label__SNHKT")[3].innerText == "TIME LEFT") {
- let newDiv = document.createElement("div");
- newDiv.className = 'status_section__8uP8o';
- document.getElementsByClassName("status_inner__1eytg")[0].appendChild(newDiv);
- newDiv.innerHTML = `<div class="status_label__SNHKT">Hedge</div><div id="hedge-streak"><div class="status_value__xZMNY">${streak}</div></div>`;
- }
- };
- function addFinalDivDelay() {
- var addFinalDivInterval = setInterval(function() {
- if (document.getElementById("hedge-streak2") == null && !!document.querySelector('.standard-final-result_section___B3ne') && location.pathname.startsWith("/game/")) {
- addFinalDiv();
- clearInterval(addFinalDivInterval);
- }
- }, 250);
- // reset interval after 3s
- setTimeout(function() {
- clearInterval(addFinalDivInterval);
- }, 3000);
- }
- function addFinalDiv() {
- let newDiv2 = document.createElement("div");
- document.getElementsByClassName("progress-bar_background__A6ZDS progress-bar_expandHeight__W_59W")[0].appendChild(newDiv2);
- newDiv2.innerHTML = `<div id="hedge-streak2" style="text-align:center"><br><h2><i>Hedge Streak: ${streak}</i></h2></div>`;
- if (lostStreakAt >= 1) {
- let endText = lostStreakAt == 1 ? "seed" : "seeds";
- 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}.`;
- // reset 'lost streak at' after showing to user
- lostStreakAt = 0;
- }
- };
- function preCheck() {
- if (!!document.querySelector('.result-layout_root__NfX12') && location.pathname.startsWith("/game/") && checked == false){
- // avoid multiple requests to GeoGuessr website after guessing a location
- checked = true;
- check();
- }
- else if (!document.querySelector('.result-layout_root__NfX12') && location.pathname.startsWith("/game/") && checked == true) {
- // reset flag for next round
- checked = false;
- }
- };
- function check() {
- const gameId = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
- // check only if this game was not the last game of your streak
- if (gameId != lastGameId) {
- let apiUrl = "";
- if (location.pathname.startsWith("/game/")) {
- apiUrl = "https://www.geoguessr.com/api/v3/games/" + gameId
- };
- fetch(apiUrl)
- .then(res => res.json())
- .then((out) => {
- // check only finished seeds for total score
- if (out.state == "finished") {
- if (parseInt(out.player.totalScore.amount, 10) >= 20000) {
- streak++;
- updateStreak();
- // write this game id in Session storage to prevent counting on refresh (F5)
- lastGameId = gameId;
- sessionStorage.setItem("HedgeLastGameId", lastGameId);
- sessionStorage.setItem("HedgeStreak", streak);
- } else {
- lostStreakAt = streak;
- streak = 0;
- updateStreak();
- sessionStorage.setItem("HedgeStreak", streak);
- }
- addFinalDivDelay();
- };
- }).catch(err => { throw err; });
- } else { addFinalDivDelay(); }
- };
- document.addEventListener('keypress', (e) => {
- switch (e.key) {
- case '1':
- streak++;
- updateStreak();
- sessionStorage.setItem("HedgeStreak", streak);
- break;
- case '2':
- streak--;
- updateStreak();
- sessionStorage.setItem("HedgeStreak", streak);
- break;
- case '0':
- streak = 0;
- updateStreak();
- sessionStorage.setItem("HedgeStreak", streak);
- break;
- }
- });
- document.addEventListener('click', preCheck, false);
- document.addEventListener('click', addCounterDelay, false);
- document.addEventListener('load', addCounterDelay(), false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement