Hackatime Analyzer Helper

Easily open Hackatime Analyzer with data from leaderboard users

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Hackatime Analyzer Helper
// @namespace    http://tampermonkey.net/
// @version      2025-06-20
// @description  Easily open Hackatime Analyzer with data from leaderboard users
// @author       ShyMike
// @match        https://hackatime.hackclub.com/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
    "use strict";

    const periodMap = {
        "daily": "today",
        "last_7_days": "last_7_days",
    };
    let selectedPeriod = "today";

    function addAnalyzeButtons() {
        const rowsContainer = document.querySelector('.divide-y.divide-gray-800');
        const rows = rowsContainer.children;

        const urlParams = new URLSearchParams(window.location.search);
        const periodType = urlParams.get('period_type');

        if (periodType === "weekly") {
            return;
        }

        if (periodType && periodMap[periodType]) {
            selectedPeriod = periodMap[periodType];
        } else {
            selectedPeriod = "today";
        }

        Array.from(rows).forEach((row) => {
            if (row.querySelector(".check-btn")) return;

            const link = row.querySelector("a");
            if (!link) return;

            const userId = link.href.split("=").pop();
            if (!userId) return;

            const checkBtn = document.createElement("button");
            checkBtn.textContent = "Check";
            checkBtn.className = "check-btn";
            checkBtn.style.marginLeft = "0.5em";
            checkBtn.style.padding = "0.2rem";
            checkBtn.style.fontSize = "0.7rem";
            checkBtn.addEventListener("click", () => {
                window.open(
                    `https://hackatime-analyzer.pages.dev/?id=${userId}&period=${selectedPeriod}`,
                    "_blank"
                );
            });

            row.appendChild(checkBtn);
        });
    }

    function checkAndAddButtons() {
        if (location.pathname.includes("/leaderboards")) {
            addAnalyzeButtons();
        }
    }

    document.addEventListener("turbo:load", function () {
        checkAndAddButtons();
    });

    document.addEventListener("DOMContentLoaded", function () {
        checkAndAddButtons();
    });

    checkAndAddButtons();
})();