OsuPageObserver

Detects dynamic osu page changes

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/441010/1025088/OsuPageObserver.js

  1. class OsuWebObserver {
  2. constructor(staticFn, dynamicFn) {
  3. this.static = staticFn;
  4. this.dynamic = dynamicFn;
  5. this.static();
  6. const pageType = this.getType();
  7. if (pageType === "dynamic") {
  8. const firstLoad = new MutationObserver(() => {
  9. this.dynamic();
  10. firstLoad.disconnect();
  11. });
  12. const layout = document.querySelector(".osu-layout__section")?.firstElementChild;
  13. if (layout != null) {
  14. firstLoad.observe(layout, {childList: true});
  15. }
  16. }
  17. this.pageObserver = new MutationObserver(this.detectPageChange);
  18. this.pageObserver.observe(document.documentElement, {childList: true});
  19. }
  20. detectPageChange = (mutations) => {
  21. for (const mutation of mutations) {
  22. if (mutation.addedNodes.length > 0) {
  23. mutation.addedNodes.forEach(node => {
  24. if (node.nodeName == "BODY") {
  25. this.static();
  26. this.dynamic();
  27. return;
  28. }
  29. });
  30. }
  31. }
  32. }
  33.  
  34. getType = () => {
  35. const type = location.pathname.split("/")[1];
  36. const dynamic = ["users", "beatmapsets", "scores"];
  37. if (dynamic.includes(type)) {
  38. return "dynamic";
  39. }
  40. return "static";
  41. }
  42.  
  43. destroy = () => {
  44. this.pageObserver.disconnect();
  45. }
  46. }