cdwswb

为收藏的题目添加评分

  1. // ==UserScript==
  2. // @name cdwswb
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.0
  5. // @description 为收藏的题目添加评分
  6. // @author cww
  7. // @match https://codeforces.com/favourite/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
  9. // @grant GM_xmlhttpRequest
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. async function getUserFavorites() {
  17. let g = [];
  18. let table = document.querySelector('.problems');
  19. let rows = table.querySelectorAll('tr');
  20. for (let i = 1; i < rows.length; i++) {
  21. let row = rows[i];
  22.  
  23. var s = row.children[0].querySelector('a').textContent.replace(/\s+/g, '');
  24. var num = "";
  25. var c = "";
  26. var j = 0;
  27. while (j < s.length && s[j] >= '0' && s[j] <= '9') {
  28. j += 1;
  29. }
  30. num = s.substr(0, j);
  31. c = s.substr(j);
  32. g.push({ contestId: parseInt(num), index: c });
  33. }
  34. return g;
  35. }
  36.  
  37. async function getFilteredProblems(favoriteIds) {
  38. const url = "https://codeforces.com/api/problemset.problems";
  39.  
  40. try {
  41. const response = await fetch(url);
  42. const data = await response.json();
  43.  
  44. if (data.status === "OK") {
  45. const problems = data.result.problems;
  46. const problemMap = {};
  47. problems.forEach(problem => {
  48. problemMap[`${problem.contestId}${problem.index}`] = problem;
  49. });
  50.  
  51. return favoriteIds.map(favorite => {
  52. const problem = problemMap[`${favorite.contestId}${favorite.index}`];
  53. if (problem) {
  54. return { ...problem, rating: problem.rating || Infinity };
  55. }
  56. return { ...favorite, rating: Infinity };
  57. });
  58. } else {
  59. console.error("Error fetching problems:", data);
  60. }
  61. } catch (error) {
  62. console.error("Fetch error:", error);
  63. }
  64. return [];
  65. }
  66.  
  67. // 主程序
  68. getUserFavorites().then(favoriteIds => {
  69. getFilteredProblems(favoriteIds).then(filteredProblems => {
  70. console.log(filteredProblems);
  71. let table = document.querySelector('.problems');
  72. let rows = table.querySelectorAll('tr');
  73.  
  74. // 动态创建 <th> 和按钮
  75. const newTh = document.createElement('th');
  76. const sortButton = document.createElement('button');
  77. sortButton.textContent = 'ASC'; // 设置按钮文本
  78. sortButton.id = 'sortButton'; // 设置按钮 ID
  79. newTh.appendChild(sortButton);
  80. rows[0].insertBefore(newTh, rows[0].children[2]);
  81. let g = [];
  82. // 更新表格
  83. filteredProblems.forEach((problem, index) => {
  84. if (index + 1 < rows.length) {
  85. const rating = problem.rating;
  86. console.log(`题目名称: ${problem.name}, 比赛ID: ${problem.contestId}, 难度: ${rating}`);
  87.  
  88. let row = rows[index + 1];
  89. let newTd = document.createElement('td');
  90. if (index % 2 == 0) newTd.style.backgroundColor = "#F0F0F0";
  91.  
  92. // 根据 rating 设置字体颜色
  93. if (rating < 1200) {
  94. newTd.style.color = 'gray';
  95. } else if (rating < 1400) {
  96. newTd.style.color = 'green';
  97. } else if (rating < 1600) {
  98. newTd.style.color = 'cyan';
  99. } else if (rating < 1900) {
  100. newTd.style.color = 'blue';
  101. } else if (rating < 2100) {
  102. newTd.style.color = 'orange';
  103. } else if (rating < 2300) {
  104. newTd.style.color = '#FF8C00';
  105. } else if (rating < 2600) {
  106. newTd.style.color = 'lightcoral';
  107. } else if (rating < 3000) {
  108. newTd.style.color = 'red';
  109. } else {
  110. newTd.style.color = '#8B0000';
  111. }
  112. newTd.textContent = rating === Infinity ? '***' : rating;
  113. row.insertBefore(newTd, row.children[2]);
  114. g.push({ row : row, rating : rating });
  115. }
  116. });
  117. console.log(g);
  118. // 按钮点击事件逻辑
  119. let ascending = false; // 用于跟踪排序顺序
  120. sortButton.addEventListener('click', function() {
  121. let head = rows[0];
  122. console.log(head);
  123. // 对 g 数组按 rating 进行排序
  124. const sortedG = g.sort((a, b) => ascending ? a.rating - b.rating : b.rating - a.rating);
  125.  
  126. // 更新表格,保留表头
  127. const tbody = table.querySelector('tbody');
  128. // 清空 tbody 中的行,但保留表头
  129. tbody.innerHTML = '';
  130. tbody.appendChild(head);
  131. // 重新插入排序后的行
  132. sortedG.forEach(item => {
  133. tbody.appendChild(item.row);
  134. });
  135. console.log(g);
  136. ascending = !ascending; // 切换排序状态
  137. sortButton.textContent = ascending ? 'DESC' : 'AESC'; // 更新按钮文本
  138. });
  139. });
  140. })();
  141. })();

QingJ © 2025

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