Greasy Fork 支持简体中文。

Greasyfork 脚本显示评分

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

目前為 2024-07-19 提交的版本,檢視 最新版本

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