Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
2.6 kB
3
Indexable
function getUserProfileData() {
    var parentElement = document.getElementById('content');
    var elements = parentElement.querySelectorAll('a[href*="/profile/"], a[href*="/u/#/"]');
    var result = {};

    Array.from(elements).forEach(function(element) {
        var id = element.getAttribute('href').match(/\/(profile|u\/#)\/(\d+)(?:\/|$)/)?.[2];
        if (id) {
            result[id] = [...(result[id] || []), element];
        }
    });

    Object.values(result).forEach(function(elements) {
        elements.sort((a, b) => Array.from(a.parentElement.children).indexOf(a) - Array.from(b.parentElement.children).indexOf(b));
    });

    return result;
}

function getInfoAboutId(profileId) {
    const apiUrl = `https://klavogonki.ru/api/profile/get-summary?id=${profileId}`;
    return fetch(apiUrl)
        .then(response => {
            if (response.ok) {
                return response.json();
            }
            throw new Error('Network response was not ok.');
        })
        .then(data => {
            if (data && data.title) {
                return `Status Title for Profile ID ${profileId}: ${data.title}`;
            } else {
                throw new Error('Invalid data format received from the API.');
            }
        });
}

function getRankColor(statusTitle) {
    const statusColors = {
        'Новичок': '#8d8d8d',
        'Любитель': '#4f9a97',
        'Таксист': '#187818',
        'Профи': '#8c8100',
        'Гонщик': '#ba5800',
        'Маньяк': '#bc0143',
        'Супермен': '#5e0b9e',
        'Кибергонщик': '#2e32ce',
        'Экстракибер': '#061956'
    };

    return statusColors[statusTitle] || '#000000'; // Default to black color if status title not found
}

var foundElementsWithIDs = getUserProfileData();
Object.keys(foundElementsWithIDs).forEach(function(id) {
    getInfoAboutId(id)
        .then(info => {
            const statusTitle = info.split(':')[1].trim(); // Extract status title from the API response
            const hexColor = getRankColor(statusTitle); // Get hex color based on status title
            foundElementsWithIDs[id].forEach(function(element) {
                element.style.setProperty('color', hexColor, 'important'); // Apply color to the element with !important
            });
            console.log(info); // Log status title for the specific ID
        })
        .catch(error => {
            console.error(error);
        });
});