astars.club Auto Helper

хелпер который помогает определить популярность карты на сайте astars.club

当前为 2025-01-23 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         astars.club Auto Helper
// @namespace    astars.club
// @version      2.6
// @description  хелпер который помогает определить популярность карты на сайте astars.club
// @author       astars lover
// @match        https://astars.club/*
// @license MIT
// @grant        none

// ==/UserScript==

const DELAY = 500; // Задержка между запросами в миллисекундах (по умолчанию 0,5 секунды) не менять чтоб не делать избыточную нагрузку на сайт

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

async function getCount(cardId, type) {
    let count = 0;
    let needResponse = await fetch(`https://astars.club/cards/${cardId}/users/${type}/`);
    if (needResponse.status === 502) {
        console.error("Ошибка 502: Остановка выполнения скриптов.");
        throw new Error("502 Bad Gateway");
    }
    let needHtml = '';
    let needDoc = '';
    if (needResponse.ok) {
        needHtml = await needResponse.text();
        needDoc = new DOMParser().parseFromString(needHtml, 'text/html');
        count = needDoc.querySelectorAll('.profile__friends-item').length;
    } else {
        return count;
    }

    const pagination = needDoc.querySelector('.pagination__pages');
    if (pagination && count >= 50) {
        const lastPageNum = pagination.querySelector('a:last-of-type');
        const totalPages = lastPageNum ? parseInt(lastPageNum.innerText, 10) : 1;
        if (totalPages > 1) {
            count = (totalPages - 1) * 50;
        }
        needResponse = await fetch(`https://astars.club/cards/${cardId}/users/${type}/page/${totalPages}`);
        if (needResponse.status === 502) {
            console.error("Ошибка 502: Остановка выполнения скриптов.");
            throw new Error("502 Bad Gateway");
        }
        if (needResponse.ok) {
            needHtml = await needResponse.text();
            needDoc = new DOMParser().parseFromString(needHtml, 'text/html');
            count += needDoc.querySelectorAll('.profile__friends-item').length;
        }
    }

    return count;
}

async function updateCardInfo(cardId, element) {
    if (!cardId || !element) {
        console.log(cardId, 'updateCardInfo error');
        return;
    }

    try {
        console.log(`Обработка карточки с ID: ${cardId}`);

        await sleep(DELAY);

        // Получение количества "Желающих"
        let needCount = await getCount(cardId, 'need');

        await sleep(DELAY);

        // Получение количества "Готовых поменять"
        let tradeCount = await getCount(cardId, 'trade');

        await sleep(DELAY);

        // Получение популярности и ранга
        const popularityResponse = await fetch(`https://astars.club/cards/${cardId}/users/`);
        if (popularityResponse.status === 502) {
            console.error("Ошибка 502: Остановка выполнения скриптов.");
            throw new Error("502 Bad Gateway");
        }

        let popularityCount = 0;
        let rankText = '';
        if (popularityResponse.ok) {
            const popularityHtml = await popularityResponse.text();
            const popularityDoc = new DOMParser().parseFromString(popularityHtml, 'text/html');

            const rankElement = popularityDoc.querySelector('.anime-cards__rank');
            if (rankElement) {
                rankText = rankElement.textContent.trim();
            }

            popularityCount = popularityDoc.querySelectorAll('.card-show__owner').length;

            const pagination = popularityDoc.querySelector('.pagination__pages');
            if (pagination) {
                const lastPageNum = pagination.querySelector('a:last-of-type');
                const totalPages = lastPageNum ? parseInt(lastPageNum.innerText, 10) : 1;
                if (totalPages > 1 && popularityCount >= 35) {
                    popularityCount = (totalPages - 1) * 35;
                    const needResponse = await fetch(`https://astars.club/cards/${cardId}/users/page/${lastPageNum}`);
                    if (needResponse.status === 502) {
                        console.error("Ошибка 502: Остановка выполнения скриптов.");
                        throw new Error("502 Bad Gateway");
                    }
                    if (needResponse.ok) {
                        popularityCount += (new DOMParser().parseFromString(await needResponse.text(), 'text/html')).querySelectorAll('.card-show__owner').length;
                    }
                }
            }
        }

        // Очистка старой информации
        element.querySelector('.link-icon')?.remove();

        // Добавление новой информации
        const icon = document.createElement('div');
        icon.className = 'link-icon';
        icon.style.position = 'absolute';
        icon.style.top = '10px';
        icon.style.right = '10px';
        icon.style.backgroundColor = 'rgba(0, 0, 0, 0.4)';
        icon.style.color = '#05ed5b';
        icon.style.padding = '5px';
        icon.style.borderRadius = '5px';
        icon.style.fontSize = '8px';
        icon.innerHTML = `Ранг: ${rankText}<br>Уже имеют: ${popularityCount}<br>Хотят получить: ${needCount}<br>Готовы поменять: ${tradeCount}`;

        element.style.position = 'relative';
        element.appendChild(icon);

        console.log(cardId, '- ok');

    } catch (error) {
        console.error(`Ошибка обработки карты ${cardId}:`, error);
        // Остановка выполнения скриптов
        throw error;
    }
}

