astars.club Auto Helper

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

目前為 2025-01-20 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name astars.club Auto Helper
  3. // @namespace astars.club
  4. // @version 2.1
  5. // @description хелпер который помогает определить популярность карты на сайте astars.club
  6. // @author astars lover
  7. // @match https://astars.club/*
  8. // @license MIT
  9. // @grant none
  10.  
  11.  
  12. // ==/UserScript==
  13.  
  14. const DELAY = 500; // Задержка между запросами в миллисекундах (по умолчанию 0,5 секунды)
  15.  
  16. const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
  17.  
  18. async function updateCardInfo(cardId, element) {
  19. if (!cardId || !element) {
  20. console.log(cardId, 'updateCardInfo error');
  21. return;
  22. }
  23.  
  24. try {
  25. console.log(`Обработка карточки с ID: ${cardId}`);
  26.  
  27. await sleep(DELAY);
  28.  
  29. // Получение количества "Желающих"
  30. const needResponse = await fetch(`https://astars.club/cards/${cardId}/users/need/`);
  31. if (needResponse.status === 502) {
  32. console.error("Ошибка 502: Остановка выполнения скриптов.");
  33. throw new Error("502 Bad Gateway");
  34. }
  35.  
  36. let needCount = 0;
  37. if (needResponse.ok) {
  38. const needHtml = await needResponse.text();
  39. const needDoc = new DOMParser().parseFromString(needHtml, 'text/html');
  40. needCount = needDoc.querySelectorAll('.profile__friends-item').length;
  41.  
  42. const pagination = needDoc.querySelector('.pagination__pages');
  43. if (pagination) {
  44. const lastPageLink = pagination.querySelector('a:last-of-type');
  45. const totalPages = lastPageLink ? parseInt(lastPageLink.innerText, 10) : 1;
  46. if (totalPages > 1) {
  47. needCount += (totalPages - 1) * 50;
  48. }
  49. }
  50. }
  51.  
  52. await sleep(DELAY);
  53.  
  54. // Получение количества "Готовых поменять"
  55. const tradeResponse = await fetch(`https://astars.club/cards/${cardId}/users/trade/`);
  56. if (tradeResponse.status === 502) {
  57. console.error("Ошибка 502: Остановка выполнения скриптов.");
  58. throw new Error("502 Bad Gateway");
  59. }
  60.  
  61. let tradeCount = 0;
  62. if (tradeResponse.ok) {
  63. const tradeHtml = await tradeResponse.text();
  64. const tradeDoc = new DOMParser().parseFromString(tradeHtml, 'text/html');
  65. tradeCount = tradeDoc.querySelectorAll('.profile__friends-item').length;
  66.  
  67. const pagination = tradeDoc.querySelector('.pagination__pages');
  68. if (pagination) {
  69. const lastPageLink = pagination.querySelector('a:last-of-type');
  70. const totalPages = lastPageLink ? parseInt(lastPageLink.innerText, 10) : 1;
  71. if (totalPages > 1 && tradeCount >= 50) {
  72. tradeCount += (totalPages - 1) * 50;
  73. }
  74. }
  75. }
  76.  
  77. await sleep(DELAY);
  78.  
  79. // Получение популярности и ранга
  80. const popularityResponse = await fetch(`https://astars.club/cards/${cardId}/users/`);
  81. if (popularityResponse.status === 502) {
  82. console.error("Ошибка 502: Остановка выполнения скриптов.");
  83. throw new Error("502 Bad Gateway");
  84. }
  85.  
  86. let popularityCount = 0;
  87. let rankText = '';
  88. if (popularityResponse.ok) {
  89. const popularityHtml = await popularityResponse.text();
  90. const popularityDoc = new DOMParser().parseFromString(popularityHtml, 'text/html');
  91. popularityCount = popularityDoc.querySelectorAll('.card-show__owner').length;
  92.  
  93. const pagination = popularityDoc.querySelector('.pagination__pages');
  94. if (pagination) {
  95. const lastPageLink = pagination.querySelector('a:last-of-type');
  96. const totalPages = lastPageLink ? parseInt(lastPageLink.innerText, 10) : 1;
  97. if (totalPages > 1) {
  98. popularityCount += (totalPages - 1) * 35;
  99. }
  100. }
  101.  
  102. const rankElement = popularityDoc.querySelector('.anime-cards__rank');
  103. if (rankElement) {
  104. rankText = rankElement.textContent.trim();
  105. }
  106. }
  107.  
  108. // Очистка старой информации
  109. element.querySelector('.link-icon')?.remove();
  110.  
  111. // Добавление новой информации
  112. const icon = document.createElement('div');
  113. icon.className = 'link-icon';
  114. icon.style.position = 'absolute';
  115. icon.style.top = '10px';
  116. icon.style.right = '10px';
  117. icon.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
  118. icon.style.color = 'white';
  119. icon.style.padding = '5px';
  120. icon.style.borderRadius = '5px';
  121. icon.style.fontSize = '12px';
  122. icon.innerHTML = `Ранг: ${rankText}<br>Уже имеют: ${popularityCount}<br>Хотят получить: ${needCount}<br>Готовы поменять: ${tradeCount}`;
  123.  
  124. element.style.position = 'relative';
  125. element.appendChild(icon);
  126.  
  127. console.log(cardId, '- ok');
  128.  
  129. } catch (error) {
  130. console.error(`Ошибка обработки карты ${cardId}:`, error);
  131. // Остановка выполнения скриптов
  132. throw error;
  133. }
  134. }
  135.  
  136. async function processCards() {
  137. const cards = document.querySelectorAll('.lootbox__card, .anime-cards__item, .trade__inventory-item, .trade__main-item');
  138.  
  139. console.log(cards.length, 'cards found');
  140.  
  141. for (const card of cards) {
  142. let cardId = card.getAttribute('data-id') || card.getAttribute('card-id');
  143. const href = card.getAttribute('href');
  144.  
  145. if (href) {
  146. let cardIdMatch = href.match(/\/cards\/(\d+)\/users\//);
  147. console.log(cardIdMatch);
  148. if (cardIdMatch) {
  149. cardId = cardIdMatch[1];
  150. }
  151. }
  152. if (cardId) {
  153. await updateCardInfo(cardId, card).catch(error => {
  154. console.error("Остановка из-за критической ошибки:", error.message);
  155. return;
  156. });
  157. } else {
  158. console.log(cardId, 'cardId not found');
  159. console.log(card);
  160. }
  161. }
  162. }
  163.  
  164. function addUpdateButton() {
  165. if (!document.querySelector('#fetchLinksButton')) {
  166. const button = document.createElement('button');
  167. button.id = 'fetchLinksButton';
  168. button.innerText = 'Обновить инфу';
  169. button.style.position = 'fixed';
  170. button.style.top = '37%';
  171. button.style.right = '-50px';
  172. button.style.zIndex = '1000';
  173. button.style.backgroundColor = '#007bff';
  174. button.style.color = '#fff';
  175. button.style.border = 'none';
  176. button.style.borderRadius = '5px';
  177. button.style.padding = '10px 15px';
  178. button.style.cursor = 'pointer';
  179. button.style.boxShadow = '0 2px 5px rgba(0, 0, 0, 0.2)';
  180. button.style.transform = 'rotate(90deg)';
  181. button.addEventListener('click', processCards);
  182. document.body.appendChild(button);
  183. }
  184. }
  185.  
  186. function clearIcons() {
  187. $('.card-notification')?.first()?.click();
  188. }
  189.  
  190. (function() {
  191. 'use strict';
  192.  
  193. setInterval(clearIcons, 2000);
  194. addUpdateButton();
  195. })();