煎蛋用户名搜索

在煎蛋网页添加用户名搜索功能,支持自动翻页查找

  1. // ==UserScript==
  2. // @name 煎蛋用户名搜索
  3. // @name:en JandanUserSearch
  4. // @namespace https://github.com/cornradio/jandan-user-search
  5. // @version 1.0.4
  6. // @description 在煎蛋网页添加用户名搜索功能,支持自动翻页查找
  7. // @description:en Add username search function to jandan.net with auto-page-turning
  8. // @author 您的名字
  9. // @match https://jandan.net/*
  10. // @match https://i.jandan.net/*
  11. // @license MIT
  12. // @icon https://jandan.net/favicon.ico
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. // 创建一个容器来放置搜索框和按钮
  19. const container = document.createElement('div');
  20. container.style.position = 'fixed';
  21. container.style.top = '10px';
  22. container.style.right = '10px';
  23. container.style.zIndex = '9999';
  24. container.style.display = 'flex';
  25. container.style.gap = '5px';
  26. //max-width
  27.  
  28. // 搜索框
  29. const search = document.createElement('input');
  30. search.type = 'text';
  31. search.placeholder = '请输入用户名';
  32. search.style.padding = '8px 100px 8px 8px'; // 右侧留出更多空间给计数器和按钮
  33. search.style.border = '1px solid #ccc';
  34. search.style.borderRadius = '4px';
  35. search.style.backgroundColor = '#ffffff78';
  36. search.style.backdropFilter = 'blur(10px)';
  37. search.style.boxShadow = '0 2px 4px rgba(0,0,0,0.1)';
  38. search.style.position = 'relative';
  39. search.style.maxWidth = '80px';
  40.  
  41.  
  42. // 计数器
  43. const counter = document.createElement('span');
  44. counter.textContent = '0/0';
  45. counter.style.position = 'absolute';
  46. counter.style.right = '50px'; // 调整位置,为搜索按钮留出空间
  47. counter.style.top = '50%';
  48. counter.style.transform = 'translateY(-50%)';
  49. counter.style.color = '#666';
  50. counter.style.fontSize = '14px';
  51. counter.style.pointerEvents = 'none';
  52. counter.style.backgroundColor = 'transparent';
  53. counter.style.border = 'none';
  54. counter.style.padding = '0';
  55.  
  56. // 搜索按钮
  57. const searchBtn = document.createElement('button');
  58. searchBtn.innerHTML = '🔍';
  59. searchBtn.style.position = 'absolute';
  60. searchBtn.style.right = '8px';
  61. searchBtn.style.top = '50%';
  62. searchBtn.style.transform = 'translateY(-50%)';
  63. searchBtn.style.border = 'none';
  64. searchBtn.style.backgroundColor = 'transparent';
  65. searchBtn.style.color = '#666';
  66. searchBtn.style.cursor = 'pointer';
  67. searchBtn.style.fontSize = '16px';
  68. searchBtn.style.display = 'flex';
  69. searchBtn.style.alignItems = 'center';
  70. searchBtn.style.justifyContent = 'center';
  71. searchBtn.style.width = '32px';
  72. searchBtn.style.height = '32px';
  73. searchBtn.style.padding = '0';
  74. searchBtn.title = '搜索用户';
  75.  
  76. // 创建一个包装器来包含搜索框、计数器和按钮
  77. const searchWrapper = document.createElement('div');
  78. searchWrapper.style.position = 'relative';
  79. searchWrapper.style.display = 'inline-block';
  80. searchWrapper.appendChild(search);
  81. searchWrapper.appendChild(counter);
  82. searchWrapper.appendChild(searchBtn);
  83.  
  84. // 从localStorage加载保存的用户名
  85. const savedUsername = localStorage.getItem('monkey_jandan_username');
  86. if (savedUsername) {
  87. search.value = savedUsername;
  88. }
  89.  
  90. // 监听输入变化并保存到localStorage
  91. search.addEventListener('input', () => {
  92. localStorage.setItem('monkey_jandan_username', search.value);
  93. });
  94.  
  95. // 添加一个变量来跟踪当前匹配的索引
  96. let currentMatchIndex = -1;
  97. let currentMatches = [];
  98.  
  99. // 修改搜索功能
  100. async function searchUsername() {
  101. // 移除之前的高亮
  102. document.querySelectorAll('.highlight-author').forEach(el => {
  103. el.classList.remove('highlight-author');
  104. });
  105.  
  106. const username = search.value.trim();
  107. if (!username) {
  108. counter.textContent = '0/0';
  109. currentMatches = [];
  110. currentMatchIndex = -1;
  111. return;
  112. }
  113.  
  114. // 同时支持PC版和手机版的选择器
  115. const authors = document.querySelectorAll('.author, li[id^="comment-"] > b');
  116. currentMatches = Array.from(authors).filter(author =>
  117. author.textContent.toLowerCase().includes(username.toLowerCase())
  118. );
  119.  
  120. if (currentMatches.length > 0) {
  121. // 如果是新的搜索,重置索引
  122. if (currentMatchIndex === -1) {
  123. currentMatchIndex = 0;
  124. } else {
  125. // 移动到下一个匹配
  126. currentMatchIndex++;
  127. // 如果已经是最后一个匹配,则跳转到下一页
  128. if (currentMatchIndex >= currentMatches.length) {
  129. const nextPageLink = document.querySelector('.previous-comment-page');
  130. if (nextPageLink) {
  131. sessionStorage.setItem('searchUsername', username);
  132. sessionStorage.setItem('autoSearch', 'true');
  133. nextPageLink.click();
  134. return;
  135. } else {
  136. alert('已到最后一页,未找到更多结果');
  137. currentMatchIndex = currentMatches.length - 1;
  138. }
  139. }
  140. }
  141.  
  142. // 更新计数器显示当前位置/匹配总数
  143. counter.textContent = `${currentMatchIndex + 1}/${currentMatches.length}`;
  144.  
  145. // 高亮并滚动到当前匹配
  146. currentMatches.forEach((match, index) => {
  147. match.classList.add('highlight-author');
  148. if (index === currentMatchIndex) {
  149. match.scrollIntoView({ behavior: 'smooth', block: 'start' });
  150. }
  151. });
  152. } else {
  153. counter.textContent = '0/0';
  154. currentMatchIndex = -1;
  155. const nextPageLink = document.querySelector('.previous-comment-page');
  156. if (nextPageLink) {
  157. sessionStorage.setItem('searchUsername', username);
  158. sessionStorage.setItem('autoSearch', 'true');
  159. nextPageLink.click();
  160. } else {
  161. alert('已到最后一页,未找到该用户名');
  162. }
  163. }
  164. }
  165.  
  166. // 页面加载完成后检查是否需要自动搜索
  167. function checkAutoSearch() {
  168. const autoSearch = sessionStorage.getItem('autoSearch');
  169. const searchUsername = sessionStorage.getItem('searchUsername');
  170. if (autoSearch === 'true' && searchUsername) {
  171. // 清除自动搜索标记
  172. sessionStorage.removeItem('autoSearch');
  173. // 设置搜索框的值
  174. search.value = searchUsername;
  175. // 缩短延迟时间,加快搜索速度
  176. setTimeout(() => {
  177. searchUsername();
  178. }, 500);
  179. }
  180. }
  181.  
  182. // 添加高亮样式
  183. const style = document.createElement('style');
  184. style.textContent = `
  185. .highlight-author {
  186. background-color: yellow !important;
  187. padding: 2px 5px !important;
  188. border-radius: 3px !important;
  189. }
  190. `;
  191. document.head.appendChild(style);
  192.  
  193. // 添加事件监听
  194. searchBtn.addEventListener('click', searchUsername);
  195. search.addEventListener('keypress', (e) => {
  196. if (e.key === 'Enter') {
  197. searchUsername();
  198. }
  199. });
  200.  
  201. // 修改container样式
  202. container.style.alignItems = 'center';
  203.  
  204. // 修改元素添加顺序
  205. container.appendChild(searchWrapper);
  206. document.body.appendChild(container);
  207.  
  208. // 检查是否需要自动搜索
  209. checkAutoSearch();
  210. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址