Greasyfork 脚本显示评分

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

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

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