Youtube Player perf

Optimizes animation calls for lower GPU/CPU consumption

当前为 2023-07-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube Player perf
  3. // @version 0.1
  4. // @description Optimizes animation calls for lower GPU/CPU consumption
  5. // @namespace nopeless.github.io
  6. // @author nopeless
  7. // @match https://www.youtube.com/watch?v=*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. /* global _yt_player */
  14.  
  15. (() => {
  16. "use strict";
  17.  
  18. function modifyBase() {
  19. console.log("Overriding _yt_player methods");
  20.  
  21. if (!window._yt_player) return console.error("YT player not avaliable, load order is wrong");
  22.  
  23. const PV = _yt_player.PV;
  24.  
  25. // save the original prototype
  26. const PVPrototype = _yt_player.PV.prototype;
  27.  
  28. let dirty = false;
  29.  
  30. function update(a) {
  31. for (var b = _yt_player.u(Object.keys(a)), c = b.next(); !c.done; c = b.next()) {
  32. c = c.value;
  33.  
  34. if (this.__updateCache.get(c) !== a[c]) {
  35. // console.log("updating", c, a[c]);
  36. this.updateValue(c, a[c]);
  37. dirty = true;
  38. this.__updateCache.set(c, a[c]);
  39. }
  40. }
  41. }
  42.  
  43. _yt_player.PV = function (...args) {
  44. PV.call(this, ...args);
  45.  
  46. this.__updateCache = new Map();
  47.  
  48. // override update
  49. this.update = update;
  50. };
  51.  
  52. _yt_player.PV.prototype = Object.create(PVPrototype);
  53. _yt_player.PV.prototype.constructor = _yt_player.PV;
  54.  
  55.  
  56.  
  57. const Nn = _yt_player.Nn;
  58. let counter = 0; // up to 4
  59. _yt_player.Nn = (a, b, c) => {
  60. // don't do excessive progress bar updates
  61. if (b === "transform") {
  62. if (dirty) {
  63. counter = 4;
  64. // console.log("unmarking dirty");
  65. dirty = false;
  66. }
  67. if (counter > 0) {
  68. // console.log("updating bar");
  69. counter--;
  70. } else {
  71. return;
  72. }
  73. }
  74. Nn(a, b, c);
  75. };
  76. }
  77.  
  78. window.__modifyBase = modifyBase;
  79.  
  80. const ob = new MutationObserver(mrs => {
  81. const l = mrs.map(mr => mr.addedNodes[0]).find(node => node && node.nodeName === "SCRIPT" && node.src && node.src.match(/\/base\.js$/));
  82.  
  83. if (!l) return;
  84.  
  85. l.setAttribute("onload", "__modifyBase()");
  86.  
  87. ob.disconnect();
  88. });
  89.  
  90. console.log("watching for script changes");
  91. ob.observe(document, { attributes: false, childList: true, subtree: true });
  92. })();
  93.  

QingJ © 2025

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