Steam评论时长过滤

动态过滤掉Steam评论页面上游玩时长小于指定小时数和指定字数的评论并显示直方图

  1. // ==UserScript==
  2. // @name Steam评论时长过滤
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.5
  5. // @description 动态过滤掉Steam评论页面上游玩时长小于指定小时数和指定字数的评论并显示直方图
  6. // @author akira0245, gpt4o
  7. // @match https://steamcommunity.com/app/*/reviews/*
  8. // @grant none
  9. // @require https://cdn.jsdelivr.net/npm/chart.js@4.4.3/dist/chart.umd.min.js
  10. // @license gplv3
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 定时检测滚动位置,并调用 CheckForMoreContent
  17. setInterval(function() {
  18. if(filterSwitch.checked)
  19. CheckForMoreContent();
  20. }, 100);
  21.  
  22. // 创建总容器
  23. const mainContainer = document.createElement('div');
  24. mainContainer.style.position = 'fixed';
  25. mainContainer.style.top = '10px';
  26. mainContainer.style.right = '10px';
  27. mainContainer.style.zIndex = '1000';
  28. mainContainer.style.backgroundColor = 'rgb(13 19 27)';
  29. mainContainer.style.padding = '10px';
  30. mainContainer.style.border = '1px solid black';
  31. mainContainer.style.borderRadius = '10px';
  32. // mainContainer.style.display = 'flex';
  33. mainContainer.style.flexFlow = 'column';
  34. mainContainer.style.gap = '5px'; // 增加滑块之间的间距
  35.  
  36.  
  37. // 创建一个简易开关
  38. const filterSwitchContainer = document.createElement('div');
  39. filterSwitchContainer.style.display = 'flex';
  40. // filterSwitchContainer.style.alignItems = 'center'; // 垂直居中对齐
  41. // filterSwitchContainer.style.gap = '10px'; // 增加复选框与标签之间的间隔
  42.  
  43. const filterSwitch = document.createElement('input');
  44. filterSwitch.type = 'checkbox';
  45. filterSwitch.checked = true; // 初始状态为启用
  46. filterSwitchContainer.appendChild(filterSwitch);
  47.  
  48. const switchLabel = document.createElement('label');
  49. switchLabel.textContent = '自动加载';
  50. filterSwitchContainer.appendChild(switchLabel);
  51.  
  52. mainContainer.appendChild(filterSwitchContainer);
  53.  
  54.  
  55. ///////////////////////////////////////////////////////////////////////////////
  56.  
  57. // 创建滑块容器
  58. const sliderContainer = document.createElement('div');
  59.  
  60. const sliderLabel = document.createElement('label');
  61. sliderLabel.textContent = '过滤时间 (小时):';
  62. sliderLabel.style.display = 'block';
  63. sliderContainer.appendChild(sliderLabel);
  64.  
  65. // 创建对数比例滑块
  66. const slider = document.createElement('input');
  67. slider.type = 'range';
  68. slider.min = '0';
  69. slider.max = '4'; // 对数比例范围
  70. slider.step = '0.01';
  71. slider.value = '2'; // 初始值为100小时对应的对数值log10(100) = 2
  72. slider.style.width = '200px';
  73. sliderContainer.appendChild(slider);
  74.  
  75. const sliderValue = document.createElement('span');
  76. const hoursFromLog = logarithmicToLinear(slider.value);
  77. sliderValue.textContent = hoursFromLog.toFixed(2);
  78. sliderValue.style.marginLeft = '10px';
  79. sliderContainer.appendChild(sliderValue);
  80.  
  81. // 添加刻度显示
  82. const scaleContainer = document.createElement('div');
  83. scaleContainer.style.display = 'flex';
  84. scaleContainer.style.justifyContent = 'space-between';
  85. scaleContainer.style.marginTop = '5px';
  86.  
  87. const scales = [1, 10, 100, 1000, 10000];
  88. scales.forEach(scale => {
  89. const scaleLabel = document.createElement('span');
  90. scaleLabel.textContent = scale;
  91. scaleContainer.appendChild(scaleLabel);
  92. });
  93. sliderContainer.appendChild(scaleContainer);
  94.  
  95. ///////////////////////////////////////////////////////////////////////////
  96.  
  97. // 创建字数滑块容器
  98. const wordCountSliderContainer = document.createElement('div');
  99.  
  100. const wordCountSliderLabel = document.createElement('label');
  101. wordCountSliderLabel.textContent = '过滤评论字数:';
  102. wordCountSliderLabel.style.display = 'block';
  103. wordCountSliderContainer.appendChild(wordCountSliderLabel);
  104.  
  105. // 创建字数滑块
  106. const wordCountSlider = document.createElement('input');
  107. wordCountSlider.type = 'range';
  108. wordCountSlider.min = '0';
  109. wordCountSlider.max = '4'; // 对数比例范围,同样适用
  110. wordCountSlider.step = '0.01';
  111. wordCountSlider.value = '2'; // 初始值
  112. wordCountSlider.style.width = '200px';
  113. wordCountSliderContainer.appendChild(wordCountSlider);
  114.  
  115. const wordCountSliderValue = document.createElement('span');
  116. const wordsFromLog = logarithmicToLinear(wordCountSlider.value);
  117. wordCountSliderValue.textContent = wordsFromLog.toFixed(0);
  118. wordCountSliderValue.style.marginLeft = '10px';
  119. wordCountSliderContainer.appendChild(wordCountSliderValue);
  120.  
  121. // 添加字数刻度显示
  122. const wordCountScaleContainer = document.createElement('div');
  123. wordCountScaleContainer.style.display = 'flex';
  124. wordCountScaleContainer.style.justifyContent = 'space-between';
  125. wordCountScaleContainer.style.marginTop = '5px';
  126.  
  127. const wordCountScales = [1, 10, 100, 1000, 10000];
  128. wordCountScales.forEach(scale => {
  129. const scaleLabel = document.createElement('span');
  130. scaleLabel.textContent = scale;
  131. wordCountScaleContainer.appendChild(scaleLabel);
  132. });
  133. wordCountSliderContainer.appendChild(wordCountScaleContainer);
  134.  
  135. //////////////////////////////////////////////////////////////////////////////
  136.  
  137. // 将滑块容器添加到总容器
  138. mainContainer.appendChild(sliderContainer);
  139. mainContainer.appendChild(wordCountSliderContainer);
  140.  
  141. // 将总容器添加到文档主体
  142. document.body.appendChild(mainContainer);
  143.  
  144. // 更新滑块值和应用筛选
  145. slider.addEventListener('input', () => {
  146. const hours = logarithmicToLinear(slider.value);
  147. sliderValue.textContent = hours.toFixed(2);
  148. applyFilter(logarithmicToLinear(slider.value), logarithmicToLinear(wordCountSlider.value));
  149. });
  150.  
  151. // 更新字数滑块值和应用筛选
  152. wordCountSlider.addEventListener('input', () => {
  153. const words = logarithmicToLinear(wordCountSlider.value);
  154. wordCountSliderValue.textContent = words.toFixed(0);
  155. applyFilter(logarithmicToLinear(slider.value), logarithmicToLinear(wordCountSlider.value));
  156. });
  157.  
  158. // 创建用于直方图的画布
  159. const histogramCanvas = document.createElement('canvas');
  160. histogramCanvas.style.marginTop = '20px';
  161. mainContainer.appendChild(histogramCanvas);
  162.  
  163. // 数据和配置初始化
  164. let histogramChart;
  165. // 初始化图表
  166. function initializeHistogram() {
  167. const context = histogramCanvas.getContext('2d');
  168. histogramCanvas.height = 300; // 设置图表高度为500px
  169. histogramChart = new Chart(context, {
  170. type: 'line', // 使用折线图来显示连续的图表
  171. data: {
  172. labels: [],
  173. datasets: [{
  174. label: '评论数量',
  175. data: [],
  176. backgroundColor: 'rgba(54, 162, 235, 0.2)',
  177. borderColor: 'rgba(54, 162, 235, 1)',
  178. borderWidth: 1,
  179. fill: true // 填充图表下方区域
  180. }]
  181. },
  182. options: {
  183. scales: {
  184. x: {
  185. title: {
  186. display: true,
  187. text: '游玩时长 (小时)'
  188. }
  189. },
  190. y: {
  191. title: {
  192. display: true,
  193. text: '评论数量'
  194. },
  195. type: 'logarithmic',
  196. min: 0,
  197. ticks: {
  198. callback: function(value) {
  199. if (Number.isInteger(value)) {
  200. return value; // 仅显示整数刻度
  201. }
  202. return null; // 跳过小数刻度
  203. }
  204. }
  205. }
  206. },
  207. elements: {
  208. line: {
  209. tension: 0.4 // 设置曲线张力,使线条更平滑
  210. }
  211. }
  212. }
  213. });
  214. }
  215.  
  216. // 更新数据并刷新图表
  217. function updateHistogram(hoursArray) {
  218. // 设置直方图的区间和范围
  219. const binCount = 50; // 将游玩时长分为50个区间
  220. const minHours = Math.min(...hoursArray);
  221. const maxHours = Math.max(...hoursArray);
  222. const binSize = (maxHours - minHours) / binCount;
  223.  
  224. // 初始化直方图数据
  225. const histogramData = new Array(binCount).fill(0);
  226.  
  227. // 填充直方图数据
  228. hoursArray.forEach(hours => {
  229. const binIndex = Math.floor((hours - minHours) / binSize);
  230. histogramData[Math.min(binIndex, binCount - 1)]++;
  231. });
  232.  
  233. // 更新图表数据
  234. histogramChart.data.labels = histogramData.map((_, i) => {
  235. const binStart = Math.floor(minHours + i * binSize);
  236. const binEnd = Math.floor(minHours + (i + 1) * binSize);
  237. return `${binStart} - ${binEnd}`;
  238. });
  239. histogramChart.data.datasets[0].data = histogramData;
  240. histogramChart.update();
  241. }
  242.  
  243.  
  244.  
  245. // 对数比例转换为线性值
  246. function logarithmicToLinear(value) {
  247. return Math.pow(10, parseFloat(value));
  248. }
  249.  
  250. // 检查并标记不符合条件的评论,同时更新直方图数据
  251. function checkAndMarkReviews() {
  252. const hoursArray = [];
  253. document.querySelectorAll('.apphub_Card').forEach(reviewCard => {
  254. const hoursElement = reviewCard.querySelector('.hours');
  255. if (hoursElement) {
  256. const hoursText = hoursElement.textContent.trim();
  257. const match = hoursText.match(/总时数\s*([\d,]+(\.\d+)?)\s*小时/); // 增加对逗号的匹配
  258. if (match) {
  259. const hours = parseFloat(match[1].replace(/,/g, '')); // 移除逗号并解析为浮点数
  260. hoursElement.setAttribute('data-hours', hours);
  261. hoursArray.push(hours);
  262. }
  263. }
  264. });
  265. if (histogramChart) {
  266. updateHistogram(hoursArray);
  267. }
  268. }
  269.  
  270.  
  271.  
  272.  
  273. // 修改applyFilter函数,增加字数过滤
  274. function applyFilter(timeThreshold, wordCountThreshold) {
  275. document.querySelectorAll('.apphub_Card').forEach(reviewCard => {
  276. const hoursElement = reviewCard.querySelector('.hours');
  277. const reviewTextElement = reviewCard.querySelector('.apphub_CardTextContent');
  278. if (hoursElement && reviewTextElement) {
  279. const hours = parseFloat(hoursElement.getAttribute('data-hours'));
  280. const reviewText = Array.from(reviewTextElement.childNodes).slice(2).map(el => el.textContent).join(" ");
  281. const wordCount = reviewText.split(/\s+/).length;
  282.  
  283. if (hours < timeThreshold || wordCount < wordCountThreshold) {
  284. reviewCard.style.display = 'none';
  285. } else {
  286. reviewCard.style.display = '';
  287. }
  288. }
  289. });
  290. }
  291.  
  292.  
  293.  
  294. // 使用本地存储保存滑块值
  295. function saveSliderValues() {
  296. localStorage.setItem('timeSliderValue', slider.value);
  297. localStorage.setItem('wordCountSliderValue', wordCountSlider.value);
  298. }
  299.  
  300.  
  301. function loadSliderValues() {
  302. const savedTimeSliderValue = localStorage.getItem('timeSliderValue');
  303. if (savedTimeSliderValue !== null) {
  304. slider.value = savedTimeSliderValue;
  305. sliderValue.textContent = logarithmicToLinear(savedTimeSliderValue).toFixed(2);
  306. }
  307.  
  308. const savedWordCountSliderValue = localStorage.getItem('wordCountSliderValue');
  309. if (savedWordCountSliderValue !== null) {
  310. wordCountSlider.value = savedWordCountSliderValue;
  311. wordCountSliderValue.textContent = logarithmicToLinear(savedWordCountSliderValue).toFixed(2);
  312. }
  313. }
  314.  
  315. // 监听滑块变化并保存
  316. slider.addEventListener('change', saveSliderValues);
  317. wordCountSlider.addEventListener('change', saveSliderValues);
  318.  
  319. // 页面初次加载时加载保存的滑块值
  320. loadSliderValues();
  321. applyFilter(logarithmicToLinear(slider.value), logarithmicToLinear(wordCountSlider.value));
  322.  
  323.  
  324. // 初始化图表
  325. initializeHistogram();
  326. checkAndMarkReviews(); // 确保在图表初始化后立即更新直方图
  327.  
  328. // 创建一个MutationObserver来监听动态添加的新评论
  329. const observer = new MutationObserver(() => {
  330. checkAndMarkReviews();
  331. applyFilter(logarithmicToLinear(slider.value), logarithmicToLinear(wordCountSlider.value));
  332. });
  333. observer.observe(document.querySelector('#AppHubCards'), { childList: true, subtree: true });
  334.  
  335. })();

QingJ © 2025

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