osuMapFilter

filter osu maps

当前为 2022-01-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name osuMapFilter
  3. // @namespace https://greasyfork.org/users/110545
  4. // @version 0.4
  5. // @description filter osu maps
  6. // @author x94fujo6
  7. // @match https://osu.ppy.sh/beatmapsets
  8. // @match https://osu.ppy.sh/beatmapsets?*
  9. // @grant none
  10. // ==/UserScript==
  11. /* jshint esversion: 9 */
  12. /*
  13. you need map list to make this work!!!
  14. https://github.com/x94fujo6rpg/osuMapFilter
  15. */
  16.  
  17. (function () {
  18. 'use strict';
  19. let map_list = [];
  20. let stop = false;
  21. let debug_msg = false;
  22. let mode = 2; // 1:array 2:hash 3:set, https://jsbench.me/zfknghmteu/2
  23. let tester;
  24. switch (mode) {
  25. case 1:
  26. tester = (id) => { return map_list.includes(id); };
  27. break;
  28. case 2:
  29. tester = (id) => { return map_list[id]; };
  30. break;
  31. case 3:
  32. tester = (id) => { return map_list.has(id); };
  33. break;
  34. default:
  35. tester = false;
  36. break;
  37. }
  38. if (!tester) return console.log("tester not set");
  39. window.onload = main();
  40.  
  41. function main() {
  42. creatbox();
  43. document.getElementById("read_osu_map_list")
  44. .addEventListener("change", readfile, false);
  45. }
  46.  
  47. function updatestatus(text = "") {
  48. document.getElementById("current_filter_status").textContent = text;
  49. }
  50.  
  51. function runfilter() {
  52. let length = mode == 3 ? map_list.size : map_list.length;
  53. if (stop || length === 0) return updatestatus("Filter stopped");
  54. let all_map = document.querySelectorAll(".beatmapsets__item");
  55. let count = all_map.length;
  56. updatestatus(`Filter is running\n${length} maps in list`);
  57. all_map.forEach(item => {
  58. setTimeout(() => {
  59. if (stop || length === 0) return updatestatus("Filter stopped");
  60. let map_id = item.querySelector(`[data-audio-url]`).getAttribute("data-audio-url").match(/\/(\d+)\.mp3/);
  61. if (!map_id) return;
  62. map_id = map_id[1];
  63. if (tester(map_id)) {
  64. item.style.opacity = "10%";
  65. if (debug_msg) console.log(`${count} hide ${map_id}`);
  66. }
  67. let link = item.querySelectorAll("a");
  68. link.forEach(a => { if (!a.href.includes("/download")) a.setAttribute("target", "_blank"); });
  69. count--;
  70. if (count === 0) setTimeout(runfilter, 100);
  71. }, 0);
  72. });
  73. }
  74.  
  75. function creatbox() {
  76. let newbox = document.createElement("div");
  77. Object.assign(newbox.style, {
  78. position: "fixed",
  79. top: "20%",
  80. right: "5%",
  81. width: "200px",
  82. "z-index": "100",
  83. border: "2px",
  84. "border-color": "rgba(255, 255, 255, 0.7)",
  85. "border-style": "ridge",
  86. "background-color": "rgba(255, 255, 255, 0.3)",
  87. });
  88. let readfile = document.createElement("input");
  89. Object.assign(readfile, {
  90. type: "file",
  91. id: "read_osu_map_list"
  92. });
  93. let status = document.createElement("span");
  94. Object.assign(status, {
  95. id: "current_filter_status",
  96. style: "word-wrap:break-word;white-space:pre-line;",
  97. textContent: "Load map_list.txt to start"
  98. });
  99. let button = document.createElement("button");
  100. Object.assign(button, {
  101. textContent: "Stop Script",
  102. style: "color: black",
  103. onclick: function () {
  104. stop = true;
  105. updatestatus("Filter stopped");
  106. }
  107. });
  108. newbox.appendChild(readfile);
  109. newbox.appendChild(status);
  110. newbox.appendChild(button);
  111. document.body.appendChild(newbox);
  112. }
  113.  
  114. function readfile(myfile) {
  115. let file = myfile.target.files[0];
  116. if (!file) {
  117. stop = true;
  118. updatestatus("Filter stopped");
  119. return;
  120. }
  121. let reader = new FileReader();
  122. reader.onload = function (myfile) {
  123. let contents = myfile.target.result;
  124. map_list = JSON.parse(contents);
  125. stop = 0;
  126. if (map_list.length > 0) {
  127. switch (mode) {
  128. case 1:
  129. console.log("mode: array");
  130. break;
  131. case 2:
  132. console.log("mode: hash");
  133. let new_obj = {};
  134. map_list.forEach(id => new_obj[id] = true);
  135. new_obj.length = map_list.length;
  136. map_list = new_obj;
  137. break;
  138. case 3:
  139. console.log("mode: set");
  140. map_list = new Set(map_list);
  141. break;
  142. }
  143. runfilter();
  144. }
  145. };
  146. reader.readAsText(file);
  147. }
  148. })();
  149.