OsuWebRequests

simple wrapper for making osu related sequential web requests

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/441005/1241410/OsuWebRequests.js

  1. class Request {
  2. constructor(url, params, successFn, errorFn, options = {}) {
  3. this.url = new URL(url);
  4. this.url.search = new URLSearchParams(params).toString();
  5. this.successFn = successFn;
  6. this.errorFn = errorFn;
  7. this.options = options;
  8. }
  9.  
  10. send(cb) {
  11. fetch(this.url, this.options).then(res => {
  12. cb();
  13. if (this.successFn) {
  14. this.successFn(res);
  15. }
  16. }).catch(err => {
  17. cb();
  18. if (this.errorFn) {
  19. this.errorFn(err);
  20. }
  21. });
  22. }
  23. }
  24.  
  25. class Web {
  26. base = window.location.origin;
  27.  
  28. constructor(apiKey = null) {
  29. this.key = apiKey;
  30. this.requests = [];
  31. }
  32.  
  33. get(path, params, successFn, errorFn, options = {}) {
  34. const req = new Request(`${this.base}${path}`, params, successFn, errorFn, options);
  35. this.queue(req);
  36. }
  37.  
  38. api(path, params, successFn, errorFn) {
  39. params.k = this.key;
  40. const req = new Request(`${this.base}/api${path}`, params, successFn, errorFn);
  41. this.queue(req);
  42. }
  43.  
  44. queue(req) {
  45. this.requests.push(req);
  46. if (this.requests.length === 1) {
  47. this.next();
  48. }
  49. }
  50.  
  51. next() {
  52. if (this.requests.length === 0) {
  53. return;
  54. }
  55. const req = this.requests[0];
  56. req.send(() => {
  57. this.requests.shift();
  58. this.next();
  59. });
  60. }
  61. }

QingJ © 2025

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