redex557 / Geoguessr Bot

// ==UserScript==
// @name    Geoguessr Bot
// @version    1.0.0
// @description  level up your geoguessr battle royale account
// @author    redex557
// @license MIT
// @copyright 2021, redex557 (https://openuserjs.org/users/redex557)
// @require     https://cdn.jsdelivr.net/gh/bigdatacloudapi/js-reverse-geocode-client@latest/bigdatacloud_reverse_geocode.min.js
// @match    https://www.geoguessr.com/*
// @grant       GM_xmlhttpRequest
// ==/UserScript==

function getTargetUrl() {
  const raw = document.querySelectorAll("#__NEXT_DATA__")[0].text;
  const json = JSON.parse(raw);
  const rounds = json.props.pageProps.game.rounds;
  console.log(rounds);
  const currentRound = rounds[rounds.length - 1];

  const targetUrl = "https://google.com/maps/place/" + currentRound.lat + "," + currentRound.lng;

  return targetUrl;
}

function getLat(d) {
  const json = JSON.parse(d);
  const rounds = json.rounds;
  const currentRound = rounds[rounds.length - 1];

  return currentRound.lat;
}

function getLng(d) {
  const json = JSON.parse(d);
  const rounds = json.rounds;
  const currentRound = rounds[rounds.length - 1];

  return currentRound.lng;
}

function getRound(d) {
  const json = JSON.parse(d);
  const round = json.currentRoundNumber;

  return round;
}

function getToken() {
  const raw = document.querySelectorAll("#__NEXT_DATA__")[0].text;
  const json = JSON.parse(raw);
  const token = json.query.token;

  return token;
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

(function () {
  'use strict';
  var reverseGeocoder = new BDCReverseGeocode();
  var roundn = 0;
  var guessr = function () {
    let done = Array.from(document.getElementsByClassName("popup-view__header")).find(e => e.innerText.includes('qualified'));
    if (done) {
      console.log("done here");
    }
    if (!done) {
      function $(d) {
        return document.getElementById(d);
      }
      roundn += 1;

      GM_xmlhttpRequest({
        method: "post",
        url: "https://game-server.geoguessr.com/api/battle-royale/" + getToken() + "/guess",
        headers: {
          "Content-type": "application/json"
        },
        data: "{\"countryCode\":\"in\",\"roundNumber\":" + roundn + "}",
        onload: function (e) { //console.log(e)
          console.log("Send india guess for round " + getRound(e.responseText));
          /* Get the administrative location information using a set of known coordinates */
          reverseGeocoder.getClientLocation({
            latitude: getLat(e.responseText),
            longitude: getLng(e.responseText),
          }, async function (result) {

            console.log(result.countryCode);
            await sleep(3000);
            GM_xmlhttpRequest({
              method: "post",
              url: "https://game-server.geoguessr.com/api/battle-royale/" + getToken() + "/guess",
              headers: {
                "Content-type": "application/json"
              },
              data: "{\"countryCode\":\"" + result.countryCode.toLowerCase() + "\",\"roundNumber\":" + getRound(e.responseText) + "}",
              onload: function (f) {
                console.log("Send guess for round " + getRound(e.responseText) + " : " + result.countryCode.toLowerCase());
                roundn = getRound(e.responseText);
              }
            });
          });
        }
      });
    }
  };
  var restarter = function () {
    let but = Array.from(document.getElementsByClassName("button button--medium button--ghost")).find(e => e.textContent === 'Play again');
    if (but) {
      but.click();
    }
  };
  window.setInterval(guessr, 10000);
  window.setInterval(restarter, 30000);
})();