Elo tracker

Adds an elo tracker to the GeoGuessr website

目前為 2024-08-15 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Elo tracker
// @version      0.2
// @description  Adds an elo tracker to the GeoGuessr website
// @match        https://www.geoguessr.com/*
// @run-at document-start
// @author       eru
// @license      MIT
// @icon         https://www.google.com/s2/favicons?sz=64&domain=geoguessr.com
// @grant        none
// @namespace https://greasyfork.org/users/1348455
// ==/UserScript==

/* jshint esversion: 8 */

const API_URL = 'https://ggstats.eu';

async function getUserInfo() {
    try {
        const response = await fetch('https://www.geoguessr.com/api/v3/profiles');
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
        const data = await response.json();
        return {nick: data.user.nick, hexId: data.user.id, pinUrl: data.user.pin.url, level: data.user.br.level};
    } catch (error) {
        console.error('Error fetching data:', error);
        return null;
    }
}

function updateElo(data) {
    fetch(API_URL+'/add-elo', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    }).then(response => response.json()).then(data => console.log('Success:', data)).catch((error) => console.error('Error:', error));
}

function run() {
    const elo_string = document.querySelector('div[class*="game-finished-ranked_box__"]').firstChild.firstChild.firstChild.textContent;
    getUserInfo()
      .then(infos => {
        const { nick, hexId, pinUrl, level } = infos;
        const data = {
          name: nick,
          player_hexid: hexId,
          elo: parseInt(elo_string),
          pinUrl: pinUrl,
          level: parseInt(level)
        };
        updateElo(data);
    })
}

let lastDoCheckCall = 0;
// Any changes in the DOM triggers the MutationObserver callback
new MutationObserver(async (mutations) => {
    // First make sure we are in a game
    if (!location.pathname.includes("/duels/") || lastDoCheckCall >= (Date.now() - 50)) return;
    lastDoCheckCall = Date.now();
    // Then make sure we are in the finished game screen
    if (!document.querySelector('div[class*="game-finished-ranked_container__"]')) {
        sessionStorage.setItem("Checked", 0);
        return;
    } else if ((sessionStorage.getItem("Checked") || 0) == 0) {
        run();
        sessionStorage.setItem("Checked", 1);
    }
}).observe(document.body, { subtree: true, childList: true });