Youtube Music fix performance

Try to fix the performance issues that youtube music has

  1. // ==UserScript==
  2. // @name Youtube Music fix performance
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Try to fix the performance issues that youtube music has
  6. // @author Marco Pfeiffer <git@marco.zone>
  7. // @match https://music.youtube.com/*
  8. // @icon https://music.youtube.com/favicon.ico
  9. // @run-at document-start
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const global = window.unsafeWindow ?? window;
  17. const setInterval = global.setInterval;
  18. const clearInterval = global.clearInterval;
  19. const intervals = new Set();
  20.  
  21. global.setInterval = function(fn, ms) {
  22. const n = setInterval.call(this, fn, ms);
  23. console.log("setup interval", n, fn, ms, "still pending timers", Array.from(intervals));
  24. intervals.add(n);
  25. return n;
  26. };
  27. global.clearInterval = function (n) {
  28. if (!intervals.delete(n)) {
  29. console.log("failed cleanup of", n, "still pending timers", Array.from(intervals));
  30. if (intervals.size > 5) {
  31. const timersToCleanUp = Array.from(intervals).slice(0, -5);
  32. console.log("cleanup timers", timersToCleanUp);
  33. timersToCleanUp.forEach(clearInterval);
  34. }
  35. } else {
  36. console.log("cleanup interval", n, "still pending timers", Array.from(intervals));
  37. }
  38. return clearInterval.call(this, n);
  39. };
  40. })();