YouTube CPU Tamer by AnimationFrame

减少YouTube影片所致的能源消耗

目前为 2021-08-30 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube CPU Tamer by AnimationFrame
  3. // @name:en YouTube CPU Tamer by AnimationFrame
  4. // @name:jp YouTube CPU Tamer by AnimationFrame
  5. // @name:zh-tw YouTube CPU Tamer by AnimationFrame
  6. // @name:zh-cn YouTube CPU Tamer by AnimationFrame
  7. // @namespace http://tampermonkey.net/
  8. // @version 2021.08.30
  9. // @license MIT License
  10. // @description Reduce Browser's Energy Impact for playing YouTube Video
  11. // @description:en Reduce Browser's Energy Impact for playing YouTube Video
  12. // @description:jp YouTubeビデオのエネルギーインパクトを減らす
  13. // @description:zh-tw 減少YouTube影片所致的能源消耗
  14. // @description:zh-cn 减少YouTube影片所致的能源消耗
  15. // @author CY Fung
  16. // @include https://www.youtube.com/*
  17. // @include https://www.youtube.com/embed/*
  18. // @include https://www.youtube-nocookie.com/embed/*
  19. // @include https://www.youtube.com/live_chat*
  20. // @include https://www.youtube.com/live_chat_replay*
  21. // @icon https://www.google.com/s2/favicons?domain=youtube.com
  22. // @run-at document-start
  23. // @grant none
  24. // ==/UserScript==
  25. (function $$() {
  26. 'use strict';
  27.  
  28. const window = new Function('return window;')(); // real window object
  29.  
  30. const hkey_script = 'nzsxclvflluv';
  31. if (window[hkey_script]) return; // avoid duplicated scripting
  32. window[hkey_script] = true;
  33.  
  34. //if (!document.documentElement) return window.requestAnimationFrame($$); // no document access
  35.  
  36. // copies of native functions
  37. const $$requestAnimationFrame = window.requestAnimationFrame.bind(window); // core looping
  38. const $$setTimeout = window.setTimeout.bind(window); // for race
  39. const $$setInterval = window.setInterval.bind(window); // for background execution
  40.  
  41. const $busy = Symbol('$busy');
  42.  
  43. let mi = 0;
  44. const sb = {};
  45. const sFunc = (prop) => {
  46. return (func, ms, ...args) => {
  47. mi++; // start at 1
  48. let handler = args.length > 0 ? func.bind(null, ...args) : func; // original func if no extra argument
  49. handler[$busy] = handler[$busy] || 0;
  50. sb[mi] = {
  51. handler,
  52. [prop]: ms, // timeout / interval; value can be undefined
  53. nextAt: Date.now() + (ms > 0 ? ms : 0) // overload for setTimeout(func);
  54. };
  55. return mi;
  56. };
  57. };
  58. const rm = (jd) => {
  59. let o = sb[jd];
  60. if (typeof o != 'object') return;
  61. for (let k in o) o[k] = null;
  62. o = null;
  63. sb[jd] = null;
  64. delete sb[jd];
  65. };
  66. window.setTimeout = sFunc('timeout');
  67. window.setInterval = sFunc('interval');
  68. window.clearInterval = window.clearTimeout = rm;
  69.  
  70.  
  71. const pf = (
  72. handler => new Promise(resolve => {
  73. // try catch is not required - no further execution on the handler
  74. // For function handler with high energy impact, discard 1st, 2nd, ... (n-1)th calling: (a,b,c,a,b,d,e,f) => (c,a,b,d,e,f)
  75. // For function handler with low energy impact, discard or not discard depends on system performance
  76. if (handler[$busy] == 1) handler();
  77. handler[$busy]--;
  78. resolve();
  79. })
  80. );
  81.  
  82. let jf, tf;
  83. let bgExecutionAt = 0; // set at 0 to trigger tf in background startup when requestAnimationFrame is not responsive
  84. tf = () => {
  85. new Promise(resolveApp1 => {
  86. let now = Date.now();
  87. bgExecutionAt = now + 160; // if requestAnimationFrame is not responsive (e.g. background running)
  88. let promisesF = [];
  89. for (let mi in sb) {
  90. const o = sb[mi];
  91. let {
  92. handler,
  93. // timeout,
  94. interval,
  95. nextAt
  96. } = o;
  97. if (now < nextAt) continue;
  98. handler[$busy]++;
  99. promisesF.push(handler);
  100. if (interval > 0) { // prevent undefined, zero, negative values
  101. o.nextAt += +interval; // convertion from string to number if necessary; decimal is acceptable
  102. } else {
  103. rm(mi); // remove timeout
  104. }
  105. }
  106. resolveApp1(promisesF);
  107. }).then(promisesF => {
  108. bgExecutionAt = Date.now() + 160; // if requestAnimationFrame is not responsive (e.g. background running)
  109. let hidden = document.hidden; // background running would not call requestAnimationFrame
  110. if (promisesF.length == 0) { // no handler functions
  111. // requestAnimationFrame when the page is active
  112. // execution interval is no less than AnimationFrame
  113. return hidden || jf();
  114. }
  115. if (!hidden) {
  116. let ret2 = new Promise(resolve => $$setTimeout(resolve, 16));
  117. let promises = promisesF.map(pf);
  118. let ret1 = Promise.all(promises);
  119. let race = Promise.race([ret1, ret2]);
  120. // ensure jf must be called after 16ms to maintain visual changes in high fps.
  121. // >16ms examples: repaint/reflow, change of style/content
  122. race.then(jf);
  123. promises.length = 0;
  124. } else {
  125. promisesF.forEach(pf);
  126. }
  127. promisesF.length = 0;
  128. })
  129. };
  130. (jf = $$requestAnimationFrame.bind(window, tf))();
  131.  
  132. $$setInterval(() => {
  133. // no response of requestAnimationFrame; e.g. running in background
  134. if (Date.now() > bgExecutionAt) tf();
  135. }, 250);
  136. // i.e. 4 times per second for background execution - to keep YouTube application functional
  137.  
  138. window.addEventListener("yt-navigate-finish", () => {
  139. // ensure all interval handlers can be executed after YouTube navigation.
  140. for (let jb in sb) sb[jb].handler[$busy] = 0;
  141. }, true); // capturing event - to let it runs before all everything else.
  142.  
  143. // Your code here...
  144. })();