-
Notifications
You must be signed in to change notification settings - Fork 0
/
osumapfilter.js
309 lines (290 loc) · 9.58 KB
/
osumapfilter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// ==UserScript==
// @name osuMapFilter
// @namespace https://greasyfork.org/users/110545
// @updateURL https://github.com/x94fujo6rpg/osuMapFilter/raw/master/osumapfilter.js
// @downloadURL https://github.com/x94fujo6rpg/osuMapFilter/raw/master/osumapfilter.js
// @version 0.5
// @description filter osu maps
// @author x94fujo6
// @match https://osu.ppy.sh/*
// @grant none
// ==/UserScript==
/* jshint esversion: 9 */
/*
you need map list to make this work!!!
https://github.com/x94fujo6rpg/osuMapFilter
*/
(function () {
'use strict';
const LS = window.localStorage;
let map_list = [],
stop = false,
debug_msg = false,
mode = 3, // don't touch this
// 1:array 2:hash 3:set, https://jsbench.me/zfknghmteu/2
tester;
function getTester() {
switch (mode) {
case 1:
tester = (id) => { return map_list.includes(id); };
break;
case 2:
tester = (id) => { return map_list[id]; };
break;
case 3:
tester = (id) => { return map_list.has(id); };
break;
default:
tester = false;
break;
}
}
getTester();
if (!tester) return console.log("tester not set");
window.onload = main();
async function main() {
let link = window.location.href;
await wait_tab();
if (link.includes("/beatmapsets")) {
if (link.match(/beatmapsets\/\d+/)) {
waitHTML(".beatmapset-header__buttons [href$='download']", mapPage);
} else {
mapListPage();
}
} else {
console.log("no match, abort");
}
}
function mapPage() {
let dlb = document.querySelector(".beatmapset-header__buttons [href$='download']");
let map_id = dlb.href.match(/beatmapsets\/(\d+)\/download/)[1];
mapListPage(false);
dlb.onclick = () => {
console.log(`download ${map_id}, add to list`);
addItem(map_id);
saveData({
mode,
map_list,
});
};
}
function mapListPage(run = true) {
if (run) {
creatbox();
document.getElementById("read_osu_map_list")
.addEventListener("change", readfile, false);
}
if (LS.getItem("mode")) {
console.log("found old data, load it");
mode = Number(LS.getItem("mode"));
if (mode == 3) {
map_list = new Set(JSON.parse(LS.getItem("map_list")));
} else {
map_list = JSON.parse(LS.getItem("map_list"));
}
getTester();
console.log({
mode,
tester,
});
stop = false;
if (run) {
setTimeout(runfilter, 1000);
}
}
}
function wait_tab() {
return new Promise(resolve => {
if (document.visibilityState === "visible") return resolve();
console.log("tab in background, script paused");
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
console.log("script unpaused");
return resolve();
}
});
});
}
function waitHTML(css_selector, run) {
let id = setInterval(() => {
if (document.querySelectorAll(css_selector).length) {
clearInterval(id);
run();
console.log(`found [${css_selector}]`);
} else {
console.log(`[${css_selector}] not found`);
}
}, 1000);
}
function updatestatus(text = "") {
document.getElementById("current_filter_status").textContent = text;
}
function runfilter() {
let length = mode == 3 ? map_list.size : map_list.length;
if (stop || length == 0) return updatestatus("Filter stopped");
let all_map = document.querySelectorAll(".beatmapsets__item");
let count = all_map.length;
updatestatus(`Filter is running\n${length} maps in list`);
all_map.forEach(item => {
setTimeout(() => {
if (stop || length === 0) return updatestatus("Filter stopped");
let map_id = item.querySelector(`[data-audio-url]`).getAttribute("data-audio-url").match(/\/(\d+)\.mp3/);
let link = item.querySelectorAll("a");
let dlb = item.querySelector("[href$='/download']");
if (!map_id) return;
map_id = map_id[1];
if (tester(map_id)) {
item.style.opacity = "10%";
if (debug_msg) console.log(`${count} hide ${map_id}`);
} else {
item.style.opacity = "100%";
}
link.forEach(a => {
if (!a.href.includes("/download")) {
a.setAttribute("target", "_blank");
}
});
dlb.onclick = () => {
console.log(`download ${map_id}, add to list`);
addItem(map_id);
saveData({
mode,
map_list,
});
};
count--;
if (count == 0) {
setTimeout(runfilter, 200);
}
}, 0);
});
}
function addItem(id = "") {
switch (mode) {
case 1:
map_list.push(id);
break;
case 2:
map_list[id] = true;
break;
case 3:
map_list.add(id);
break;
default:
throw Error("unknown mode");
}
}
function creatbox() {
let newbox = document.createElement("div");
Object.assign(newbox.style, {
position: "fixed",
top: "20%",
right: "5%",
width: "200px",
"z-index": "100",
border: "2px",
"border-color": "rgba(255, 255, 255, 0.7)",
"border-style": "ridge",
"background-color": "rgba(255, 255, 255, 0.3)",
});
let readfile = document.createElement("input");
Object.assign(readfile, {
type: "file",
id: "read_osu_map_list"
});
let status = document.createElement("span");
Object.assign(status, {
id: "current_filter_status",
style: "word-wrap:break-word;white-space:pre-line;",
textContent: "Load map_list.txt to start"
});
let button = document.createElement("button");
Object.assign(button, {
textContent: "Stop Script",
style: "color: black",
onclick: function () {
if (!stop) {
stop = true;
updatestatus("Filter stopped");
button.textContent = "Resume Script";
} else {
stop = false;
button.textContent = "Stop Script";
runfilter();
}
}
});
newbox.appendChild(readfile);
newbox.appendChild(status);
newbox.appendChild(button);
document.body.appendChild(newbox);
}
function readfile(myfile) {
let file = myfile.target.files[0];
if (!file) {
stop = true;
updatestatus("Filter stopped");
return;
}
let reader = new FileReader();
reader.onload = function (myfile) {
let contents = myfile.target.result;
map_list = JSON.parse(contents);
stop = 0;
if (map_list.length > 0) {
switch (mode) {
case 1:
console.log("mode: array");
break;
case 2:
console.log("mode: hash");
let new_obj = {};
map_list.forEach(id => new_obj[id] = true);
new_obj.length = map_list.length;
map_list = new_obj;
break;
case 3:
console.log("mode: set");
map_list = new Set(map_list);
break;
}
runfilter();
saveData({
mode,
map_list,
});
}
};
reader.readAsText(file);
}
function removeData(data = {}) {
return new Promise((resolve, reject) => {
try {
for (let key in data) {
LS.removeItem(key);
}
} catch (e) {
console.log(e);
reject();
}
resolve();
});
}
async function saveData(data = {}) {
//await removeData(data);
for (let key in data) {
if (key == "map_list") {
let json;
if (data[key] instanceof Set) {
json = JSON.stringify([...data[key]]);
} else {
json = JSON.stringify(data[key]);
}
LS.setItem(key, json);
continue;
}
LS.setItem(key, data[key]);
}
return true;
}
})();