Geoguessr Freedom Script

Play Geoguessr with Yandex, Baidu, Kakao streetviews, and Aris coverage with good movement. Credit to kommu and MrAmericanMike to the original Yandex Script. Thanks to Alok, Mapper for advise for map making.

Fra 09.12.2021. Se den seneste versjonen.

  1. // ==UserScript==
  2. // @name Geoguessr Freedom Script
  3. // @description Play Geoguessr with Yandex, Baidu, Kakao streetviews, and Aris coverage with good movement. Credit to kommu and MrAmericanMike to the original Yandex Script. Thanks to Alok, Mapper for advise for map making.
  4. // @version 3.0.0b
  5. // @include https://www.geoguessr.com/*
  6. // @run-at document-start
  7. // @license MIT
  8. // @namespace https://greatest.deepsurf.us/users/838374
  9. // ==/UserScript==
  10.  
  11. var YANDEX_API_KEY = "b704b5a9-3d67-4d19-b702-ec7807cecfc6";
  12. var BAIDU_API_KEY = "8dQ9hZPGEQnqg9r0O1C8Ate2N6P8Zk92";
  13. var KAKAO_API_KEY = "cbacbe41e3a223d794f321de4f3e247b";
  14.  
  15. myLog("Geoguessr Freedom Script");
  16.  
  17. const MAPS_API_URL = "https://maps.googleapis.com/maps/api/js?";
  18.  
  19. let PLAYER = null;
  20. let PLAYER2 = null;
  21. let PLAYER3 = null;
  22. let rv = null;
  23. let ROUND = 0;
  24. let YANDEX_INJECTED = false;
  25. let BAIDU_INJECTED = false;
  26. let KAKAO_INJECTED = false;
  27. let NEW_ROUND_LOADED = false;
  28. let COMPASS = null;
  29. let PANORAMA_MAP = false;
  30. let CURRENT_ROUND_DATA = null;
  31. let nextPlayer = "Google";
  32. let global_lat = 0;
  33. let global_lng = 0;
  34. let global_panoID = null;
  35. let playerLoaded = false;
  36.  
  37. let BR = false;
  38. let eventlistener = false;
  39. let loc_list = [];
  40. let pops = true;
  41.  
  42. let teleportLoaded = false;
  43.  
  44. let krCoordinates = [38.75292321084364, 124.2804539232574, 33.18509676203202, 129.597381999198]
  45. let yandex_map = false;
  46.  
  47.  
  48. /**
  49. * Helper Functions
  50. */
  51.  
  52. function myLog(...args) {
  53. console.log(...args);
  54. }
  55. function myHighlight(...args) {
  56. console.log(`%c${[...args]}`, "color: dodgerblue; font-size: 24px;");
  57. }
  58.  
  59. function hex2a(hexx) {
  60. var hex = hexx.toString();//force conversion
  61. var str = '';
  62. for (var i = 0; i < hex.length; i += 2)
  63. str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
  64. return str;
  65. }
  66.  
  67. function FindPointAtDistanceFrom(startPoint, initialBearingRadians, distanceKilometres) {
  68. const radiusEarthKilometres = 6371.01;
  69. var distRatio = distanceKilometres / radiusEarthKilometres;
  70. var distRatioSine = Math.sin(distRatio);
  71. var distRatioCosine = Math.cos(distRatio);
  72.  
  73. var startLatRad = DegreesToRadians(startPoint.lat);
  74. var startLonRad = DegreesToRadians(startPoint.lng);
  75.  
  76. var startLatCos = Math.cos(startLatRad);
  77. var startLatSin = Math.sin(startLatRad);
  78.  
  79. var endLatRads = Math.asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.cos(initialBearingRadians)));
  80.  
  81. var endLonRads = startLonRad
  82. + Math.atan2(
  83. Math.sin(initialBearingRadians) * distRatioSine * startLatCos,
  84. distRatioCosine - startLatSin * Math.sin(endLatRads));
  85.  
  86. return { lat: RadiansToDegrees(endLatRads), lng: RadiansToDegrees(endLonRads) };
  87. }
  88.  
  89. function DegreesToRadians(degrees) {
  90. const degToRadFactor = Math.PI / 180;
  91. return degrees * degToRadFactor;
  92. }
  93.  
  94. function RadiansToDegrees(radians) {
  95. const radToDegFactor = 180 / Math.PI;
  96. return radians * radToDegFactor;
  97. }
  98.  
  99. function runAsClient(f) {
  100. var s = document.createElement("script");
  101. s.type = "text/javascript";
  102. s.text = "(async () => { try { await (" + f.toString() + ")(); } catch (e) { console.error(e); }})();";
  103. document.body.appendChild(s);
  104. }
  105.  
  106. window.runAsClient = runAsClient;
  107.  
  108. /**
  109. * Resolves succesfully after detecting that Google Maps API was loaded in a page
  110. *
  111. * @returns Promise
  112. */
  113. function gmp() {
  114. return new Promise((resolve, reject) => {
  115. let scriptObserver = new MutationObserver((mutations, observer) => {
  116. for (let mutation of mutations) {
  117. for (let node of mutation.addedNodes) {
  118. if (node.tagName === "SCRIPT" && node.src.startsWith(MAPS_API_URL)) {
  119. scriptObserver.disconnect();
  120. scriptObserver = undefined;
  121. myLog("Detected Google Maps API");
  122. node.onload = () => resolve();
  123. }
  124. }
  125. }
  126. });
  127.  
  128. let bodyDone = false;
  129. let headDone = false;
  130.  
  131. let injectorObserver = new MutationObserver((mutations, observer) => {
  132. if (!bodyDone && document.body) {
  133. bodyDone = true;
  134. myLog("Body Observer Injected");
  135. scriptObserver && scriptObserver.observe(document.body, {
  136. childList: true
  137. });
  138. }
  139. if (!headDone && document.head) {
  140. headDone = true;
  141. myLog("Head Observer Injected");
  142. scriptObserver && scriptObserver.observe(document.head, {
  143. childList: true
  144. });
  145. }
  146. if (headDone && bodyDone) {
  147. myLog("Body and Head Observers Injected");
  148. observer.disconnect();
  149. }
  150. });
  151.  
  152. injectorObserver.observe(document.documentElement, {
  153. childList: true,
  154. subtree: true
  155. });
  156. });
  157. }
  158.  
  159. /**
  160. * Creates teleportation and Kakao switch button
  161. *
  162. * @returns Promise
  163. */
  164.  
  165. function ArisKakao() {
  166. runAsClient(() => {
  167.  
  168. let radi = 100;
  169. const google = window.google;
  170. let curPosition, mapPlayer;
  171. let kakao_enabled = true;
  172.  
  173. const isGamePage = () => location.pathname.startsWith("/challenge/") || location.pathname.startsWith("/results/") || location.pathname.startsWith("/game/");
  174.  
  175. const getPosition = sv => (
  176. {
  177. lat: sv.position.lat(),
  178. lng: sv.position.lng(),
  179. });
  180.  
  181. // Handle the street view being navigated
  182. const onMove = (sv) => {
  183. try {
  184. if (!isGamePage()) return;
  185. const position = getPosition(sv);
  186. curPosition = position;
  187. }
  188. catch (e) {
  189. console.error("GeoGuessr Path Logger Error:", e);
  190. }
  191. };
  192.  
  193. // Helper Functions
  194.  
  195. function FindPointAtDistanceFrom(startPoint, initialBearingRadians, distanceKilometres) {
  196. const radiusEarthKilometres = 6371.01;
  197. var distRatio = distanceKilometres / radiusEarthKilometres;
  198. var distRatioSine = Math.sin(distRatio);
  199. var distRatioCosine = Math.cos(distRatio);
  200.  
  201. var startLatRad = DegreesToRadians(startPoint.lat);
  202. var startLonRad = DegreesToRadians(startPoint.lng);
  203.  
  204. var startLatCos = Math.cos(startLatRad);
  205. var startLatSin = Math.sin(startLatRad);
  206.  
  207. var endLatRads = Math.asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.cos(initialBearingRadians)));
  208.  
  209. var endLonRads = startLonRad
  210. + Math.atan2(
  211. Math.sin(initialBearingRadians) * distRatioSine * startLatCos,
  212. distRatioCosine - startLatSin * Math.sin(endLatRads));
  213.  
  214. return { lat: RadiansToDegrees(endLatRads), lng: RadiansToDegrees(endLonRads) };
  215. }
  216.  
  217. function DegreesToRadians(degrees) {
  218. const degToRadFactor = Math.PI / 180;
  219. return degrees * degToRadFactor;
  220. }
  221.  
  222. function RadiansToDegrees(radians) {
  223. const radToDegFactor = 180 / Math.PI;
  224. return radians * radToDegFactor;
  225. }
  226.  
  227. function svCheck(data, status) {
  228. if (status === 'OK') {
  229. console.log("OK for " + data.location.latLng + " at ID " + data.location.pano);
  230. let l = data.location.latLng.toString().split(',');
  231. let lat = l[0].replaceAll('(', '')
  232. let lng = l[1].replaceAll(')', '')
  233. console.log(lat);
  234. console.log(lng)
  235. console.log(curPosition)
  236. if (lat == curPosition.lat && lng == curPosition.lng)
  237. {
  238. console.log("Trying more distance");
  239. radi += 100;
  240. teleportButton.innerHTML = "Teleport " + radi + " m";
  241. }
  242. else
  243. {
  244. mapPlayer.setPosition(data.location.latLng);
  245. if (radi > 200)
  246. {
  247. radi = 100;
  248. }
  249. }
  250. }
  251. else {
  252. console.log("STATUS NOT OK");
  253. radi += 100;
  254. teleportButton.innerHTML = "Teleport " + radi + " m";
  255. }
  256. }
  257.  
  258. // When a StreetViewPanorama is constructed, add a listener for moving
  259. const oldSV = google.maps.StreetViewPanorama;
  260. const svService = new google.maps.StreetViewService();
  261.  
  262. google.maps.StreetViewPanorama = Object.assign(function (...args) {
  263. const res = oldSV.apply(this, args);
  264. this.addListener('position_changed', () => onMove(this));
  265. mapPlayer = this;
  266. return res;
  267. }, {
  268. prototype: Object.create(oldSV.prototype)
  269. });
  270.  
  271. var showButtons = document.createElement("Button");
  272. showButtons.id = "Show Buttons"
  273. showButtons.innerHTML = "Script Buttons";
  274. showButtons.style =
  275. "top:6em;right:0.5em;width:6em;height:4.5em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  276. document.body.appendChild(showButtons);
  277. showButtons.addEventListener("click", () => {
  278. if (hide) {
  279. teleportButton.style.visibility = "";
  280. plusButton.style.visibility = "";
  281. minusButton.style.visibility = "";
  282. resetButton.style.visibility = "";
  283. googleKakaoButton.style.visibility = "";
  284. hide = false;
  285. }
  286. else {
  287. teleportButton.style.visibility = "hidden";
  288. plusButton.style.visibility = "hidden";
  289. minusButton.style.visibility = "hidden";
  290. resetButton.style.visibility = "hidden"
  291. googleKakaoButton.style.visibility = "hidden";
  292. hide = true;
  293. }
  294. });
  295.  
  296. var teleportButton = document.createElement("Button");
  297. teleportButton.id = "Main Button"
  298. teleportButton.innerHTML = "Teleport 100m";
  299. teleportButton.style =
  300. "visibility:hidden;top:6em;right:9.5em;width:10em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  301. document.body.appendChild(teleportButton);
  302. teleportButton.addEventListener("click", () => {
  303. let heading = mapPlayer.getPov().heading;
  304. let place = FindPointAtDistanceFrom(curPosition, DegreesToRadians(heading), radi * 0.001)
  305. svService.getPanorama({ location: place, radius: 1000 }, svCheck);
  306.  
  307. });
  308.  
  309. var plusButton = document.createElement("Button");
  310. plusButton.id = "plus"
  311. plusButton.innerHTML = "+";
  312. plusButton.style =
  313. "visibility:hidden;top:6em;right:20em;width:2em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  314. document.body.appendChild(plusButton);
  315. plusButton.addEventListener("click", () => {
  316. if (radi > 21 && radi < 200) {
  317. radi = radi + 25;
  318. }
  319. teleportButton.innerHTML = "Teleport " + radi + " m";
  320. });
  321.  
  322. var minusButton = document.createElement("Button");
  323. minusButton.id = "minus"
  324. minusButton.innerHTML = "-";
  325. minusButton.style =
  326. "visibility:hidden;top:6em;right:7em;width:2em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  327. document.body.appendChild(minusButton);
  328. minusButton.addEventListener("click", () => {
  329. if (radi > 26) {
  330. radi = radi - 25;
  331. }
  332. teleportButton.innerHTML = "Teleport " + radi + " m";
  333. });
  334.  
  335. var resetButton = document.createElement("Button");
  336. resetButton.id = "reset"
  337. resetButton.innerHTML = "Reset";
  338. resetButton.style =
  339. "visibility:hidden;top:8.5em;right:17.5em;width:4.5em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  340. document.body.appendChild(resetButton);
  341. resetButton.addEventListener("click", () => {
  342. radi = 100;
  343. teleportButton.innerHTML = "Teleport " + radi + " m";
  344. });
  345.  
  346. var googleKakaoButton = document.createElement("Button");
  347. googleKakaoButton.id = "switch"
  348. googleKakaoButton.innerHTML = "Switch coverage";
  349. googleKakaoButton.style =
  350. "visibility:hidden;top:8.5em;right:7em;width:10em;height:2em;position:absolute;z-index:99999;background-color: #4CAF50;border: none;color: white;padding: none;text-align: center;vertical-align: text-top;text-decoration: none;display: inline-block;font-size: 16px;";
  351. document.body.appendChild(googleKakaoButton);
  352. googleKakaoButton.addEventListener("click", () => {
  353. let GOOGLE_MAPS_CANVAS = document.querySelector(".game-layout__panorama-canvas");
  354. let KAKAO_MAPS_CANVAS = document.getElementById("roadview");
  355. if (kakao_enabled) {
  356. GOOGLE_MAPS_CANVAS.style.visibility = "";
  357. KAKAO_MAPS_CANVAS.style.visibility = "hidden";
  358. kakao_enabled = false;
  359. }
  360. else {
  361. GOOGLE_MAPS_CANVAS.style.visibility = "hidden";
  362. KAKAO_MAPS_CANVAS.style.visibility = "";
  363. kakao_enabled = true;
  364. console.log("use Kakao");
  365. }
  366. });
  367.  
  368. let hide = true;
  369.  
  370. console.log("Buttons Loaded");
  371.  
  372. });
  373. }
  374.  
  375. /**
  376. * Hide or Reveal such buttons
  377. */
  378.  
  379. function setHidden(cond)
  380. {
  381. if (cond)
  382. {
  383. if (document.getElementById("Show Buttons") != null)
  384. {
  385. document.getElementById("Show Buttons").style.visibility = "hidden";
  386. if (document.getElementById("Main Button") != null)
  387. {
  388. document.getElementById("plus").style.visibility = "hidden";
  389. document.getElementById("minus").style.visibility = "hidden";
  390. document.getElementById("reset").style.visibility = "hidden";
  391. document.getElementById("Main Button").style.visibility = "hidden";
  392. document.getElementById("switch").style.visibility = "hidden";
  393. }
  394. }
  395. }
  396. else
  397. {
  398. if (document.getElementById("Show Buttons") != null)
  399. {
  400. document.getElementById("Show Buttons").style.visibility = "";
  401. }
  402. }
  403. }
  404.  
  405. function setDisable(cond)
  406. {
  407. if (cond !== "Google")
  408. {
  409. if (document.getElementById("Main Button") != null)
  410. {
  411. document.getElementById("plus").disabled = true;
  412. document.getElementById("plus").style.backgroundColor = "red";
  413. document.getElementById("minus").disabled = true;
  414. document.getElementById("minus").style.backgroundColor = "red";
  415. document.getElementById("reset").disabled = true;
  416. document.getElementById("reset").style.backgroundColor = "red";
  417. document.getElementById("Main Button").disabled = true;
  418. document.getElementById("Main Button").style.backgroundColor = "red";
  419. document.getElementById("switch").disabled = true;
  420. document.getElementById("switch").style.backgroundColor = "red";
  421.  
  422. if (cond === "Kakao")
  423. {
  424. document.getElementById("switch").disabled = false;
  425. document.getElementById("switch").style.backgroundColor = "#4CAF50";
  426. }
  427. else
  428. {
  429. document.getElementById("switch").disabled = true;
  430. document.getElementById("switch").style.backgroundColor = "red";
  431. }
  432. }
  433. }
  434. else
  435. {
  436. if (document.getElementById("Main Button") != null)
  437. {
  438. document.getElementById("plus").disabled = false;
  439. document.getElementById("plus").style.backgroundColor = "#4CAF50";
  440. document.getElementById("minus").disabled = false;
  441. document.getElementById("minus").style.backgroundColor = "#4CAF50";
  442. document.getElementById("reset").disabled = false;
  443. document.getElementById("reset").style.backgroundColor = "#4CAF50";
  444. document.getElementById("Main Button").disabled = false;
  445. document.getElementById("Main Button").style.backgroundColor = "#4CAF50";
  446. document.getElementById("switch").disabled = false;
  447. document.getElementById("switch").style.backgroundColor = "#4CAF50";
  448. document.getElementById("switch").disabled = true;
  449. document.getElementById("switch").style.backgroundColor = "red";
  450. }
  451. }
  452. }
  453.  
  454. /**
  455. * This observer stays alive while the script is running
  456. */
  457. function launchObserver() {
  458. ArisKakao();
  459. myHighlight("Main Observer");
  460. const OBSERVER = new MutationObserver((mutations, observer) => {
  461. detectGamePage();
  462. });
  463. OBSERVER.observe(document.head, { attributes: true, childList: true, subtree: true });
  464. }
  465.  
  466. /**
  467. * Once the Google Maps API was loaded we can do more stuff
  468. */
  469. gmp().then(() => {
  470. launchObserver();
  471. });
  472.  
  473.  
  474. /**
  475. * Detects if the current page contains /game/ or /challenge/ in it
  476. */
  477.  
  478. function detectGamePage() {
  479. let toLoad = !playerLoaded && !PLAYER && !PLAYER2 && !PLAYER3 && !YANDEX_INJECTED && !BAIDU_INJECTED && !KAKAO_INJECTED
  480. const PATHNAME = window.location.pathname;
  481. if (PATHNAME.startsWith("/game/") || PATHNAME.startsWith("/challenge/")) {
  482. // myLog("Game page");
  483. BR = false;
  484.  
  485. if (toLoad) {
  486. loadPlayers();
  487. }
  488. waitLoad();
  489. }
  490. else if (PATHNAME.startsWith("/battle-royale/")) {
  491. if (document.querySelector(".br-game-layout") == null) {
  492. // myLog("Battle Royale Lobby");
  493. }
  494. else {
  495. // myLog("Battle Royale");
  496. BR = true;
  497.  
  498. if (toLoad) {
  499. loadPlayers();
  500. }
  501. waitLoad();
  502. }
  503. }
  504. else {
  505. //myLog("Not a Game page");
  506. ROUND = 0;
  507. PLAYER = null;
  508. PLAYER2 = null;
  509. PLAYER3 = null;
  510. COMPASS = null;
  511. eventlistener = false;
  512. BAIDU_INJECTED = false;
  513. YANDEX_INJECTED = false;
  514. KAKAO_INJECTED = false;
  515. global_lat = 0;
  516. global_lng = 0;
  517. global_panoID = null;
  518. playerLoaded = false;
  519. loc_list = [];
  520. setHidden(true);
  521. yandex_map = false;
  522. }
  523. }
  524.  
  525. /**
  526. * Wait for various players to load
  527. */
  528.  
  529. function waitLoad() {
  530. if (!PLAYER || !PLAYER2 || !PLAYER3 || !YANDEX_INJECTED || !BAIDU_INJECTED || !KAKAO_INJECTED) {
  531. setTimeout(waitLoad, 100);
  532. } else {
  533. checkRound();
  534. }
  535. }
  536.  
  537. /**
  538. * Checks for round changes
  539. */
  540. function checkRound() {
  541. // myLog("Check Round");
  542. if (!BR) {
  543. let currentRound = getRoundFromPage();
  544. if (ROUND != currentRound) {
  545. myHighlight("New round");
  546. ROUND = currentRound;
  547. NEW_ROUND_LOADED = true;
  548. COMPASS = null;
  549. loc_list = [];
  550. getMapData();
  551. nextButtonCallback();
  552. }
  553. }
  554. else {
  555. myHighlight("BR New round");
  556. NEW_ROUND_LOADED = true;
  557. COMPASS = null;
  558. loc_list = [];
  559. getMapData();
  560. }
  561. }
  562.  
  563. function nextButtonCallback()
  564. {
  565. let nextButton = document.querySelector("button[data-qa='close-round-result']");
  566. if (nextButton != null)
  567. {
  568. nextButton.addEventListener("click", (e) => {
  569. if (document.getElementById("Show Buttons") != null)
  570. {
  571. myLog("try to hide show buttons")
  572. document.getElementById("Show Buttons").style.visibility = "";
  573. }
  574. })
  575. }
  576. else
  577. {
  578. setTimeout(nextButtonCallback, 500);
  579. }
  580. }
  581.  
  582. function guessButtonCallback()
  583. {
  584. let guessButton = document.querySelector("button[data-qa='perform-guess']");
  585.  
  586. if (guessButton!= null)
  587. {
  588.  
  589. guessButton.addEventListener("click", (e) => {
  590. if (document.getElementById("Show Buttons") != null)
  591. {
  592. myLog("try to hide show buttons")
  593. document.getElementById("Show Buttons").style.visibility = "hidden";
  594. setHidden(true);
  595. }
  596. })
  597. }
  598. else
  599. {
  600. setTimeout(guessButtonCallback, 500);
  601. }
  602. }
  603.  
  604. /**
  605. * Load different streetview players
  606. */
  607.  
  608. function loadPlayers() {
  609. playerLoaded = true;
  610. if (!BR)
  611. {
  612. getSeed().then((data) => {
  613. myLog(data);
  614. if (data.mapName.includes("A United World"))
  615. {
  616. injectYandexScript().then(() => {
  617. myLog("Ready to inject Yandex player");
  618. injectYandexPlayer();
  619. }).catch((error) => {
  620. myLog(error);
  621. });
  622. }
  623. else if (data.mapName.includes("Yandex"))
  624. {
  625. yandex_map = true;
  626. injectYandexScript().then(() => {
  627. myLog("Ready to inject Yandex player");
  628. injectYandexPlayer();
  629. }).catch((error) => {
  630. myLog(error);
  631. });
  632. }
  633. else{
  634. YANDEX_API_KEY = "";
  635. myLog("Not a Yandex map");
  636. YANDEX_INJECTED = true;
  637. PLAYER = "YD";
  638. injectYandexScript().then(() => {
  639. myLog("Ready to inject Yandex player");
  640. }).catch((error) => {
  641. myLog(error);
  642. });
  643. }
  644. setHidden(false);
  645.  
  646. }).catch((error) => {
  647. myLog(error);
  648. });
  649. }
  650. initializeCanvas();
  651. injectBaiduPlayer();
  652. injectKakaoScript().then(() => {
  653. myLog("Ready to inject Kakao player");
  654. }).catch((error) => {
  655. myLog(error);
  656. });
  657. if (BR)
  658. {
  659. injectYandexScript().then(() => {
  660. myLog("Ready to inject Yandex player");
  661. injectYandexPlayer();
  662. }).catch((error) => {
  663. myLog(error);
  664. });
  665. }
  666. }
  667.  
  668. /**
  669. * Handles Return to start and undo
  670. */
  671.  
  672. function handleReturnToStart() {
  673. myLog("handleReturnToStart listener attached");
  674. let rtsButton = document.querySelector("button[data-qa='return-to-start']");
  675. if (rtsButton != null) {
  676. rtsButton.addEventListener("click", (e) => {
  677. goToLocation(CURRENT_ROUND_DATA);
  678. const elementClicked = e.target;
  679. elementClicked.setAttribute('listener', 'true');
  680. myLog("Return to start");
  681. });
  682. }
  683. guessButtonCallback();
  684. }
  685.  
  686. function handleUndo() {
  687. let undoButton = document.querySelector("button[data-qa='undo-move']");
  688. undoButton.addEventListener("click", (e) => {
  689. if (loc_list.length > 0) {
  690. goToUndoMove();
  691. myLog("Undo Move");
  692. }
  693. })
  694.  
  695. }
  696.  
  697. function getMapData() {
  698. getSeed().then((data) => {
  699. myHighlight("Seed data");
  700. myLog(data);
  701. if (BR) {
  702. // myLog("hello");
  703. let origin = false;
  704. if (!CURRENT_ROUND_DATA) {
  705. CURRENT_ROUND_DATA = data
  706. origin = true;
  707. }
  708.  
  709. if (origin || !(data.currentRoundNumber === CURRENT_ROUND_DATA.currentRoundNumber)) {
  710. if (!origin) {
  711. CURRENT_ROUND_DATA = data;
  712. }
  713. locationCheck(data);
  714. // myLog(data)
  715. goToLocation(data, nextPlayer);
  716. setTimeout(function () { handleReturnToStart(); }, 2000);
  717.  
  718. }
  719. }
  720. else {
  721. locationCheck(data);
  722. goToLocation(data, nextPlayer);
  723. setTimeout(function () { handleReturnToStart(); }, 2000);
  724. setTimeout(function () { handleUndo(); }, 2000);
  725. hideButtons();
  726. }
  727. }).catch((error) => {
  728. myLog(error);
  729. });
  730. }
  731.  
  732. function hideButtons() {
  733. let CHECKPOINT = document.querySelector("button[data-qa='set-checkpoint']");
  734. let ZOOM_IN = document.querySelector("button[data-qa='pano-zoom-in']");
  735. let ZOOM_OUT = document.querySelector("button[data-qa='pano-zoom-out']");
  736. // myLog(CHECKPOINT);
  737. if (nextPlayer === "Google") {
  738. if (CHECKPOINT != null) {
  739. CHECKPOINT.style.visibility = "";
  740. ZOOM_IN.style.visibility = "";
  741. ZOOM_OUT.style.visibility = "";
  742. myLog("Buttons Unhidden");
  743. }
  744. }
  745. else {
  746. if (CHECKPOINT != null) {
  747. CHECKPOINT.style.visibility = "hidden";
  748. ZOOM_IN.style.visibility = "hidden";
  749. ZOOM_OUT.style.visibility = "hidden";
  750. myLog("Buttons Hidden");
  751. }
  752. }
  753. }
  754.  
  755. function locationCheck(data) {
  756. if (BR) {
  757. global_lat = data.rounds[data.currentRoundNumber - 1].lat;
  758. global_lng = data.rounds[data.currentRoundNumber - 1].lng;
  759. global_panoID = data.rounds[data.currentRoundNumber - 1].panoId;
  760. }
  761. else {
  762. global_lat = data.rounds[data.round - 1].lat;
  763. global_lng = data.rounds[data.round - 1].lng;
  764. global_panoID = data.rounds[data.round - 1].panoId;
  765. }
  766. myLog(global_lat);
  767. myLog(global_lng);
  768. myLog(krCoordinates);
  769. if ( krCoordinates[0] > global_lat && krCoordinates[2] < global_lat && krCoordinates[1] < global_lng && krCoordinates[3] > global_lng)
  770. {
  771. nextPlayer = "Kakao";
  772. }
  773. else
  774. {
  775. if (global_panoID) {
  776. let output = hex2a(global_panoID);
  777. let type = output.substring(0, 5);
  778. if (type === "BDMAP") {
  779. nextPlayer = "Baidu";
  780. let coord = output.substring(5);
  781. global_lat = coord.split(",")[0];
  782. global_lng = coord.split(",")[1];
  783. }
  784. else if (type === "YDMAP" ) {
  785. nextPlayer = "Yandex";
  786. }
  787. else {
  788. nextPlayer = "Google";
  789. }
  790. }
  791. else {
  792. if (yandex_map)
  793. {
  794. nextPlayer = "Yandex";
  795. }
  796. else
  797. {
  798. nextPlayer = "Google";
  799. }
  800. }
  801. }
  802. setDisable(nextPlayer);
  803. myLog(nextPlayer);
  804. injectCanvas();
  805. }
  806.  
  807.  
  808. function initializeCanvas() {
  809. let GAME_CANVAS = ""
  810. if (!BR) {
  811. GAME_CANVAS = document.querySelector(".game-layout__canvas");
  812. }
  813. else {
  814. GAME_CANVAS = document.querySelector(".br-game-layout__canvas");
  815. }
  816. GAME_CANVAS.id = "player";
  817. myLog("Canvas injected");
  818. }
  819.  
  820. function injectCanvas() {
  821. Google();
  822. Baidu();
  823. Kakao();
  824. Yandex();
  825. ZoomControls();
  826. }
  827.  
  828.  
  829. function Google() {
  830. let GOOGLE_MAPS_CANVAS = ""
  831. if (!BR) {
  832. GOOGLE_MAPS_CANVAS = document.querySelector(".game-layout__panorama-canvas");
  833. }
  834. else {
  835. GOOGLE_MAPS_CANVAS = document.querySelector(".br-game-layout__panorama-canvas");
  836. }
  837. if (nextPlayer === "Google") {
  838. GOOGLE_MAPS_CANVAS.style.visibility = "";
  839. myLog("Google Canvas loaded");
  840. }
  841. else {
  842. GOOGLE_MAPS_CANVAS.style.visibility = "hidden";
  843. myLog("Google Canvas hidden");
  844. }
  845.  
  846. }
  847.  
  848.  
  849. function Baidu() {
  850. let BAIDU_MAPS_CANVAS = document.getElementById("PanoramaMap");
  851. myLog("Baidu canvas");
  852. if (nextPlayer === "Baidu") {
  853. BAIDU_MAPS_CANVAS.style.visibility = "";
  854. myLog("Baidu Canvas loaded");
  855. }
  856. else {
  857. BAIDU_MAPS_CANVAS.style.visibility = "hidden";
  858. myLog("Baidu Canvas hidden");
  859. }
  860.  
  861. }
  862.  
  863. function Kakao() {
  864. let KAKAO_MAPS_CANVAS = document.getElementById("roadview");
  865. myLog("Kakao canvas");
  866. if (nextPlayer === "Kakao") {
  867. KAKAO_MAPS_CANVAS.style.visibility = "";
  868. myLog("Kakao Canvas loaded");
  869. }
  870. else {
  871. KAKAO_MAPS_CANVAS.style.visibility = "hidden";
  872. myLog("Kakao Canvas hidden");
  873. }
  874.  
  875. }
  876.  
  877. function Yandex() {
  878. let YANDEX_MAPS_CANVAS = document.querySelector(".ymaps-2-1-79-panorama-screen");
  879. if (YANDEX_MAPS_CANVAS != null)
  880. {
  881. myLog("Yandex canvas");
  882. /* myLog(YANDEX_MAPS_CANVAS); */
  883. if (nextPlayer === "Yandex") {
  884. YANDEX_MAPS_CANVAS.style.visibility = "";
  885. myLog("Yandex Canvas loaded");
  886. }
  887. else {
  888. YANDEX_MAPS_CANVAS.style.visibility = "hidden";
  889. myLog("Yandex Canvas hidden");
  890. }
  891. }
  892.  
  893. }
  894.  
  895. function ZoomControls() {
  896. let style = `
  897. .ymaps-2-1-79-panorama-gotoymaps {display: none !important;}
  898. .game-layout__controls {bottom: 8rem !important; left: 1rem !important;}
  899. `;
  900.  
  901. let style_element = document.createElement("style");
  902. style_element.innerHTML = style;
  903. document.body.appendChild(style_element);
  904. }
  905.  
  906. function goToLocation(data) {
  907. myLog("Going to location");
  908. if (nextPlayer === "Yandex") {
  909. let options = {};
  910. PLAYER.moveTo([global_lat, global_lng], options);
  911. PLAYER.setDirection([0, 16]);
  912. PLAYER.setSpan([10, 67]);
  913. }
  914. else if (nextPlayer === "Baidu") {
  915. let a = new BMap.Point(global_lng, global_lat);
  916. PLAYER2.setPov({ heading: -40, pitch: 6 });
  917. PLAYER2.setPosition(a);
  918. }
  919. else if (nextPlayer === "Kakao") {
  920. var roadviewClient = new kakao.maps.RoadviewClient();
  921. var position = new kakao.maps.LatLng(global_lat, global_lng);
  922. roadviewClient.getNearestPanoId(position, 500, function (panoId) {
  923. PLAYER3.setPanoId(panoId, position);
  924. });
  925. }
  926. else {
  927.  
  928. }
  929.  
  930. }
  931.  
  932. function goToUndoMove(data) {
  933. /* myLog(global_lat);
  934. myLog(global_lng); */
  935. if (nextPlayer === "Yandex") {
  936. let options = {};
  937. let place2 = null;
  938. if (loc_list.length === 1) {
  939. place2 = loc_list[0];
  940. }
  941. else {
  942. place2 = loc_list.pop();
  943. }
  944. pops = false;
  945. // myLog(place2);
  946. // myLog(loc_list)
  947. PLAYER.moveTo([place2[0], place2[1]], options);
  948. PLAYER.setDirection([place2[2], place2[3]]);
  949. PLAYER.setSpan([10, 67]);
  950. }
  951. else if (nextPlayer === "Kakao") {
  952. let place3 = null;
  953. if (loc_list.length === 1) {
  954. place3 = loc_list[0];
  955. }
  956. else {
  957. place3 = loc_list.pop();
  958. }
  959. pops = false;
  960. var position = new kakao.maps.LatLng(place3[0], place3[1]);
  961. PLAYER3.setPanoId(place3[2], position);
  962. }
  963. else if (nextPlayer === "Baidu") {
  964. /* myLog(PLAYER2);
  965. myLog(global_lng);
  966. myLog(global_lat); */
  967. let place = null;
  968. if (loc_list.length === 1) {
  969. place = loc_list[0];
  970. }
  971. else {
  972. place = loc_list.pop();
  973. }
  974. // myLog(place);
  975. let a = new BMap.Point(place[1], place[0]);
  976. let pov = { heading: place[2], pitch: place[3] };
  977. PLAYER2.setPosition(a);
  978. PLAYER2.setPov(pov);
  979. // myLog(loc_list);
  980. /* myLog(PLAYER2); */
  981. }
  982. else {
  983.  
  984. }
  985.  
  986. }
  987.  
  988.  
  989.  
  990.  
  991. /**
  992. * Gets the seed data for the current game
  993. *
  994. * @returns Promise with seed data as object
  995. */
  996. function getSeed() {
  997. myLog("getSeed called");
  998. return new Promise((resolve, reject) => {
  999. let token = getToken();
  1000. let URL;
  1001. let cred = ""
  1002.  
  1003. const PATHNAME = window.location.pathname;
  1004.  
  1005. if (PATHNAME.startsWith("/game/")) {
  1006. URL = `https://www.geoguessr.com/api/v3/games/${token}`;
  1007. }
  1008. else if (PATHNAME.startsWith("/challenge/")) {
  1009. URL = `https://www.geoguessr.com/api/v3/challenges/${token}/game`;
  1010. }
  1011. else if (PATHNAME.startsWith("/battle-royale/")) {
  1012. URL = `https://game-server.geoguessr.com/api/battle-royale/${token}`;
  1013. }
  1014. if (BR) {
  1015. fetch(URL, {
  1016. // Include credentials to GET from the endpoint
  1017. credentials: 'include'
  1018. })
  1019. .then((response) => response.json())
  1020. .then((data) => {
  1021. resolve(data);
  1022. })
  1023. .catch((error) => {
  1024. reject(error);
  1025. });
  1026. }
  1027. else {
  1028. fetch(URL)
  1029. .then((response) => response.json())
  1030. .then((data) => {
  1031. resolve(data);
  1032. })
  1033. .catch((error) => {
  1034. reject(error);
  1035. });
  1036. }
  1037. });
  1038. }
  1039.  
  1040. /**
  1041. * Gets the token from the current URL
  1042. *
  1043. * @returns token
  1044. */
  1045. function getToken() {
  1046. const PATHNAME = window.location.pathname;
  1047. if (PATHNAME.startsWith("/game/")) {
  1048. return PATHNAME.replace("/game/", "");
  1049. }
  1050. else if (PATHNAME.startsWith("/challenge/")) {
  1051. return PATHNAME.replace("/challenge/", "");
  1052. }
  1053. else if (PATHNAME.startsWith("/battle-royale/")) {
  1054. return PATHNAME.replace("/battle-royale/", "");
  1055. }
  1056. }
  1057.  
  1058. /**
  1059. * Gets the round number from the ongoing game from the page itself
  1060. *
  1061. * @returns Round number
  1062. */
  1063. function getRoundFromPage() {
  1064. const roundData = document.querySelector("div[data-qa='round-number']");
  1065. if (roundData) {
  1066. let roundElement = roundData.querySelector("div:last-child");
  1067. if (roundElement) {
  1068. let round = parseInt(roundElement.innerText.charAt(0));
  1069. if (!isNaN(round) && round >= 1 && round <= 5) {
  1070. return round;
  1071. }
  1072. }
  1073. }
  1074. else {
  1075. return ROUND;
  1076. }
  1077. }
  1078.  
  1079.  
  1080. /**
  1081. * Injects Yandex Script
  1082. */
  1083. function injectYandexScript() {
  1084. return new Promise((resolve, reject) => {
  1085. if (!YANDEX_INJECTED) {
  1086. if (YANDEX_API_KEY === "") {
  1087. // let canvas = document.getElementById("player");
  1088. // canvas.innerHTML = `
  1089. // <div style="text-align: center;">
  1090. // <h1 style="margin-top: 80px; font-size: 48px;">YOU NEED YANDEX API KEY<h1>
  1091. // <p><a target="_blank" href="https://yandex.com/dev/maps/jsapi/doc/2.1/quick-start/index.html?from=techmapsmain">Get it here</a></p>
  1092. // <br/>
  1093. // <p>After that you need to add that key into this script in</p>
  1094. // <code>const YANDEX_API_KEY = "";</code>
  1095. // </div>
  1096. // `;
  1097. myLog("No Yandex Key")
  1098. reject();
  1099. }
  1100. else {
  1101. const SCRIPT = document.createElement("script");
  1102. SCRIPT.type = "text/javascript";
  1103. SCRIPT.async = true;
  1104. SCRIPT.onload = () => {
  1105. ymaps.ready(() => {
  1106. YANDEX_INJECTED = true;
  1107. myHighlight("Yandex API Loaded");
  1108. resolve();
  1109. });
  1110. }
  1111. SCRIPT.src = `https://api-maps.yandex.ru/2.1/?lang=en_US&apikey=${YANDEX_API_KEY}`;
  1112. document.body.appendChild(SCRIPT);
  1113. }
  1114. }
  1115. else {
  1116. resolve();
  1117. }
  1118. });
  1119. }
  1120.  
  1121. /**
  1122. * Injects Yandex Player and calls handleReturnToStart
  1123. */
  1124. function injectYandexPlayer() {
  1125. let lat = 41.321861;
  1126. let lng = 69.212920;
  1127.  
  1128. let options = {
  1129. "direction": [0, 16],
  1130. "span": [10, 67],
  1131. "controls": ["zoomControl"]
  1132. };
  1133. ymaps.panorama.createPlayer("player", [lat, lng], options)
  1134. .done((player) => {
  1135. PLAYER = player;
  1136. PLAYER.events.add("directionchange", (e) => {
  1137. updateCompass();
  1138. });
  1139. PLAYER.events.add("panoramachange", (e) => {
  1140. if (pops && !BR) {
  1141. let num = PLAYER.getPanorama().getPosition();
  1142. let pov = PLAYER.getDirection();
  1143. // myLog(num);
  1144. // myLog(pov);
  1145. loc_list.push([num[0], num[1], pov[0], pov[1]]);
  1146. let btn = document.querySelector("button[data-qa='undo-move']");
  1147. if (loc_list.length > 1) {
  1148. btn.disabled = false;
  1149. btn.classList.remove('styles_disabled__W_k45');
  1150. }
  1151. // myLog(loc_list);
  1152. }
  1153. pops = true;
  1154.  
  1155. });
  1156. myHighlight("Player injected");
  1157. });
  1158.  
  1159. }
  1160.  
  1161.  
  1162. /**
  1163. * Injects Baidu
  1164. */
  1165. function injectBaiduScript() {
  1166. return new Promise((resolve, reject) => {
  1167. if (!BAIDU_INJECTED) {
  1168. if (BAIDU_API_KEY === "") {
  1169. let canvas = document.getElementById("player");
  1170. canvas.innerHTML = `
  1171. <div style="text-align: center;">
  1172. <h1 style="margin-top: 80px; font-size: 48px;">YOU NEED Baidu API KEY<h1>
  1173. <br/>
  1174. <p>After that you need to add that key into this script in</p>
  1175. <code>const YANDEX_API_KEY = "";</code>
  1176. </div>
  1177. `;
  1178. reject();
  1179. }
  1180. else {
  1181. const SCRIPT = document.createElement("script");
  1182. SCRIPT.type = "text/javascript";
  1183. SCRIPT.async = true;
  1184. SCRIPT.src = `https://api.map.baidu.com/api?v=3.0&ak=${BAIDU_API_KEY}&callback=init`;
  1185. document.body.appendChild(SCRIPT);
  1186.  
  1187. let canvas = document.createElement("bmap");
  1188. if (BR) {
  1189. canvas.innerHTML = `
  1190. <div id="PanoramaMap" class="br-game-layout__panorama" style="zIndex: 99999,position: "absolute", top: 0, left: 0, width: '100%', height: '100%',"> </div>
  1191. `;
  1192. }
  1193. else {
  1194. canvas.innerHTML = `
  1195. <div id="PanoramaMap" class="game-layout__panorama" style="zIndex: 99999,position: "absolute", top: 0, left: 0, width: '100%', height: '100%',"> </div>
  1196. `;
  1197. }
  1198.  
  1199. var div = document.getElementById("player");
  1200. div.appendChild(canvas);
  1201.  
  1202. SCRIPT.addEventListener('load', () => {
  1203. myHighlight("Baidu API Loaded");
  1204. // resolve(BMap);
  1205. let timeout = 0;
  1206. let interval = setInterval(() => {
  1207. if (timeout >= 20) {
  1208. reject();
  1209. clearInterval(interval);
  1210. }
  1211. if (typeof BMap !== "undefined") {
  1212. BAIDU_INJECTED = true;
  1213. resolve(BMap);
  1214. clearInterval(interval);
  1215. }
  1216. timeout += 1;
  1217. }, 500);
  1218. })
  1219.  
  1220.  
  1221. }
  1222. }
  1223. else {
  1224. resolve();
  1225. }
  1226. });
  1227. }
  1228.  
  1229.  
  1230.  
  1231. /**
  1232. * Injects Baidu
  1233. */
  1234. function injectKakaoScript() {
  1235. return new Promise((resolve, reject) => {
  1236. if (!KAKAO_INJECTED) {
  1237. if (KAKAO_API_KEY === "") {
  1238. let canvas = document.getElementById("player");
  1239. canvas.innerHTML = `
  1240. <div style="text-align: center;">
  1241. <h1 style="margin-top: 80px; font-size: 48px;">YOU NEED Kakao API KEY<h1>
  1242. <br/>
  1243. <p>After that you need to add that key into this script in</p>
  1244. <code>const YANDEX_API_KEY = "";</code>
  1245. </div>
  1246. `;
  1247. reject();
  1248. }
  1249. else {
  1250.  
  1251. let canvas = document.createElement("kmap");
  1252. if (BR) {
  1253. canvas.innerHTML = `
  1254. <div id="roadview" class="br-game-layout__panorama" style="zIndex: 99999,position: "absolute", top: 0, left: 0, width: '100%', height: '100%',"> </div>
  1255. `;
  1256. }
  1257. else {
  1258. canvas.innerHTML = `
  1259. <div id="roadview" class="game-layout__panorama" style="zIndex: 99999,position: "absolute", top: 0, left: 0, width: '100%', height: '100%',"> </div>
  1260. `;
  1261. }
  1262.  
  1263. var div = document.getElementById("player");
  1264. div.appendChild(canvas);
  1265.  
  1266. const SCRIPT = document.createElement("script");
  1267. SCRIPT.async = true;
  1268. // SCRIPT.type = "text/javascript";
  1269. SCRIPT.src = `//dapi.kakao.com/v2/maps/sdk.js?appkey=${KAKAO_API_KEY}&autoload=false`;
  1270. document.body.appendChild(SCRIPT);
  1271. SCRIPT.onload = () => {
  1272. kakao.maps.load(function () {
  1273. var position = new kakao.maps.LatLng(33.450701, 126.560667);
  1274. let roadviewContainer = document.getElementById('roadview');
  1275. PLAYER3 = new kakao.maps.Roadview(roadviewContainer);
  1276. var panoId = 1023434522;
  1277. PLAYER3.setPanoId(panoId, position);
  1278. KAKAO_INJECTED = true;
  1279. kakao.maps.event.addListener(PLAYER3, 'panoid_changed', function() {
  1280. if (pops && !BR) {
  1281. let latlng = PLAYER3.getPosition();
  1282. let lat = latlng.getLat();
  1283. let lng = latlng.getLng();
  1284. let pID = PLAYER3.getPanoId();
  1285. // myLog(num);
  1286. // myLog(pov);
  1287. loc_list.push([lat, lng, pID]);
  1288. let btn = document.querySelector("button[data-qa='undo-move']");
  1289. if (loc_list.length > 1) {
  1290. btn.disabled = false;
  1291. btn.classList.remove('styles_disabled__W_k45');
  1292. }
  1293. myLog(loc_list);
  1294. }
  1295. pops = true;
  1296. });
  1297. resolve();
  1298. });
  1299. };
  1300.  
  1301.  
  1302.  
  1303. }
  1304. }
  1305. else {
  1306. resolve();
  1307. }
  1308. });
  1309. }
  1310.  
  1311.  
  1312. /**
  1313. * Injects Baidu Player and calls handleReturnToStart
  1314. */
  1315. function injectBaiduPlayer() {
  1316. let lat = 0;
  1317. let lng = 0;
  1318. injectBaiduScript().then(BMap => {
  1319. PLAYER2 = new BMap.Panorama('PanoramaMap');
  1320. PLAYER2.setPov({ heading: -40, pitch: 6 });
  1321. PLAYER2.setPosition(new BMap.Point(lng, lat));
  1322. if (!eventlistener && !BR) {
  1323. myLog("position_listener attached");
  1324. PLAYER2.addEventListener('position_changed', function (e) {
  1325. eventlistener = true;
  1326. // myLog('position_changed')
  1327. let num = PLAYER2.getPosition();
  1328. let pov = PLAYER2.getPov();
  1329. if (num.lat != 0 && num.lat != 27.101448386472637) {
  1330. loc_list.push([num.lat, num.lng, pov.heading, pov.pitch]);
  1331. }
  1332. let btn = document.querySelector("button[data-qa='undo-move']");
  1333. if (loc_list.length > 1) {
  1334. btn.disabled = false;
  1335. btn.classList.remove('styles_disabled__W_k45');
  1336. }
  1337. // myLog(loc_list);
  1338. })
  1339. }
  1340. });
  1341. }
  1342.  
  1343.  
  1344.  
  1345. /**
  1346. * Goes to location when PLAYER already exists
  1347. *
  1348. * @param {*} data
  1349. */
  1350.  
  1351. /**
  1352. * Updates the compass to match Yandex Panorama facing
  1353. */
  1354. function updateCompass() {
  1355. if (!COMPASS) {
  1356. let compass = document.querySelector("img.compass__indicator");
  1357. if (compass != null) {
  1358. COMPASS = compass;
  1359. let direction = PLAYER.getDirection()[0] * -1;
  1360. COMPASS.setAttribute("style", `transform: rotate(${direction}deg);`);
  1361. }
  1362. }
  1363. else {
  1364. let direction = PLAYER.getDirection()[0] * -1;
  1365. COMPASS.setAttribute("style", `transform: rotate(${direction}deg);`);
  1366. }
  1367. }
  1368.