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.1
  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;')();
  29.  
  30. let hkey_script = 'nzsxclvflluv';
  31. if (window[hkey_script]) return;
  32. window[hkey_script] = true;
  33.  
  34. //if (!document.documentElement) return window.requestAnimationFrame($$);
  35.  
  36. let $$requestAnimationFrame = window.requestAnimationFrame.bind(window);
  37. let $$setTimeout = window.setTimeout.bind(window);
  38.  
  39. let mi = 0;
  40. let sb = {};
  41. const sFunc = (prop) => {
  42. return (func, ms, ...args) => {
  43. mi++;
  44. sb[mi] = {
  45. handler: args.length > 0 ? func.bind(null, ...args) : func,
  46. isNative: (func + "").indexOf('[native code]') > 0,
  47. [prop]: ms,
  48. nextAt: Date.now() + (ms > 0 ? ms : 0)
  49. };
  50. return mi;
  51. }
  52. }
  53. const rm = (jd) => {
  54. let o = sb[jd];
  55. if (typeof o != 'object') return;
  56. for (let k in o) o[k] = null;
  57. o = null;
  58. sb[jd] = null;
  59. delete sb[jd];
  60. }
  61. window.setTimeout = sFunc('timeout');
  62. window.setInterval = sFunc('interval');
  63. window.clearInterval = window.clearTimeout = rm;
  64.  
  65. const $busy = Symbol('$busy');
  66.  
  67. // high energy for non-native API
  68. let pf = (handler =>
  69. new Promise(resolve => {
  70. if (!($busy in handler)) {
  71. handler[$busy] = true;
  72. handler();
  73. delete handler[$busy];
  74. }
  75. resolve();
  76. })
  77. );
  78.  
  79. let dt = 0;
  80. let jf;
  81. (jf = $$requestAnimationFrame.bind(window, () => {
  82. let now = Date.now();
  83. if (now < dt) return jf();
  84. // ======= MarcoTask [requestAnimationFrame] =======
  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. isNative
  94. } = o;
  95. if (now < nextAt) continue;
  96. isNative ? handler() : promises.push(pf(handler));
  97. if (interval > 0) {
  98. o.nextAt += interval;
  99. } else {
  100. rm(mi);
  101. }
  102. }
  103. // ======= MarcoTask [requestAnimationFrame] =======
  104. if (promises.length > 0) {
  105. if (document.hidden) dt = Date.now() + 160; // lower task execution rate for background playing
  106. let ret1 = Promise.all(promises);
  107. let ret2 = new Promise(resolve => $$setTimeout(resolve, 16));
  108. let race = Promise.race([ret1, ret2]);
  109. race.then(jf);
  110. } else {
  111. jf();
  112. }
  113. }))();
  114.  
  115. // Your code here...
  116. })();

QingJ © 2025

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