JanitorAI Token Filter

Filters character cards on JanitorAI by token count

当前为 2025-04-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name JanitorAI Token Filter
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.21
  5. // @description Filters character cards on JanitorAI by token count
  6. // @author Fefnik
  7. // @match https://janitorai.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. let MIN_TOKENS = 500;
  15. let sliderElement = null;
  16.  
  17. function parseTokens(tokenText) {
  18. try {
  19. let cleanText = tokenText.replace(/<!--[\s\S]*?-->/g, '').replace('tokens', '').trim();
  20. if (cleanText.includes('k')) {
  21. return parseFloat(cleanText.replace('k', '')) * 1000;
  22. }
  23. return parseInt(cleanText, 10) || 0;
  24. } catch (error) {
  25. return 0;
  26. }
  27. }
  28.  
  29. function filterCards() {
  30. const cards = document.querySelectorAll('.chakra-stack.css-1s5evre');
  31. cards.forEach(card => {
  32. const tokenElement = card.querySelector('.chakra-text.css-jccmq6');
  33. if (!tokenElement) return;
  34.  
  35. const tokenCount = parseTokens(tokenElement.textContent);
  36. const parentContainer = card.closest('.css-1sxhvxh');
  37. if (parentContainer) {
  38. parentContainer.style.display = tokenCount < MIN_TOKENS ? 'none' : '';
  39. }
  40. });
  41. }
  42.  
  43. function createOrUpdateSlider() {
  44. if (!sliderElement) {
  45. const sliderContainer = document.createElement('div');
  46. sliderContainer.id = 'token-filter-container';
  47. sliderContainer.style.position = 'fixed';
  48. sliderContainer.style.top = '50px';
  49. sliderContainer.style.left = '0px';
  50. sliderContainer.style.zIndex = '99999';
  51. sliderContainer.style.display = 'flex';
  52. sliderContainer.style.flexDirection = 'column';
  53. sliderContainer.style.alignItems = 'center';
  54. sliderContainer.style.height = '150px';
  55. sliderContainer.style.width = '50px';
  56.  
  57. const sliderWrapper = document.createElement('div');
  58. sliderWrapper.style.width = '100px';
  59. sliderWrapper.style.height = '10px';
  60. sliderWrapper.style.transform = 'rotate(-90deg)';
  61. sliderWrapper.style.transformOrigin = 'center center';
  62. sliderWrapper.style.marginTop = '10px';
  63.  
  64. sliderElement = document.createElement('input');
  65. sliderElement.type = 'range';
  66. sliderElement.id = 'token-filter-slider';
  67. sliderElement.min = '0';
  68. sliderElement.max = '6000';
  69. sliderElement.step = '100';
  70. sliderElement.value = MIN_TOKENS;
  71. sliderElement.style.width = '100%';
  72. sliderElement.style.height = '20px';
  73. sliderElement.style.backgroundColor = '#4a4a4a';
  74. sliderElement.style.cursor = 'pointer';
  75. sliderElement.style.appearance = 'none';
  76. sliderElement.style.outline = 'none';
  77. sliderElement.style.borderRadius = '5px';
  78.  
  79. const style = document.createElement('style');
  80. style.textContent = `
  81. #token-filter-slider::-webkit-slider-thumb {
  82. -webkit-appearance: none;
  83. appearance: none;
  84. width: 20px;
  85. height: 20px;
  86. background: #ffffff;
  87. cursor: pointer;
  88. border-radius: 50%;
  89. border: 2px solid #000;
  90. }
  91. #token-filter-slider::-moz-range-thumb {
  92. width: 20px;
  93. height: 20px;
  94. background: #ffffff;
  95. cursor: pointer;
  96. border-radius: 50%;
  97. border: 2px solid #000;
  98. }
  99. `;
  100. document.head.appendChild(style);
  101.  
  102. const label = document.createElement('span');
  103. label.id = 'token-filter-label';
  104. label.style.color = '#fff';
  105. label.style.fontSize = '12px';
  106. label.style.textAlign = 'center';
  107. label.style.marginTop = '50px';
  108. label.textContent = `${MIN_TOKENS} tokens`;
  109.  
  110. sliderElement.addEventListener('input', (e) => {
  111. MIN_TOKENS = parseInt(e.target.value);
  112. label.textContent = `${MIN_TOKENS} tokens`;
  113. filterCards();
  114. });
  115.  
  116. sliderWrapper.appendChild(sliderElement);
  117. sliderContainer.appendChild(sliderWrapper);
  118. sliderContainer.appendChild(label);
  119.  
  120. const appendSlider = () => {
  121. if (document.body) {
  122. document.body.appendChild(sliderContainer);
  123. } else {
  124. setTimeout(appendSlider, 500);
  125. }
  126. };
  127. appendSlider();
  128. }
  129.  
  130. const container = document.getElementById('token-filter-container');
  131. if (container) {
  132. container.style.display = window.location.pathname === '/' ? 'flex' : 'none';
  133. }
  134. }
  135.  
  136. function initialize() {
  137. createOrUpdateSlider();
  138. if (window.location.pathname === '/') {
  139. filterCards();
  140. }
  141. }
  142.  
  143. const tryInitialize = () => {
  144. if (document.body) {
  145. initialize();
  146.  
  147. let lastPath = window.location.pathname;
  148. const checkPath = () => {
  149. if (lastPath !== window.location.pathname) {
  150. lastPath = window.location.pathname;
  151. createOrUpdateSlider();
  152. if (lastPath === '/') {
  153. filterCards();
  154. }
  155. }
  156. };
  157.  
  158. setInterval(checkPath, 500);
  159.  
  160. const observer = new MutationObserver(() => {
  161. if (window.location.pathname === '/') {
  162. setTimeout(filterCards, 500);
  163. }
  164. });
  165. observer.observe(document.body, { childList: true, subtree: true });
  166. } else {
  167. setTimeout(tryInitialize, 1000);
  168. }
  169. };
  170.  
  171. tryInitialize();
  172. })();