Untitled
unknown
javascript
2 years ago
4.4 kB
6
Indexable
// ==UserScript==
// @name KG_Rank_Colorizer
// @namespace https://klavogonki.ru
// @version 0.1
// @description Nickname colorizer
// @author Patcher
// @match http*://klavogonki.ru/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=klavogonki.ru
// @grant none
// ==/UserScript==
(function() {
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
}
function processElementsWithColors(elementsWithIDs) {
Object.keys(elementsWithIDs).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
elementsWithIDs[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);
});
});
}
function waitForElements(parentSelector, targetSelectors, delay = 50, tries = 300) {
var triesCount = 0;
function checkElements() {
var parentElement = document.querySelector(parentSelector);
if (parentElement) {
var elements = Array.from(parentElement.querySelectorAll(targetSelectors));
if (elements.length > 0) {
if (elements.length === triesCount) {
// All elements are loaded, stop checking
var foundElementsWithIDs = getUserProfileData();
processElementsWithColors(foundElementsWithIDs);
return;
} else {
// Update the number of elements found
triesCount = elements.length;
}
}
}
if (triesCount < tries) {
// Continue checking if not all elements are loaded and the number of tries is not exceeded
setTimeout(checkElements, delay);
} else {
// Stop checking after the specified number of tries
console.log('Stopped checking for elements. Maximum tries reached.');
}
}
// Start checking for elements
checkElements();
}
// Call the waitForElements function with appropriate selectors and delay
waitForElements('#content', 'a[href*="/profile/"], a[href*="/u/#/"]');
})();Editor is loading...