Grifftopia Points

Gain points by commenting on Grifftopia!

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Grifftopia Points
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Gain points by commenting on Grifftopia!
// @author       alboxer2000
// @match        https://scratch.mit.edu/users/*
// @grant        GM_xmlhttpRequest
// @connect      raw.githubusercontent.com
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    
    function getUsername() {
        const pathParts = window.location.pathname.split('/');
        
        return pathParts[2];
    }

    
    async function fetchAndDisplayPoints() {
        const targetUser = getUsername();
        if (!targetUser) return;

        const apiUrl = `https://raw.githubusercontent.com/alboxer2000/grifftopia-points/refs/heads/main/data.json`;

        GM_xmlhttpRequest({
            method: "GET",
            url: apiUrl,
            onload: function(response) {
                try {
                    const data = JSON.parse(response.responseText);
                    
                    let points = 0;
                    
                    
                    if (Array.isArray(data)) {
                        const userEntry = data.find(u => u.username && u.username.toLowerCase() === targetUser.toLowerCase());
                        points = userEntry ? userEntry.points : 0;
                    } else if (typeof data === 'object') {
                        const keys = Object.keys(data);
                        const match = keys.find(k => k.toLowerCase() === targetUser.toLowerCase());
                        points = match ? data[match] : 0;
                    }

                    injectPointsUI(points);
                } catch (e) {
                    console.error("Failed to parse points data:", e);
                }
            },
            onerror: function(err) {
                console.error("Error fetching points:", err);
            }
        });
    }

   
    function injectPointsUI(points) {
        
        if (document.getElementById('scratch-points-display')) return;

        const container = document.querySelector('.header-text h2') || document.querySelector('.header-text');
        if (!container) return;

        const pointsBadge = document.createElement('span');
        pointsBadge.id = 'scratch-points-display';
        pointsBadge.innerText = ` ${points} pts`;
        

        pointsBadge.style.fontSize = '0.65em';
        pointsBadge.style.color = '#4d97ff'; // Scratch Blue
        pointsBadge.style.backgroundColor = '#f2f2f2';
        pointsBadge.style.padding = '2px 8px';
        pointsBadge.style.borderRadius = '10px';
        pointsBadge.style.marginLeft = '10px';
        pointsBadge.style.verticalAlign = 'middle';
        pointsBadge.style.fontWeight = 'bold';
        pointsBadge.style.border = '1px solid #d9d9d9';
        pointsBadge.style.display = 'inline-block';

        container.appendChild(pointsBadge);
    }

    
    const observer = new MutationObserver((mutations) => {
        const container = document.querySelector('.header-text');
        if (container && !document.getElementById('scratch-points-display')) {
            fetchAndDisplayPoints();
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    
    if (document.querySelector('.header-text')) {
        fetchAndDisplayPoints();
    }

})();