YouTube CPU Tamer by AnimationFrame

Reduce Browser's Energy Impact for playing YouTube Video

当前为 2021-08-29 提交的版本,查看 最新版本

  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.29.3
  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. let mi = 0;
  42. const sb = {};
  43. const sFunc = (prop) => {
  44. return (func, ms, ...args) => {
  45. mi++;
  46. sb[mi] = {
  47. handler: args.length > 0 ? func.bind(null, ...args) : func, // original func if no extra argument
  48. [prop]: ms, // timeout / interval
  49. nextAt: Date.now() + (ms > 0 ? ms : 0) // overload for setTimeout(func);
  50. };
  51. return mi;
  52. };
  53. };
  54. const rm = (jd) => {
  55. let o = sb[jd];
  56. if (typeof o != 'object') return;
  57. for (let k in o) o[k] = null;
  58. o = null;
  59. sb[jd] = null;
  60. delete sb[jd];
  61. };
  62. window.setTimeout = sFunc('timeout');
  63. window.setInterval = sFunc('interval');
  64. window.clearInterval = window.clearTimeout = rm;
  65.  
  66. const $busy = Symbol('$busy');
  67.  
  68. const pf = (handler => {
  69. if ($busy in handler) return;
  70. handler[$busy] = true;
  71. return new Promise(resolve => {
  72. // == microTask [Promise] ==
  73. handler(); // try catch is not required - no further execution on the handler
  74. delete handler[$busy];
  75. resolve();
  76. // == microTask [Promise] ==
  77. });
  78. });
  79.  
  80. let jf, tf;
  81. let bgExecutionAt = 0;
  82. tf = () => {
  83. let now = Date.now();
  84. // ======= MarcoTask [requestAnimationFrame / setInterval] =======
  85. let promises = [];
  86. for (let mi in sb) {
  87. const o = sb[mi];
  88. let {
  89. handler,
  90. // timeout,
  91. interval,
  92. nextAt
  93. } = o;
  94. if (now < nextAt) continue;
  95. promises.push(pf(handler));
  96. if (interval > 0) {
  97. o.nextAt += interval;
  98. } else {
  99. rm(mi);
  100. }
  101. }
  102. // ======= MarcoTask [requestAnimationFrame / setInterval] =======
  103. if (!document.hidden) {
  104. // calling requestAnimationFrame in visible tab(s) only
  105. if (promises.length > 0) {
  106. let ret1 = Promise.all(promises);
  107. let ret2 = new Promise(resolve => $$setTimeout(resolve, 16));
  108. let race = Promise.race([ret1, ret2]); // ensure jf must be called after 16ms to maintain visual changes in high fps.
  109. race.then(jf);
  110. } else {
  111. jf(); // execution rate is captured by requestAnimationFrame
  112. }
  113. }
  114. bgExecutionAt = now + 160; // lower task execution rate for background playing
  115. };
  116. (jf = $$requestAnimationFrame.bind(window, tf))();
  117.  
  118. $$setInterval(() => {
  119. // no response of requestAnimationFrame in background
  120. if (Date.now() > bgExecutionAt) tf();
  121. }, 250);
  122. // i.e. interval = 500ms for background execution
  123.  
  124. // Your code here...
  125. })();