SubSymmetry

Country Streak Counter (Automated, Enabled on Challenges)

Jul 15th, 2020 (edited)
2,963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.49 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Country Streak Counter (Automated, Enabled on Challenges)
  3. // @include /^(https?)?(\:)?(\/\/)?([^\/]*\.)?geoguessr\.com($|\/.*)/
  4. // @description Adds a country streak counter to the GeoGuessr website
  5. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  6. // @require http://code.jquery.com/jquery-3.4.1.min.js
  7. // @grant GM_addStyle
  8. // ==/UserScript==
  9. // last updated March 16 2022
  10.  
  11. let API_Key = 'ENTER_API_KEY_HERE'; //Replace ENTER_API_KEY_HERE with your API Key (so keep the quote marks)
  12.  
  13. var $ = window.jQuery
  14. let streak = parseInt(sessionStorage.getItem("Streak"), 10);
  15. let streakBackup = parseInt(sessionStorage.getItem("StreakBackup"), 10);
  16. let checked = parseInt(sessionStorage.getItem("Checked"), 10);
  17. let last_guess = [0,0];
  18. let guess_btn = null;
  19. let check_result = null;
  20.  
  21. if(sessionStorage.getItem("Streak") == null){
  22. sessionStorage.setItem("Streak", 0);
  23. streak = 0;
  24. };
  25.  
  26. if(sessionStorage.getItem("StreakBackup") == null){
  27. sessionStorage.setItem("StreakBackup", 0);
  28. streakBackup = 0;
  29. };
  30.  
  31. if(sessionStorage.getItem("Checked") == null){
  32. sessionStorage.setItem("Checked", 0);
  33. checked = 0;
  34. };
  35.  
  36. var CountryDict = {
  37. AF: 'AF',
  38. AX: 'FI',
  39. AL: 'AL',
  40. DZ: 'DZ',
  41. AS: 'US',
  42. AD: 'AD',
  43. AO: 'AO',
  44. AI: 'GB',
  45. AQ: 'AQ',
  46. AG: 'AG',
  47. AR: 'AR',
  48. AM: 'AM',
  49. AW: 'NL',
  50. AU: 'AU',
  51. AT: 'AT',
  52. AZ: 'AZ',
  53. BS: 'BS',
  54. BH: 'BH',
  55. BD: 'BD',
  56. BB: 'BB',
  57. BY: 'BY',
  58. BE: 'BE',
  59. BZ: 'BZ',
  60. BJ: 'BJ',
  61. BM: 'GB',
  62. BT: 'BT',
  63. BO: 'BO',
  64. BQ: 'NL',
  65. BA: 'BA',
  66. BW: 'BW',
  67. BV: 'NO',
  68. BR: 'BR',
  69. IO: 'GB',
  70. BN: 'BN',
  71. BG: 'BG',
  72. BF: 'BF',
  73. BI: 'BI',
  74. KH: 'KH',
  75. CM: 'CM',
  76. CA: 'CA',
  77. CV: 'CV',
  78. KY: 'UK',
  79. CF: 'CF',
  80. TD: 'TD',
  81. CL: 'CL',
  82. CN: 'CN',
  83. CX: 'AU',
  84. CC: 'AU',
  85. CO: 'CO',
  86. KM: 'KM',
  87. CG: 'CG',
  88. CD: 'CD',
  89. CK: 'NZ',
  90. CR: 'CR',
  91. CI: 'CI',
  92. HR: 'HR',
  93. CU: 'CU',
  94. CY: 'CY',
  95. CZ: 'CZ',
  96. DK: 'DK',
  97. DJ: 'DJ',
  98. DM: 'DM',
  99. DO: 'DO',
  100. EC: 'EC',
  101. EG: 'EG',
  102. SV: 'SV',
  103. GQ: 'GQ',
  104. ER: 'ER',
  105. EE: 'EE',
  106. ET: 'ET',
  107. FK: 'GB',
  108. FO: 'DK',
  109. FJ: 'FJ',
  110. FI: 'FI',
  111. FR: 'FR',
  112. GF: 'FR',
  113. PF: 'FR',
  114. TF: 'FR',
  115. GA: 'GA',
  116. GM: 'GM',
  117. GE: 'GE',
  118. DE: 'DE',
  119. GH: 'GH',
  120. GI: 'GI',
  121. GR: 'GR',
  122. GL: 'DK',
  123. GD: 'GD',
  124. GP: 'FR',
  125. GU: 'US',
  126. GT: 'GT',
  127. GG: 'GB',
  128. GN: 'GN',
  129. GW: 'GW',
  130. GY: 'GY',
  131. HT: 'HT',
  132. HM: 'AU',
  133. VA: 'VA',
  134. HN: 'HN',
  135. HK: 'CN',
  136. HU: 'HU',
  137. IS: 'IS',
  138. IN: 'IN',
  139. ID: 'ID',
  140. IR: 'IR',
  141. IQ: 'IQ',
  142. IE: 'IE',
  143. IM: 'GB',
  144. IL: 'IL',
  145. IT: 'IT',
  146. JM: 'JM',
  147. JP: 'JP',
  148. JE: 'GB',
  149. JO: 'JO',
  150. KZ: 'KZ',
  151. KE: 'KE',
  152. KI: 'KI',
  153. KR: 'KR',
  154. KW: 'KW',
  155. KG: 'KG',
  156. LA: 'LA',
  157. LV: 'LV',
  158. LB: 'LB',
  159. LS: 'LS',
  160. LR: 'LR',
  161. LY: 'LY',
  162. LI: 'LI',
  163. LT: 'LT',
  164. LU: 'LU',
  165. MO: 'CN',
  166. MK: 'MK',
  167. MG: 'MG',
  168. MW: 'MW',
  169. MY: 'MY',
  170. MV: 'MV',
  171. ML: 'ML',
  172. MT: 'MT',
  173. MH: 'MH',
  174. MQ: 'FR',
  175. MR: 'MR',
  176. MU: 'MU',
  177. YT: 'FR',
  178. MX: 'MX',
  179. FM: 'FM',
  180. MD: 'MD',
  181. MC: 'MC',
  182. MN: 'MN',
  183. ME: 'ME',
  184. MS: 'GB',
  185. MA: 'MA',
  186. MZ: 'MZ',
  187. MM: 'MM',
  188. NA: 'NA',
  189. NR: 'NR',
  190. NP: 'NP',
  191. NL: 'NL',
  192. AN: 'NL',
  193. NC: 'FR',
  194. NZ: 'NZ',
  195. NI: 'NI',
  196. NE: 'NE',
  197. NG: 'NG',
  198. NU: 'NZ',
  199. NF: 'AU',
  200. MP: 'US',
  201. NO: 'NO',
  202. OM: 'OM',
  203. PK: 'PK',
  204. PW: 'PA',
  205. PS: 'IL',
  206. PA: 'PA',
  207. PG: 'PG',
  208. PY: 'PY',
  209. PE: 'PE',
  210. PH: 'PH',
  211. PN: 'GB',
  212. PL: 'PL',
  213. PT: 'PT',
  214. PR: 'US',
  215. QA: 'QA',
  216. RE: 'FR',
  217. RO: 'RO',
  218. RU: 'RU',
  219. RW: 'RW',
  220. BL: 'FR',
  221. SH: 'GB',
  222. KN: 'KN',
  223. LC: 'LC',
  224. MF: 'FR',
  225. PM: 'FR',
  226. VC: 'VC',
  227. WS: 'WS',
  228. SM: 'SM',
  229. ST: 'ST',
  230. SA: 'SA',
  231. SN: 'SN',
  232. RS: 'RS',
  233. SC: 'SC',
  234. SL: 'SL',
  235. SG: 'SG',
  236. SK: 'SK',
  237. SI: 'SI',
  238. SB: 'SB',
  239. SO: 'SO',
  240. ZA: 'ZA',
  241. GS: 'GB',
  242. ES: 'ES',
  243. LK: 'LK',
  244. SD: 'SD',
  245. SR: 'SR',
  246. SJ: 'NO',
  247. SZ: 'SZ',
  248. SE: 'SE',
  249. CH: 'CH',
  250. SY: 'SY',
  251. TW: 'TW',
  252. TJ: 'TJ',
  253. TZ: 'TZ',
  254. TH: 'TH',
  255. TL: 'TL',
  256. TG: 'TG',
  257. TK: 'NZ',
  258. TO: 'TO',
  259. TT: 'TT',
  260. TN: 'TN',
  261. TR: 'TR',
  262. TM: 'TM',
  263. TC: 'GB',
  264. TV: 'TV',
  265. UG: 'UG',
  266. UA: 'UA',
  267. AE: 'AE',
  268. GB: 'GB',
  269. US: 'US',
  270. UM: 'US',
  271. UY: 'UY',
  272. UZ: 'UZ',
  273. VU: 'VU',
  274. VE: 'VE',
  275. VN: 'VN',
  276. VG: 'GB',
  277. VI: 'US',
  278. WF: 'FR',
  279. EH: 'MA',
  280. YE: 'YE',
  281. ZM: 'ZM',
  282. ZW: 'ZW',
  283. SX: 'NL',
  284. CW: 'NL'
  285. };
  286.  
  287. function updateStreak(newVariable){
  288. streak = newVariable;
  289. if(document.getElementById("country-streak") != null){
  290. document.getElementById("country-streak").innerHTML = `<div id="country-streak"><div class="status_value__xZMNY">${streak}</div></div>`;
  291. };
  292. if(document.getElementById("country-streak2") != null && document.querySelectorAll('div[data-qa]')[8] != null && !document.querySelector('.standard-final-result_section___B3ne')){
  293. document.getElementById("country-streak2").innerHTML = `<br><h2><i>Country Streak: ${streak}</i></h2>`;
  294. };
  295. if(document.getElementById("country-streak2") != null && !!document.querySelector('.standard-final-result_section___B3ne')){
  296. document.getElementById("country-streak2").innerHTML = `<br><h2><i>Country Streak: ${streak}</i></h2>`;
  297. };
  298. };
  299.  
  300. function addCounter(newDiv0){
  301. if(document.getElementsByClassName("status_section__8uP8o").length == 3){
  302. newDiv0 = document.createElement("div")
  303. newDiv0.className = 'status_section__8uP8o';
  304. document.getElementsByClassName("status_inner__1eytg")[0].appendChild(newDiv0);
  305. newDiv0.innerHTML = `<div class="status_label__SNHKT">Streak</div><div id="country-streak"><div class="status_value__xZMNY">${streak}</div></div>`;
  306. };
  307. if(document.getElementsByClassName("status_section__8uP8o").length == 4 && document.getElementsByClassName("status_label__SNHKT")[3].innerText == "TIME LEFT"){
  308. newDiv0 = document.createElement("div")
  309. newDiv0.className = 'status_section__8uP8o';
  310. document.getElementsByClassName("status_inner__1eytg")[0].appendChild(newDiv0);
  311. newDiv0.innerHTML = `<div class="status_label__SNHKT">Streak</div><div id="country-streak"><div class="status_value__xZMNY">${streak}</div></div>`;
  312. };
  313. };
  314.  
  315. function addCounterOnRefresh(){
  316. setTimeout(function(){
  317. addCounter();
  318. },50);
  319. setTimeout(function(){
  320. addCounter();
  321. },300);
  322. };
  323.  
  324. function addCounter2(){
  325. addCounter();
  326. if(document.getElementsByClassName("status_section__8uP8o").length == 0){
  327. setTimeout(function(){
  328. addCounter();
  329. if(document.getElementsByClassName("status_section__8uP8o").length == 0){
  330. setTimeout(function(){
  331. addCounter();
  332. if(document.getElementsByClassName("status_section__8uP8o").length == 0){
  333. setTimeout(function(){
  334. addCounter();
  335. if(document.getElementsByClassName("status_section__8uP8o").length == 0){
  336. setTimeout(function(){
  337. addCounter();
  338. if(document.getElementsByClassName("status_section__8uP8o").length == 0){
  339. setTimeout(function(){
  340. addCounter();
  341. }, 4000);
  342. };
  343. }, 3000);
  344. };
  345. }, 2000);
  346. };
  347. }, 1200);
  348. };
  349. }, 400);
  350. };
  351. };
  352.  
  353. async function getUserAsync(location){
  354. if(location[0] <= -85.05){
  355. return 'AQ';
  356. }
  357. else{
  358. let api = "https://api.bigdatacloud.net/data/reverse-geocode?latitude="+location[0]+"&longitude="+location[1]+"&localityLanguage=en&key="+API_Key
  359. let response = await fetch(api)
  360. .then(res => res.json())
  361. .then((out) => {
  362. return CountryDict[out.countryCode]
  363. })
  364. return response;
  365. };
  366. };
  367.  
  368. function check(){
  369. const game_tag = window.location.href.substring(window.location.href.lastIndexOf('/') + 1)
  370. let api_url = ""
  371. if (location.pathname.startsWith("/game/")){
  372. api_url = "https://www.geoguessr.com/api/v3/games/"+game_tag
  373. }
  374. else if (location.pathname.startsWith("/challenge/")){
  375. api_url = "https://www.geoguessr.com/api/v3/challenges/"+game_tag+"/game"
  376. };
  377. let rounds_tab = document.getElementsByClassName("status_value__xZMNY")
  378. let current_round = rounds_tab[1].innerHTML.substr(0, rounds_tab[1].innerHTML.indexOf('/')).trim();
  379. fetch(api_url)
  380. .then(res => res.json())
  381. .then((out) => {
  382. rounds_tab = document.getElementsByClassName("status_value__xZMNY")
  383. current_round = rounds_tab[1].innerHTML.substr(0, rounds_tab[1].innerHTML.indexOf('/')).trim();
  384. let guess_counter = out.player.guesses.length
  385. let guess = [out.player.guesses[guess_counter-1].lat,out.player.guesses[guess_counter-1].lng]
  386. if (guess[0] == last_guess[0] && guess[1] == last_guess[1]){
  387. return;
  388. };
  389. last_guess = guess
  390. let location = [out.rounds[guess_counter-1].lat,out.rounds[guess_counter-1].lng]
  391. getUserAsync(guess)
  392. .then(gue => {
  393. getUserAsync(location)
  394. .then(loc => {
  395. if (gue == loc){
  396. updateStreak(streak + 1);
  397. sessionStorage.setItem("Streak", streak);
  398. streakBackup = streak;
  399. sessionStorage.setItem("StreakBackup", streak);
  400. }
  401. else {
  402. if(streak == 0){
  403. streakBackup = 0;
  404. sessionStorage.setItem("StreakBackup", 0);
  405. };
  406. if(streak == 1){
  407. updateStreak(0);
  408. sessionStorage.setItem("Streak", 0);
  409. document.getElementById("country-streak2").innerHTML = `<br><h2><i>Country Streak: ${streak}</i></h2>Your streak ended after correctly guessing <div class="guess-description-distance_distanceLabel__23Opn"><div class="styles_root__eoNIJ styles_variantWhiteTransparent__20ADs styles_roundnessSmall__ZbRvs"><div class="styles_start__u_cL2 styles_right__hZg0u"></div><div class="guess-description-distance_distanceValue__BRuXF">${streakBackup}</div><div class="styles_end__euu3r styles_right__hZg0u"></div></div></div> country.`;
  410. };
  411. if(streak > 1){
  412. updateStreak(0);
  413. sessionStorage.setItem("Streak", 0);
  414. document.getElementById("country-streak2").innerHTML = `<br><h2><i>Country Streak: ${streak}</i></h2>Your streak ended after correctly guessing <div class="guess-description-distance_distanceLabel__23Opn"><div class="styles_root__eoNIJ styles_variantWhiteTransparent__20ADs styles_roundnessSmall__ZbRvs"><div class="styles_start__u_cL2 styles_right__hZg0u"></div><div class="guess-description-distance_distanceValue__BRuXF">${streakBackup}</div><div class="styles_end__euu3r styles_right__hZg0u"></div></div></div> countries in a row.`;
  415. };
  416. };
  417. });
  418. });
  419. })
  420. .catch(err => { throw err });
  421.  
  422. };
  423.  
  424. function runCheck(){
  425. if (!!document.querySelector('.result-layout_root__NfX12') && (location.pathname.startsWith("/game/") || location.pathname.startsWith("/challenge/")) && sessionStorage.getItem("Checked") == 0){
  426. check();
  427. checked = checked + 1;
  428. sessionStorage.setItem("Checked", checked);
  429. }
  430. else if (!document.querySelector('.result-layout_root__NfX12') && (location.pathname.startsWith("/game/") || location.pathname.startsWith("/challenge/")) && sessionStorage.getItem("Checked") != 0){
  431. checked = 0;
  432. sessionStorage.setItem("Checked", checked)
  433. };
  434. };
  435.  
  436. function addStreakRoundResult(newDiv1){
  437. if(document.getElementById("country-streak2") == null && document.querySelectorAll('div[data-qa]')[8] != null && !document.querySelector('.standard-final-result_section___B3ne') && (location.pathname.startsWith("/game/") || location.pathname.startsWith("/challenge/"))){
  438. newDiv1 = document.createElement("div")
  439. document.querySelectorAll('div[data-qa]')[8].appendChild(newDiv1);
  440. newDiv1.innerHTML = `<div id="country-streak2" style="text-align:center"><br><h2><i>Country Streak: ${streak}</i></h2></div>`;
  441. };
  442. };
  443.  
  444. function addStreakGameSummary(newDiv2){
  445. if(document.getElementById("country-streak2") == null && !!document.querySelector('.standard-final-result_section___B3ne') && (location.pathname.startsWith("/game/") || location.pathname.startsWith("/challenge/"))){
  446. newDiv2 = document.createElement("div")
  447. document.getElementsByClassName("progress-bar_background__A6ZDS progress-bar_expandHeight__W_59W")[0].appendChild(newDiv2);
  448. newDiv2.innerHTML = `<div id="country-streak2" style="text-align:center"><br><h2><i>Country Streak: ${streak}</i></h2></div>`;
  449. };
  450. };
  451.  
  452. function addStreak(){
  453. runCheck();
  454. setTimeout(function(){
  455. runCheck();
  456. }, 250);
  457. setTimeout(function(){
  458. runCheck();
  459. }, 500);
  460. setTimeout(function(){
  461. runCheck();
  462. }, 1200);
  463. setTimeout(function(){
  464. runCheck();
  465. }, 2000);
  466. setTimeout(function(){
  467. addStreakRoundResult();
  468. },300);
  469. setTimeout(function(){
  470. addStreakRoundResult();
  471. },500);
  472. setTimeout(function(){
  473. addStreakGameSummary();
  474. },200);
  475. setTimeout(function(){
  476. addStreakGameSummary();
  477. },400);
  478. setTimeout(function(){
  479. addStreakRoundResult();
  480. addStreakGameSummary();
  481. },1200);
  482. setTimeout(function(){
  483. addStreakRoundResult();
  484. addStreakGameSummary();
  485. },2000);
  486. };
  487.  
  488. document.addEventListener('keypress', (e) => {
  489. switch (e.key){
  490. case '1':
  491. updateStreak(streak + 1);
  492. sessionStorage.setItem("Streak", streak);
  493. streakBackup = streak;
  494. sessionStorage.setItem("StreakBackup", streak);
  495. break;
  496. case '2':
  497. updateStreak(streak - 1);
  498. sessionStorage.setItem("Streak", streak);
  499. streakBackup = streak;
  500. sessionStorage.setItem("StreakBackup", streak);
  501. break;
  502. case '8':
  503. updateStreak(streakBackup + 1);
  504. sessionStorage.setItem("Streak", streak);
  505. streakBackup = streak;
  506. sessionStorage.setItem("StreakBackup", streak);
  507. break;
  508. case '0':
  509. updateStreak(0);
  510. sessionStorage.setItem("Streak", 0);
  511. streakBackup = 0;
  512. sessionStorage.setItem("StreakBackup", 0);
  513. break;
  514. };
  515. });
  516.  
  517. document.addEventListener('click', addCounter2, false);
  518. document.addEventListener('click', addStreak, false);
  519. document.addEventListener('load', addCounterOnRefresh(), false);
Add Comment
Please, Sign In to add comment