Greasyfork 脚本显示评分

在每个脚本名称后面添加脚本评分

当前为 2024-06-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2.  
  3. // @name Greasyfork 脚本显示评分
  4.  
  5. // @namespace https://greasyfork.org/zh-CN/users/1169082-%E4%BA%BA%E6%B0%91%E7%9A%84%E5%8B%A4%E5%8A%A1%E5%91%98
  6.  
  7. // @version 1.3.1
  8.  
  9. // @description 在每个脚本名称后面添加脚本评分
  10.  
  11. // @author 人民的勤务员 <toniaiwanowskiskr47@gmail.com>
  12.  
  13. // @match https://greasyfork.org/*
  14. // @match https://sleazyfork.org/*
  15. // @license MIT
  16.  
  17.  
  18. // @grant none
  19.  
  20. // @icon https://www.google.com/s2/favicons?domain=greasyfork.org
  21.  
  22. // ==/UserScript==
  23.  
  24. (function() {
  25.  
  26. 'use strict';
  27.  
  28. // 检测脚本列表的出现,并自动插入评分
  29.  
  30. function observeScriptList() {
  31.  
  32. const scriptList = document.querySelector('.script-list');
  33.  
  34. if (scriptList) {
  35.  
  36. // 插入评分
  37.  
  38. insertRatings(scriptList);
  39.  
  40.  
  41.  
  42. // 配置观察器
  43.  
  44. const observer = new MutationObserver(function(mutationsList, observer) {
  45.  
  46. for (let mutation of mutationsList) {
  47.  
  48. if (mutation.type === 'childList') {
  49.  
  50. for (let node of mutation.addedNodes) {
  51.  
  52. // 检查是否为脚本列表项
  53.  
  54. if (node.nodeType === Node.ELEMENT_NODE && node.matches('li[data-script-id]')) {
  55.  
  56. insertRating(node);
  57.  
  58. }
  59.  
  60. }
  61.  
  62. }
  63.  
  64. }
  65.  
  66. });
  67.  
  68. // 开始观察脚本列表的变化
  69.  
  70. observer.observe(scriptList, { childList: true, subtree: true });
  71.  
  72. }
  73.  
  74. }
  75.  
  76. // 插入评分
  77.  
  78. function insertRating(scriptBlock) {
  79.  
  80. const ratingElement = scriptBlock.querySelector('dd.script-list-ratings');
  81.  
  82. if (ratingElement) {
  83.  
  84. const rating = ratingElement.getAttribute('data-rating-score');
  85.  
  86. const ratingDisplay = document.createElement('span');
  87.  
  88. ratingDisplay.textContent = rating.replace(/[^\d.]/g, '');
  89.  
  90. ratingDisplay.style.marginLeft = '30px';
  91.  
  92. ratingDisplay.style.fontSize = '1em';
  93.  
  94. ratingDisplay.style.color = '#ff8c00';
  95.  
  96. ratingDisplay.style.fontWeight = 'bold';
  97.  
  98. const titleElement = scriptBlock.querySelector('.script-link');
  99.  
  100. if (titleElement) {
  101.  
  102. titleElement.insertAdjacentElement('afterend', ratingDisplay);
  103.  
  104. }
  105.  
  106. }
  107.  
  108. }
  109.  
  110. // 插入评分到当前页面的脚本列表中
  111.  
  112. function insertRatings(scriptList) {
  113.  
  114. const scriptBlocks = scriptList.querySelectorAll('li[data-script-id]');
  115.  
  116. scriptBlocks.forEach(insertRating);
  117.  
  118. }
  119.  
  120. // 开始观察当前页面脚本列表的出现
  121.  
  122. observeScriptList();
  123.  
  124. // 观察 body 元素以检测页面加载了下一页
  125.  
  126. const bodyObserver = new MutationObserver(function(mutationsList, observer) {
  127.  
  128. mutationsList.forEach(mutation => {
  129.  
  130. mutation.addedNodes.forEach(node => {
  131.  
  132. if (node.nodeType === Node.ELEMENT_NODE && node.matches('.script-list')) {
  133.  
  134. // 页面加载了下一页,自动插入评分
  135.  
  136. insertRatings(node);
  137.  
  138. }
  139.  
  140. });
  141.  
  142. });
  143.  
  144. });
  145.  
  146. // 开始观察 body 元素的子节点变化
  147.  
  148. bodyObserver.observe(document.body, { childList: true, subtree: true });
  149.  
  150. })();
  151.