function removeAllLinkIcons() {
    const linkIcons = document.querySelectorAll('.link-icon');
    linkIcons.forEach(icon => icon.remove());
}

async function processCards() {
    removeAllLinkIcons();
    
    const cards = document.querySelectorAll('.lootbox__card, .anime-cards__item, .trade__inventory-item, .trade__main-item');

    console.log(cards.length, 'cards found');

    for (const card of cards) {

        if (card.classList.contains('trade__inventory-item--lock')) {
            console.log('Пропускаем карту с классом trade__inventory-item--lock');
            continue; // Пропускаем эту карту
        }

        let cardId = card.getAttribute('card-id') || card.getAttribute('data-card-id') || card.getAttribute('data-id');
        const href = card.getAttribute('href');

        if (href) {
            let cardIdMatch = href.match(/\/cards\/(\d+)\/users\//);
            console.log(cardIdMatch);
            if (cardIdMatch) {
                cardId = cardIdMatch[1];
            }
        }
        if (cardId) {
            await updateCardInfo(cardId, card).catch(error => {
                console.error("Остановка из-за критической ошибки:", error.message);
                return;
            });
        } else {
            console.log(cardId, 'cardId not found');
            console.log(card);
        }
    }
}

function addUpdateButton() {
    if (!document.querySelector('#fetchLinksButton')) {
        const button = document.createElement('button');
        button.id = 'fetchLinksButton';
        button.innerText = 'Обновить инфу';
        button.style.position = 'fixed';
        button.style.top = '37%';
        button.style.right = '-50px';
        button.style.zIndex = '1000';
        button.style.backgroundColor = '#007bff';
        button.style.color = '#fff';
        button.style.border = 'none';
        button.style.borderRadius = '5px';
        button.style.padding = '10px 15px';
        button.style.cursor = 'pointer';
        button.style.boxShadow = '0 2px 5px rgba(0, 0, 0, 0.2)';
        button.style.transform = 'rotate(90deg)';
        button.addEventListener('click', processCards);
        document.body.appendChild(button);
    }
}

function clearIcons() {
    $('.card-notification')?.first()?.click();
}

function startPing() {
    setInterval(() => {
        // Получаем значение из глобальной переменной
        const userHash = window.dle_login_hash;

        if (!userHash) {
            console.error("Переменная dle_login_hash не определена.");
            return;
        }

        // Определяем текущий домен
        const currentDomain = window.location.origin;

        // Формируем URL с учетом userHash
        const url = `${currentDomain}/engine/ajax/controller.php?mod=user_count_timer&user_hash=${userHash}`;

        // Выполняем GET-запрос
        fetch(url)
            .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
            }
            return response.json(); // Если ответ в формате JSON
        })
            .then(data => {
            console.log("Данные получены:", data); // Обрабатываем полученные данные
        })
            .catch(error => {
            console.error("Ошибка при выполнении запроса:", error);
        });
    }, 10000); // Интервал проверки — 10 сек
}

function checkCard() {
    setInterval(() => {
        // Получаем значение из глобальной переменной
        const userHash = window.dle_login_hash;

        if (!userHash) {
            console.error("Переменная dle_login_hash не определена.");
            return;
        }

        // Определяем текущий домен
        const currentDomain = window.location.origin;

        // Формируем URL с учетом userHash
        const url = `${currentDomain}/engine/ajax/controller.php?mod=reward_card&action=check_reward&user_hash=${userHash}`;
                // Выполняем GET-запрос
        fetch(url)
            .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
            }
            return response.json(); // Если ответ в формате JSON
        })
        .then(data => {
            if (!data.cards || !data.cards.owner_id) {
                return;
            }
            const ownerId = data.cards.owner_id;
            console.log("owner_id получен:", ownerId); // Выводим owner_id

            const url = `${currentDomain}/engine/ajax/controller.php?mod=cards_ajax`;

            // Подготавливаем параметры запроса
            const postData = new URLSearchParams({
                action: "take_card",
                owner_id: ownerId
            });

            // Выполняем POST-запрос
            fetch(url, {
                method: "POST",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                body: postData.toString() // Передаём параметры в виде строки
            })
            .then(response => {
                if (!response.ok) {
                    throw new Error(`HTTP error! Status: ${response.status}`);
                }
                return response.json(); // Если ответ в формате JSON
            })
            .then(data => {
                console.log("Данные получены:", data); // Обрабатываем полученные данные
            })
            .catch(error => {
                console.error("Ошибка при выполнении запроса:", error);
            });
        })
        .catch(error => {
            console.error("Ошибка при выполнении запроса:", error);
        });

    }, 10000); // Интервал проверки — 10 сек
}

(function() {
    'use strict';

    setInterval(clearIcons, 2000);
    startPing();
    checkCard();

    addUpdateButton();
})();