Geoguessr Team Duels Advanced Options

Adds extra options to team duel settings.

当前为 2023-02-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Geoguessr Team Duels Advanced Options
  3. // @description Adds extra options to team duel settings.
  4. // @version 0.1.3
  5. // @author macca#8949
  6. // @license MIT
  7. // @match https://www.geoguessr.com/*
  8. // @run-at document-start
  9. // @grant none
  10. // @namespace https://greasyfork.org/en/scripts/452579-geoguessr-team-duels-advanced-options
  11. // ==/UserScript==
  12.  
  13. const getGameId = () => {
  14. return window.location.href.split('/')[4];
  15. }
  16.  
  17. async function fetchWithCors(url, method, body) {
  18. return await fetch(url, {
  19. "headers": {
  20. "accept": "*/*",
  21. "accept-language": "en-US,en;q=0.8",
  22. "content-type": "application/json",
  23. "sec-fetch-dest": "empty",
  24. "sec-fetch-mode": "cors",
  25. "sec-fetch-site": "same-site",
  26. "sec-gpc": "1",
  27. "x-client": "web"
  28. },
  29. "referrer": "https://www.geoguessr.com/",
  30. "referrerPolicy": "strict-origin-when-cross-origin",
  31. "body": JSON.stringify(body),
  32. "method": method,
  33. "mode": "cors",
  34. "credentials": "include"
  35. })
  36. }
  37.  
  38. window.modifySetting = (e, settingName) => {
  39. let newValue = e.value;
  40. if (settingName === 'multiplierIncrement') {
  41. newValue *= 10;
  42. newValue = Math.round(newValue);
  43. } else {
  44. newValue *= 1; // string to number conversion
  45. newValue = Math.round(newValue);
  46. }
  47.  
  48. // Fetch the game options
  49. fetchWithCors(`https://game-server.geoguessr.com/api/lobby/${getGameId()}/join`, "POST", {})
  50. .then((response) => response.json())
  51. .then((data) => {
  52. let gameOptions = data.gameOptions;
  53. gameOptions[settingName] = newValue;
  54. // Push the updated options
  55. fetchWithCors(`https://game-server.geoguessr.com/api/lobby/${getGameId()}/options`, "PUT", gameOptions);
  56. });
  57. }
  58.  
  59. let optionTextInputInnerHTML = (id, settingName, text) =>
  60. `<input type="text" id="${id}" onchange="modifySetting(this, '${settingName}')" style="text-align: center; background: rgba(255,255,255,0.1); color: white; border: none; border-radius: 5px; width: 60px;"><div class="game-options_optionLabel__dJ_Cy">${text}</div>`
  61.  
  62. function makeCustomTextInput(elt, id, settingName, text) {
  63. if (elt == null) elt = document.createElement('label');
  64. elt.className = "game-options_option__eCz9o game-options_editableOption__Mpvar";
  65. elt.innerHTML = optionTextInputInnerHTML(id, settingName, text);
  66. elt.style = "display: flex; flex-direction: column; align-items: center; justify-content: center;";
  67. return elt
  68. }
  69.  
  70. function insertAfter(elt, child) {
  71. child.parentNode.insertBefore(elt, child.nextSibling);
  72. }
  73.  
  74. let observer = new MutationObserver((mutations) => {
  75. if ((window.location.href.includes('duels') || window.location.href.includes('live-challenge')) && document.querySelector('.game-options_slider__JwEe2')) {
  76. if (window.location.href.includes('duels')) {
  77. let healthElement = document.querySelectorAll('.game-options_lives__wNSwd')[0].parentElement;
  78. let multiplierIncrementBox = document.querySelectorAll('.game-options_lives__wNSwd')[1].parentElement
  79. makeCustomTextInput(healthElement, "health-input", "initialHealth", "Health");
  80. makeCustomTextInput(multiplierIncrementBox, "increment-input", "multiplierIncrement", "Multiplier Increment");
  81. }
  82.  
  83. let timeElementBox = makeCustomTextInput(null, "time-input", "roundTime", "Time");
  84. let timeSliderBox = document.querySelectorAll('.game-options_slider__JwEe2')[0].parentNode.parentNode;
  85. insertAfter(timeElementBox, timeSliderBox);
  86. timeSliderBox.remove()
  87.  
  88. fetchWithCors(`https://game-server.geoguessr.com/api/lobby/${getGameId()}/join`, "POST", {})
  89. .then((response) => response.json())
  90. .then((data) => {
  91. let gameOptions = data.gameOptions;
  92. if (window.location.href.includes('duels')) document.querySelector('#health-input').value = gameOptions.initialHealth;
  93. if (window.location.href.includes('duels')) document.querySelector('#increment-input').value = gameOptions.multiplierIncrement / 10;
  94. document.querySelector('#time-input').value = gameOptions.roundTime;
  95. });
  96. }
  97. });
  98.  
  99.  
  100. observer.observe(document.body, {
  101. characterDataOldValue: false,
  102. subtree: true,
  103. childList: true,
  104. characterData: false
  105. });