YouTube JS Engine Tamer

To enhance YouTube performance by modifying YouTube JS Engine

当前为 2023-09-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube JS Engine Tamer
  3. // @namespace UserScripts
  4. // @match https://www.youtube.com/*
  5. // @version 0.4.6
  6. // @license MIT
  7. // @author CY Fung
  8. // @icon https://github.com/cyfung1031/userscript-supports/raw/main/icons/yt-engine.png
  9. // @description To enhance YouTube performance by modifying YouTube JS Engine
  10. // @grant none
  11. // @run-at document-start
  12. // @unwrap
  13. // @inject-into page
  14. // @allFrames true
  15. // ==/UserScript==
  16.  
  17. (() => {
  18.  
  19. const NATIVE_CANVAS_ANIMATION = true; // for #cinematics
  20. const FIX_schedulerInstanceInstance_ = true;
  21. const FIX_yt_player = true;
  22. const FIX_Animation_n_timeline = true;
  23. const NO_PRELOAD_GENERATE_204 = false;
  24. const CHANGE_appendChild = true;
  25.  
  26. const FIX_error_many_stack = true; // should be a bug caused by uBlock Origin
  27. // const FIX_error_many_stack_keepAliveDuration = 200; // ms
  28. // const FIX_error_many_stack_keepAliveDuration_check_if_n_larger_than = 8;
  29.  
  30. /*
  31. window.addEventListener('edm',()=>{
  32. let p = [...this.onerror.errorTokens][0].token; (()=>{ console.log(p); throw new Error(p);console.log(334,p) })()
  33. });
  34.  
  35. window.addEventListener('edn',()=>{
  36. let p = [...this.onerror.errorTokens][0].token+"X"; (()=>{ console.log(p); throw new Error(p);console.log(334,p) })()
  37. });
  38. window.addEventListener('edr',()=>{
  39. let p = '123'; (()=>{ console.log(p); throw new Error(p);console.log(334,p) })()
  40. });
  41. */
  42.  
  43.  
  44. const prepareLogs = [];
  45.  
  46. const skipAdsDetection = new Set(['/robots.txt', '/live_chat', '/live_chat_replay']);
  47.  
  48. let winError00 = window.onerror;
  49.  
  50. let fix_error_many_stack_state = !FIX_error_many_stack ? 0 : skipAdsDetection.has(location.pathname) ? 2 : 1;
  51.  
  52.  
  53. ; fix_error_many_stack_state === 2 && (() => {
  54.  
  55.  
  56. let p1 = winError00;
  57.  
  58. let objectPrune = null;
  59. let stackNeedleDetails = null;
  60.  
  61. Object.defineProperty(Function.prototype, 'findOwner', {
  62. get() {
  63. objectPrune = this;
  64. return this._findOwner;
  65. },
  66. set(nv) {
  67. this._findOwner = nv;
  68. return true;
  69. },
  70. enumerable: true,
  71. configurable: true
  72. });
  73.  
  74. Object.defineProperty(Object.prototype, 'matchAll', {
  75. get() {
  76. stackNeedleDetails = this;
  77. return true;
  78. },
  79. enumerable: true,
  80. configurable: true
  81. });
  82.  
  83. try {
  84. JSON.parse("{}");
  85. } catch (e) {
  86. console.warn(e)
  87. fix_error_many_stack_state = 0;
  88. }
  89.  
  90. delete Function.prototype['findOwner'];
  91. delete Object.prototype['matchAll'];
  92.  
  93. let p2 = window.onerror;
  94.  
  95. if (p1 !== p2) fix_error_many_stack_state = 0;
  96.  
  97. if (fix_error_many_stack_state === 0) return;
  98.  
  99. // the following will only execute when Brave's scriptlets.js is executed.
  100.  
  101. prepareLogs.push("fix_error_many_stack_state NB")
  102.  
  103. if (stackNeedleDetails) {
  104. stackNeedleDetails.pattern = null;
  105. stackNeedleDetails.re = null;
  106. stackNeedleDetails.expect = null;
  107. stackNeedleDetails.matchAll = true;
  108. }
  109.  
  110. if (objectPrune) {
  111. objectPrune.findOwner = objectPrune.mustProcess = objectPrune.logJson = () => { }
  112. delete objectPrune._findOwner;
  113. }
  114.  
  115.  
  116. JSON.parseProxy = JSON.parse;
  117.  
  118.  
  119.  
  120. fix_error_many_stack_state = 1;
  121.  
  122.  
  123.  
  124. })();
  125.  
  126. ; fix_error_many_stack_state === 1 && (() => {
  127.  
  128.  
  129. let p1 = winError00;
  130.  
  131. try {
  132. JSON.parse("{}");
  133. } catch (e) {
  134. console.warn(e)
  135. fix_error_many_stack_state = 0;
  136. }
  137.  
  138. let p2 = window.onerror;
  139.  
  140. if (p1 === p2) return;
  141.  
  142. window.onerror = p1;
  143.  
  144. if (fix_error_many_stack_state === 0) return;
  145.  
  146. // the following will only execute when Brave's scriptlets.js is executed.
  147.  
  148. prepareLogs.push("fix_error_many_stack_state AB")
  149.  
  150. JSON.parseProxy = JSON.parse;
  151.  
  152. JSON.parse = ((parse) => {
  153.  
  154. parse = parse.bind(JSON); // get a new instance of the current JSON.parse
  155. return function (text, reviver) {
  156. const onerror = window.onerror;
  157. window.onerror = null;
  158. let r;
  159. try {
  160. r = parse(...arguments);
  161. } catch (e) {
  162. r = e;
  163. }
  164. window.onerror = onerror;
  165. if (r instanceof Error) {
  166. throw r;
  167. }
  168. return r;
  169. }
  170.  
  171. })(JSON.parse);
  172.  
  173.  
  174. })();
  175.  
  176.  
  177.  
  178. // ================================================ 0.4.5 ================================================
  179.  
  180.  
  181. // ; (() => {
  182.  
  183. // if (FIX_error_many_stack && self instanceof Window) {
  184. // // infinite stack due to matchesStackTrace inside objectPrune of AdsBlock
  185.  
  186. // const pdK = Object.getOwnPropertyDescriptor(window, 'onerror');
  187. // if (!pdK || (pdK.get && pdK.configurable)) {
  188.  
  189. // } else {
  190. // return;
  191. // }
  192.  
  193. // let unsupportErrorFix = false;
  194.  
  195. // let firstHook = true;
  196. // let busy33 = false;
  197.  
  198. // let state = 0;
  199.  
  200. // if (pdK) {
  201. // delete window['onerror'];
  202. // }
  203.  
  204. // const pd = {
  205. // get() {
  206. // const stack = (new Error()).stack;
  207. // // targetStack = stack;
  208. // let isGetExceptionToken = stack.indexOf('getExceptionToken') >= 0;
  209. // state = isGetExceptionToken ? 1 : 0;
  210. // delete Window.prototype['onerror'];
  211. // let r = pdK ? pdK.get.call(this) : this.onerror;
  212. // Object.defineProperty(Window.prototype, 'onerror', pd);
  213. // // console.log('onerror get', r)
  214. // return r;
  215. // },
  216. // set(nv) {
  217. // const stack = (new Error()).stack;
  218. // let isGetExceptionToken = stack.indexOf('getExceptionToken') >= 0;
  219. // state = state === 1 && isGetExceptionToken ? 2 : 0;
  220. // /** @type {string?} */
  221. // let sToken = null;
  222. // if (unsupportErrorFix || busy33) {
  223.  
  224. // } else if (typeof nv === 'function' && state === 2) {
  225. // if (firstHook) {
  226. // firstHook = false;
  227. // console.groupCollapsed('Infinite onerror Bug Found');
  228. // console.log(location.href);
  229. // console.log(stack);
  230. // console.log(nv);
  231. // console.groupEnd();
  232. // }
  233. // let _token = null;
  234. // busy33 = true;
  235. // String.prototype.includes76 = String.prototype.includes;
  236. // String.prototype.includes = function (token) {
  237. // _token = token;
  238. // return true;
  239. // }
  240. // nv('token');
  241. // String.prototype.includes = String.prototype.includes76;
  242. // sToken = _token;
  243. // busy33 = false;
  244. // if (typeof sToken !== 'string') {
  245. // unsupportErrorFix = true;
  246. // }
  247. // }
  248. // delete Window.prototype['onerror'];
  249. // if (typeof sToken === 'string' && sToken.length > 1) {
  250. // /** @type {string} */
  251. // const token = sToken;
  252. // /** @type {OnErrorEventHandler & {errorTokens: Set<string>?} } */
  253. // const currentOnerror = pdK ? pdK.get.call(this) : this.onerror;
  254.  
  255. // const now = Date.now();
  256. // const tokenEntry = {
  257. // token,
  258. // expired: now + FIX_error_many_stack_keepAliveDuration
  259. // }
  260. // /** @typedef {typeof tokenEntry} TokenEntry */
  261.  
  262. // /** @type {Set<TokenEntry>} */
  263. // const errorTokens = currentOnerror.errorTokens;
  264.  
  265. // if (errorTokens) {
  266. // if (errorTokens.size > FIX_error_many_stack_keepAliveDuration_check_if_n_larger_than) {
  267. // for (const entry of errorTokens) {
  268. // if (entry.expired < now) {
  269. // errorTokens.delete(entry);
  270. // }
  271. // }
  272. // }
  273. // errorTokens.add(tokenEntry)
  274. // } else {
  275. // /** @type {Set<TokenEntry>} */
  276. // const errorTokens = new Set([tokenEntry]);
  277. // /** @type {OnErrorEventHandler & {errorTokens: Set<string>} } */
  278. // const newOnerror = ((oe) => {
  279. // const r = function (msg, ...args) {
  280. // if (typeof msg === 'string' && errorTokens.size > 0) {
  281. // for (const entry of errorTokens) {
  282. // if (msg.includes(entry.token)) return true;
  283. // }
  284. // }
  285. // if (typeof oe === 'function') {
  286. // return oe.apply(this, arguments);
  287. // }
  288. // };
  289. // r.errorTokens = errorTokens;
  290. // return r;
  291. // })(currentOnerror);
  292.  
  293. // if (pdK && pdK.set) pdK.set.call(this, newOnerror);
  294. // else this.onerror = newOnerror;
  295. // }
  296. // } else {
  297. // if (pdK && pdK.set) pdK.set.call(this, nv);
  298. // else this.onerror = nv;
  299. // }
  300. // Object.defineProperty(Window.prototype, 'onerror', pd);
  301.  
  302. // // console.log('onerror set', nv)
  303. // return true;
  304. // },
  305. // enumerable: true,
  306. // configurable: true
  307. // }
  308.  
  309. // Object.defineProperty(Window.prototype, 'onerror', pd);
  310.  
  311.  
  312. // }
  313.  
  314.  
  315. // })();
  316.  
  317.  
  318.  
  319. // ================================================ 0.4.5 ================================================
  320.  
  321.  
  322. // << if FIX_yt_player >>
  323.  
  324. // credit to @nopeless (https://greasyfork.org/scripts/471489-youtube-player-perf/)
  325. const PERF_471489_ = true;
  326. // PERF_471489_ is not exactly the same to Youtube Player perf v0.7
  327. // This script uses a much gentle way to tamer the JS engine instead.
  328.  
  329. // << end >>
  330.  
  331. const steppingScaleN = 200; // transform: scaleX(k/N); 0<k<N
  332.  
  333. const Promise = (async () => { })().constructor;
  334.  
  335. let isMainWindow = false;
  336. try {
  337. isMainWindow = window.document === window.top.document
  338. } catch (e) { }
  339.  
  340. let NO_PRELOAD_GENERATE_204_BYPASS = NO_PRELOAD_GENERATE_204 ? false : true;
  341.  
  342. const onRegistryReady = (callback) => {
  343. if (typeof customElements === 'undefined') {
  344. if (!('__CE_registry' in document)) {
  345. // https://github.com/webcomponents/polyfills/
  346. Object.defineProperty(document, '__CE_registry', {
  347. get() {
  348. // return undefined
  349. },
  350. set(nv) {
  351. if (typeof nv == 'object') {
  352. delete this.__CE_registry;
  353. this.__CE_registry = nv;
  354. this.dispatchEvent(new CustomEvent(EVENT_KEY_ON_REGISTRY_READY));
  355. }
  356. return true;
  357. },
  358. enumerable: false,
  359. configurable: true
  360. })
  361. }
  362. let eventHandler = (evt) => {
  363. document.removeEventListener(EVENT_KEY_ON_REGISTRY_READY, eventHandler, false);
  364. const f = callback;
  365. callback = null;
  366. eventHandler = null;
  367. f();
  368. };
  369. document.addEventListener(EVENT_KEY_ON_REGISTRY_READY, eventHandler, false);
  370. } else {
  371. callback();
  372. }
  373. };
  374.  
  375. const getZq = (_yt_player) => {
  376.  
  377. const w = 'Zq';
  378.  
  379. let arr = [];
  380.  
  381. for (const [k, v] of Object.entries(_yt_player)) {
  382.  
  383. const p = typeof v === 'function' ? v.prototype : 0;
  384. if (p
  385. && typeof p.start === 'function' && p.start.length === 0
  386. && typeof p.isActive === 'function' && p.isActive.length === 0
  387. && typeof p.stop === 'function' && p.stop.length === 0
  388. && !p.isComplete && !p.getStatus && !p.getResponseHeader && !p.getLastError
  389. && !p.send && !p.abort
  390. && !p.sample && !p.initialize && !p.fail && !p.getName
  391. // && !p.dispose && !p.isDisposed
  392.  
  393. ) {
  394. arr = addProtoToArr(_yt_player, k, arr) || arr;
  395.  
  396.  
  397. }
  398.  
  399. }
  400.  
  401. if (arr.length === 0) {
  402.  
  403. console.warn(`Key does not exist. [${w}]`);
  404. } else {
  405.  
  406. console.log(`[${w}]`, arr);
  407. return arr[0];
  408. }
  409.  
  410.  
  411.  
  412.  
  413. }
  414.  
  415.  
  416. const getVG = (_yt_player) => {
  417. const w = 'VG';
  418.  
  419. let arr = [];
  420.  
  421. for (const [k, v] of Object.entries(_yt_player)) {
  422.  
  423. const p = typeof v === 'function' ? v.prototype : 0;
  424. if (p
  425. && typeof p.show === 'function' && p.show.length === 1
  426. && typeof p.hide === 'function' && p.hide.length === 0
  427. && typeof p.stop === 'function' && p.stop.length === 0) {
  428.  
  429. arr = addProtoToArr(_yt_player, k, arr) || arr;
  430.  
  431. }
  432.  
  433. }
  434.  
  435.  
  436. if (arr.length === 0) {
  437.  
  438. console.warn(`Key does not exist. [${w}]`);
  439. } else {
  440.  
  441. console.log(`[${w}]`, arr);
  442. return arr[0];
  443. }
  444.  
  445.  
  446.  
  447. }
  448.  
  449.  
  450. const getzo = (_yt_player) => {
  451. const w = 'zo';
  452.  
  453. let arr = [];
  454.  
  455. for (const [k, v] of Object.entries(_yt_player)) {
  456.  
  457. if (
  458. typeof v === 'function' && v.length === 3 && k.length < 3
  459. && (v + "").includes("a.style[b]=c")
  460. ) {
  461.  
  462. arr.push(k);
  463.  
  464. }
  465.  
  466. }
  467.  
  468.  
  469. if (arr.length === 0) {
  470.  
  471. console.warn(`Key does not exist. [${w}]`);
  472. } else {
  473.  
  474. console.log(`[${w}]`, arr);
  475. return arr[0];
  476. }
  477.  
  478. }
  479.  
  480. const addProtoToArr = (parent, key, arr) => {
  481.  
  482.  
  483. let isChildProto = false;
  484. for (const sr of arr) {
  485. if (parent[key].prototype instanceof parent[sr]) {
  486. isChildProto = true;
  487. break;
  488. }
  489. }
  490.  
  491. if (isChildProto) return;
  492.  
  493. arr = arr.filter(sr => {
  494. if (parent[sr].prototype instanceof parent[key]) {
  495. return false;
  496. }
  497. return true;
  498. });
  499.  
  500. arr.push(key);
  501.  
  502. return arr;
  503.  
  504.  
  505. }
  506.  
  507. const getuG = (_yt_player) => {
  508.  
  509. const w = 'uG';
  510.  
  511. let arr = [];
  512.  
  513. for (const [k, v] of Object.entries(_yt_player)) {
  514.  
  515.  
  516. const p = typeof v === 'function' ? v.prototype : 0;
  517.  
  518. if (p
  519. && typeof p.createElement === 'function' && p.createElement.length === 2
  520. && typeof p.detach === 'function' && p.detach.length === 0
  521. && typeof p.update === 'function' && p.update.length === 1
  522. && typeof p.updateValue === 'function' && p.updateValue.length === 2
  523. ) {
  524.  
  525. arr = addProtoToArr(_yt_player, k, arr) || arr;
  526.  
  527. }
  528.  
  529. }
  530.  
  531.  
  532.  
  533.  
  534.  
  535. if (arr.length === 0) {
  536.  
  537. console.warn(`Key does not exist. [${w}]`);
  538. } else {
  539.  
  540. console.log(`[${w}]`, arr);
  541. return arr[0];
  542. }
  543.  
  544. }
  545.  
  546.  
  547.  
  548. // << if FIX_schedulerInstanceInstance_ >>
  549.  
  550. let idleFrom = Date.now() + 2700;
  551. let slowMode = false;
  552.  
  553. let ytEvented = false;
  554.  
  555.  
  556. function setupEvents() {
  557.  
  558. document.addEventListener('yt-navigate', () => {
  559.  
  560. ytEvented = true;
  561. slowMode = false;
  562. idleFrom = Date.now() + 2700;
  563.  
  564. });
  565. document.addEventListener('yt-navigate-start', () => {
  566.  
  567. ytEvented = true;
  568. slowMode = false;
  569. idleFrom = Date.now() + 2700;
  570.  
  571. });
  572.  
  573. document.addEventListener('yt-page-type-changed', () => {
  574.  
  575. ytEvented = true;
  576. slowMode = false;
  577. idleFrom = Date.now() + 1700;
  578.  
  579. });
  580.  
  581.  
  582. document.addEventListener('yt-player-updated', () => {
  583.  
  584. ytEvented = true;
  585. slowMode = false;
  586. idleFrom = Date.now() + 1700;
  587.  
  588. });
  589.  
  590.  
  591. document.addEventListener('yt-page-data-fetched', () => {
  592.  
  593. ytEvented = true;
  594. slowMode = false;
  595. idleFrom = Date.now() + 1700;
  596.  
  597. });
  598.  
  599. document.addEventListener('yt-navigate-finish', () => {
  600.  
  601. ytEvented = true;
  602. slowMode = false;
  603. let t = Date.now() + 700;
  604. if (t > idleFrom) idleFrom = t;
  605.  
  606. });
  607.  
  608. document.addEventListener('yt-page-data-updated', () => {
  609.  
  610. ytEvented = true;
  611. slowMode = false;
  612. let t = Date.now() + 700;
  613. if (t > idleFrom) idleFrom = t;
  614.  
  615. });
  616.  
  617. document.addEventListener('yt-watch-comments-ready', () => {
  618.  
  619. ytEvented = true;
  620. slowMode = false;
  621. let t = Date.now() + 700;
  622. if (t > idleFrom) idleFrom = t;
  623.  
  624. });
  625. }
  626.  
  627.  
  628. // << end >>
  629.  
  630. const cleanContext = async (win) => {
  631. const waitFn = requestAnimationFrame; // shall have been binded to window
  632. try {
  633. let mx = 16; // MAX TRIAL
  634. const frameId = 'vanillajs-iframe-v1';
  635. /** @type {HTMLIFrameElement | null} */
  636. let frame = document.getElementById(frameId);
  637. let removeIframeFn = null;
  638. if (!frame) {
  639. frame = document.createElement('iframe');
  640. frame.id = 'vanillajs-iframe-v1';
  641. frame.sandbox = 'allow-same-origin'; // script cannot be run inside iframe but API can be obtained from iframe
  642. let n = document.createElement('noscript'); // wrap into NOSCRPIT to avoid reflow (layouting)
  643. n.appendChild(frame);
  644. while (!document.documentElement && mx-- > 0) await new Promise(waitFn); // requestAnimationFrame here could get modified by YouTube engine
  645. const root = document.documentElement;
  646. root.appendChild(n); // throw error if root is null due to exceeding MAX TRIAL
  647. removeIframeFn = (setTimeout) => {
  648. const removeIframeOnDocumentReady = (e) => {
  649. e && win.removeEventListener("DOMContentLoaded", removeIframeOnDocumentReady, false);
  650. win = null;
  651. setTimeout(() => {
  652. n.remove();
  653. n = null;
  654. }, 200);
  655. }
  656. if (document.readyState !== 'loading') {
  657. removeIframeOnDocumentReady();
  658. } else {
  659. win.addEventListener("DOMContentLoaded", removeIframeOnDocumentReady, false);
  660. }
  661. }
  662. }
  663. while (!frame.contentWindow && mx-- > 0) await new Promise(waitFn);
  664. const fc = frame.contentWindow;
  665. if (!fc) throw "window is not found."; // throw error if root is null due to exceeding MAX TRIAL
  666. const { requestAnimationFrame, setTimeout, cancelAnimationFrame, setInterval, clearInterval, requestIdleCallback, getComputedStyle } = fc;
  667. const res = { requestAnimationFrame, setTimeout, cancelAnimationFrame, setInterval, clearInterval, requestIdleCallback, getComputedStyle };
  668. for (let k in res) res[k] = res[k].bind(win); // necessary
  669. if (removeIframeFn) Promise.resolve(res.setTimeout).then(removeIframeFn);
  670. res.animate = fc.HTMLElement.prototype.animate;
  671. return res;
  672. } catch (e) {
  673. console.warn(e);
  674. return null;
  675. }
  676. };
  677.  
  678.  
  679. const promiseForCustomYtElementsReady = new Promise(onRegistryReady);
  680.  
  681. cleanContext(window).then(__CONTEXT__ => {
  682. if (!__CONTEXT__) return null;
  683.  
  684. const { requestAnimationFrame, setTimeout, cancelAnimationFrame, setInterval, clearInterval, animate, requestIdleCallback, getComputedStyle } = __CONTEXT__;
  685.  
  686.  
  687.  
  688. let rafPromiseForTickers = null;
  689.  
  690. const getRafPromiseForTickers = () => rafPromiseForTickers || (rafPromiseForTickers = new Promise(resolve => {
  691. requestAnimationFrame(hRes => {
  692. rafPromiseForTickers = null;
  693. resolve(hRes);
  694. });
  695. }));
  696.  
  697. const getForegroundPromise = () => {
  698. if (document.visibilityState === 'visible') {
  699. return Promise.resolve();
  700. } else {
  701. return getRafPromiseForTickers();
  702. }
  703. };
  704.  
  705. NO_PRELOAD_GENERATE_204_BYPASS || promiseForCustomYtElementsReady.then(() => {
  706. setTimeout(() => {
  707. NO_PRELOAD_GENERATE_204_BYPASS = true;
  708. }, 1270);
  709. });
  710.  
  711. const promiseForTamerTimeout = new Promise(resolve => {
  712. promiseForCustomYtElementsReady.then(() => {
  713. customElements.whenDefined('ytd-app').then(() => {
  714. setTimeout(resolve, 1200);
  715. });
  716. });
  717. setTimeout(resolve, 3000);
  718. });
  719.  
  720.  
  721. class RAFHub {
  722. constructor() {
  723. /** @type {number} */
  724. this.startAt = 8170;
  725. /** @type {number} */
  726. this.counter = 0;
  727. /** @type {number} */
  728. this.rid = 0;
  729. /** @type {Map<number, FrameRequestCallback>} */
  730. this.funcs = new Map();
  731. const funcs = this.funcs;
  732. /** @type {FrameRequestCallback} */
  733. this.bCallback = this.mCallback.bind(this);
  734. this.pClear = () => funcs.clear();
  735. }
  736. /** @param {DOMHighResTimeStamp} highResTime */
  737. mCallback(highResTime) {
  738. this.rid = 0;
  739. Promise.resolve().then(this.pClear);
  740. this.funcs.forEach(func => Promise.resolve(highResTime).then(func).catch(console.warn));
  741. }
  742. /** @param {FrameRequestCallback} f */
  743. request(f) {
  744. if (this.counter > 1e9) this.counter = 9;
  745. let cid = this.startAt + (++this.counter);
  746. this.funcs.set(cid, f);
  747. if (this.rid === 0) this.rid = requestAnimationFrame(this.bCallback);
  748. return cid;
  749. }
  750. /** @param {number} cid */
  751. cancel(cid) {
  752. cid = +cid;
  753. if (cid > 0) {
  754. if (cid <= this.startAt) {
  755. return cancelAnimationFrame(cid);
  756. }
  757. if (this.rid > 0) {
  758. this.funcs.delete(cid);
  759. if (this.funcs.size === 0) {
  760. cancelAnimationFrame(this.rid);
  761. this.rid = 0;
  762. }
  763. }
  764. }
  765. }
  766. }
  767.  
  768.  
  769.  
  770. NATIVE_CANVAS_ANIMATION && (() => {
  771.  
  772. HTMLCanvasElement.prototype.animate = animate;
  773.  
  774. let cid = setInterval(() => {
  775. HTMLCanvasElement.prototype.animate = animate;
  776. }, 1);
  777.  
  778. promiseForTamerTimeout.then(() => {
  779. clearInterval(cid)
  780. });
  781.  
  782. })();
  783.  
  784. CHANGE_appendChild && (() => {
  785.  
  786. HTMLElement.prototype.appendChild73 = HTMLElement.prototype.appendChild;
  787. HTMLElement.prototype.appendChild = function (a) {
  788. if (!NO_PRELOAD_GENERATE_204_BYPASS && document.head === this) {
  789. for (let node = this.firstElementChild; node instanceof HTMLElement; node = node.nextElementSibling) {
  790. if (node.nodeName === 'LINK' && node.rel === 'preload' && node.as === 'fetch' && !node.__m848__) {
  791. node.__m848__ = 1;
  792. node.rel = 'prefetch'; // see https://github.com/GoogleChromeLabs/quicklink
  793. }
  794. }
  795. }
  796. if (a instanceof DocumentFragment) {
  797. if (a.firstElementChild === null) return a;
  798. }
  799. return this.appendChild73.apply(this, arguments)
  800. }
  801.  
  802.  
  803. })();
  804.  
  805.  
  806. FIX_schedulerInstanceInstance_ && (async () => {
  807.  
  808.  
  809. const schedulerInstanceInstance_ = await new Promise(resolve => {
  810.  
  811. let cid = setInterval(() => {
  812. let t = (((window || 0).ytglobal || 0).schedulerInstanceInstance_ || 0);
  813. if (t) {
  814.  
  815. clearInterval(cid);
  816. resolve(t);
  817. }
  818. }, 1);
  819. promiseForTamerTimeout.then(() => {
  820. resolve(null)
  821. });
  822. });
  823.  
  824. if (!schedulerInstanceInstance_) return;
  825.  
  826.  
  827. if (!ytEvented) {
  828. idleFrom = Date.now() + 2700;
  829. slowMode = false; // integrity
  830. }
  831.  
  832. const checkOK = typeof schedulerInstanceInstance_.start === 'function' && !schedulerInstanceInstance_.start991 && !schedulerInstanceInstance_.stop && !schedulerInstanceInstance_.cancel && !schedulerInstanceInstance_.terminate && !schedulerInstanceInstance_.interupt;
  833. if (checkOK) {
  834.  
  835. schedulerInstanceInstance_.start991 = schedulerInstanceInstance_.start;
  836.  
  837. let requestingFn = null;
  838. let requestingArgs = null;
  839. let requestingDT = 0;
  840.  
  841. // let timerId = null;
  842. const entries = [];
  843. const f = function () {
  844. requestingFn = this.fn;
  845. requestingArgs = [...arguments];
  846. requestingDT = Date.now();
  847. entries.push({
  848. fn: requestingFn,
  849. args: requestingArgs,
  850. t: requestingDT
  851. });
  852. // if (Date.now() < idleFrom) {
  853. // timerId = this.fn.apply(window, arguments);
  854. // } else {
  855. // timerId = this.fn.apply(window, arguments);
  856.  
  857. // }
  858. // timerId = 12377;
  859. return 12377;
  860. }
  861.  
  862.  
  863. const fakeFns = [
  864. f.bind({ fn: requestAnimationFrame }),
  865. f.bind({ fn: setInterval }),
  866. f.bind({ fn: setTimeout }),
  867. f.bind({ fn: requestIdleCallback })
  868. ]
  869.  
  870.  
  871.  
  872.  
  873. let timerResolve = null;
  874. setInterval(() => {
  875. timerResolve && timerResolve();
  876. timerResolve = null;
  877. if (!slowMode && Date.now() > idleFrom) slowMode = true;
  878. }, 250);
  879.  
  880. let mzt = 0;
  881.  
  882. let fnSelectorProp = null;
  883.  
  884. schedulerInstanceInstance_.start = function () {
  885.  
  886. const mk1 = window.requestAnimationFrame
  887. const mk2 = window.setInterval
  888. const mk3 = window.setTimeout
  889. const mk4 = window.requestIdleCallback
  890.  
  891. const tThis = this['$$12378$$'] || this;
  892.  
  893.  
  894. window.requestAnimationFrame = fakeFns[0]
  895. window.setInterval = fakeFns[1]
  896. window.setTimeout = fakeFns[2]
  897. window.requestIdleCallback = fakeFns[3]
  898.  
  899. fnSelectorProp = null;
  900.  
  901.  
  902. tThis.start991.call(new Proxy(tThis, {
  903. get(target, prop, receiver) {
  904. if (prop === '$$12377$$') return true;
  905. if (prop === '$$12378$$') return target;
  906.  
  907. // console.log('get',prop)
  908. return target[prop]
  909. },
  910. set(target, prop, value, receiver) {
  911. // console.log('set', prop, value)
  912.  
  913.  
  914. if (value >= 1 && value <= 4) fnSelectorProp = prop;
  915. if (value === 12377 && fnSelectorProp) {
  916.  
  917. const originalSelection = target[fnSelectorProp];
  918. const timerIdProp = prop;
  919.  
  920. /*
  921.  
  922.  
  923. case 1:
  924. var a = this.K;
  925. this.g = this.I ? window.requestIdleCallback(a, {
  926. timeout: 3E3
  927. }) : window.setTimeout(a, ma);
  928. break;
  929. case 2:
  930. this.g = window.setTimeout(this.M, this.N);
  931. break;
  932. case 3:
  933. this.g = window.requestAnimationFrame(this.L);
  934. break;
  935. case 4:
  936. this.g = window.setTimeout(this.J, 0)
  937. }
  938.  
  939. */
  940.  
  941. const doForegroundSlowMode = () => {
  942.  
  943. const tir = ++mzt;
  944. const f = requestingArgs[0];
  945.  
  946.  
  947. getForegroundPromise().then(() => {
  948.  
  949.  
  950. new Promise(r => {
  951. timerResolve = r
  952. }).then(() => {
  953. if (target[timerIdProp] === -tir) f();
  954. });
  955.  
  956. })
  957.  
  958. target[fnSelectorProp] = 931;
  959. target[prop] = -tir;
  960. }
  961.  
  962. if (target[fnSelectorProp] === 2 && requestingFn === setTimeout) {
  963. if (slowMode && !(requestingArgs[1] > 250)) {
  964.  
  965. doForegroundSlowMode();
  966.  
  967. } else {
  968. target[prop] = setTimeout.apply(window, requestingArgs);
  969.  
  970. }
  971.  
  972. } else if (target[fnSelectorProp] === 3 && requestingFn === requestAnimationFrame) {
  973.  
  974. if (slowMode) {
  975.  
  976. doForegroundSlowMode();
  977.  
  978. } else {
  979. target[prop] = requestAnimationFrame.apply(window, requestingArgs);
  980. }
  981.  
  982.  
  983. } else if (target[fnSelectorProp] === 4 && requestingFn === setTimeout && !requestingArgs[1]) {
  984.  
  985. const f = requestingArgs[0];
  986. const tir = ++mzt;
  987. Promise.resolve().then(() => {
  988. if (target[timerIdProp] === -tir) f();
  989. });
  990. target[fnSelectorProp] = 930;
  991. target[prop] = -tir;
  992.  
  993. } else if (target[fnSelectorProp] === 1 && (requestingFn === requestIdleCallback || requestingFn === setTimeout)) {
  994.  
  995. doForegroundSlowMode();
  996.  
  997. } else {
  998. // target[prop] = timerId;
  999. target[fnSelectorProp] = 0;
  1000. target[prop] = 0;
  1001. }
  1002.  
  1003. // *****
  1004. // console.log('[[set]]', slowMode , prop, value, `fnSelectorProp: ${originalSelection} -> ${target[fnSelectorProp]}`)
  1005. } else {
  1006.  
  1007. target[prop] = value;
  1008. }
  1009. // console.log('set',prop,value)
  1010. return true;
  1011. }
  1012. }));
  1013.  
  1014. fnSelectorProp = null;
  1015.  
  1016.  
  1017. window.requestAnimationFrame = mk1;
  1018. window.setInterval = mk2
  1019. window.setTimeout = mk3
  1020. window.requestIdleCallback = mk4;
  1021.  
  1022.  
  1023.  
  1024. }
  1025.  
  1026. schedulerInstanceInstance_.start.toString = function () {
  1027. return schedulerInstanceInstance_.start991.toString();
  1028. }
  1029.  
  1030. // const funcNames = [...(schedulerInstanceInstance_.start + "").matchAll(/[\(,]this\.(\w{1,2})[,\)]/g)].map(e => e[1]).map(prop => ({
  1031. // prop,
  1032. // value: schedulerInstanceInstance_[prop],
  1033. // type: typeof schedulerInstanceInstance_[prop]
  1034.  
  1035. // }));
  1036. // console.log('fcc', funcNames)
  1037.  
  1038.  
  1039.  
  1040.  
  1041. }
  1042. })();
  1043.  
  1044.  
  1045. FIX_yt_player && (async () => {
  1046.  
  1047.  
  1048.  
  1049. const rafHub = new RAFHub();
  1050.  
  1051.  
  1052. const _yt_player = await new Promise(resolve => {
  1053.  
  1054. let cid = setInterval(() => {
  1055. let t = (((window || 0)._yt_player || 0) || 0);
  1056. if (t) {
  1057.  
  1058. clearInterval(cid);
  1059. resolve(t);
  1060. }
  1061. }, 1);
  1062.  
  1063. promiseForTamerTimeout.then(() => {
  1064. resolve(null)
  1065. });
  1066.  
  1067. });
  1068.  
  1069.  
  1070.  
  1071. if (!_yt_player || typeof _yt_player !== 'object') return;
  1072.  
  1073.  
  1074.  
  1075. let keyZq = getZq(_yt_player);
  1076. let keyVG = getVG(_yt_player);
  1077. let buildVG = _yt_player[keyVG];
  1078. let u = new buildVG({
  1079. api: {},
  1080. element: document.createElement('noscript'),
  1081. api: {},
  1082. hide: () => { }
  1083. }, 250);
  1084. const timeDelayConstructor = u.delay.constructor; // g.br
  1085. // console.log(keyVG, u)
  1086. // buildVG.prototype.show = function(){}
  1087. // _yt_player[keyZq] = g.k
  1088.  
  1089. if (!keyZq) return;
  1090.  
  1091.  
  1092. const g = _yt_player
  1093. let k = keyZq
  1094.  
  1095. const gk = g[k];
  1096. if (typeof gk !== 'function') return;
  1097.  
  1098. let dummyObject = new gk;
  1099. let nilFunc = () => { };
  1100.  
  1101. let nilObj = {};
  1102.  
  1103. // console.log(1111111111)
  1104.  
  1105. let keyBoolD = '';
  1106. let keyWindow = '';
  1107. let keyFuncC = '';
  1108. let keyCidj = '';
  1109.  
  1110. for (const [t, y] of Object.entries(dummyObject)) {
  1111. if (y instanceof Window) keyWindow = t;
  1112. }
  1113.  
  1114. const dummyObjectProxyHandler = {
  1115. get(target, prop) {
  1116. let v = target[prop]
  1117. if (v instanceof Window && !keyWindow) {
  1118. keyWindow = t;
  1119. }
  1120. let y = typeof v === 'function' ? nilFunc : typeof v === 'object' ? nilObj : v;
  1121. if (prop === keyWindow) y = {
  1122. requestAnimationFrame(f) {
  1123. return 3;
  1124. },
  1125. cancelAnimationFrame() {
  1126.  
  1127. }
  1128. }
  1129. if (!keyFuncC && typeof v === 'function' && !(prop in target.constructor.prototype)) {
  1130. keyFuncC = prop;
  1131. }
  1132. // console.log('[get]', prop, typeof target[prop])
  1133.  
  1134.  
  1135. return y;
  1136. },
  1137. set(target, prop, value) {
  1138.  
  1139. if (typeof value === 'boolean' && !keyBoolD) {
  1140. keyBoolD = prop;
  1141. }
  1142. if (typeof value === 'number' && !keyCidj && value >= 2) {
  1143. keyCidj = prop;
  1144. }
  1145.  
  1146. // console.log('[set]', prop, value)
  1147. target[prop] = value
  1148.  
  1149. return true;
  1150. }
  1151. };
  1152.  
  1153. dummyObject.start.call(new Proxy(dummyObject, dummyObjectProxyHandler))
  1154.  
  1155. /*
  1156. console.log({
  1157. keyBoolD,
  1158. keyFuncC,
  1159. keyWindow,
  1160. keyCidj
  1161. })
  1162.  
  1163. console.log( dummyObject[keyFuncC])
  1164.  
  1165.  
  1166. console.log(2222222222)
  1167. */
  1168.  
  1169.  
  1170.  
  1171.  
  1172. g[k].prototype.start = function () {
  1173. this.stop();
  1174. this[keyBoolD] = true;
  1175. this[keyCidj] = rafHub.request(this[keyFuncC]);
  1176. }
  1177. ;
  1178. g[k].prototype.stop = function () {
  1179. if (this.isActive() && this[keyCidj]) {
  1180. rafHub.cancel(this[keyCidj]);
  1181. }
  1182. this[keyCidj] = null
  1183. }
  1184.  
  1185.  
  1186. /*
  1187. g[k].start = function() {
  1188. this.stop();
  1189. this.D = true;
  1190. var a = requestAnimationFrame
  1191. , b = cancelAnimationFrame;
  1192. this.j = a.call(this.B, this.C)
  1193. }
  1194. ;
  1195. g[k].stop = function() {
  1196. if (this.isActive()) {
  1197. var a = requestAnimationFrame
  1198. , b = cancelAnimationFrame;
  1199. b.call(this.B, this.j)
  1200. }
  1201. this.j = null
  1202. }
  1203. */
  1204.  
  1205.  
  1206.  
  1207. const keyzo = PERF_471489_ ? getzo(_yt_player) : null;
  1208.  
  1209. if (keyzo) {
  1210.  
  1211. k = keyzo
  1212.  
  1213. const setCSSProp = (() => {
  1214.  
  1215. let animationPropCapable = false;
  1216. try {
  1217. const propName = "--ibxpf"
  1218. const value = 2;
  1219. const keyframes = [{
  1220. [propName]: value
  1221. }];
  1222. window.CSS.registerProperty({
  1223. name: "--ibxpf",
  1224. syntax: "<number>",
  1225. inherits: false,
  1226. initialValue: 1,
  1227. });
  1228. animationPropCapable = '1' === `${getComputedStyle(document.documentElement).getPropertyValue('--ibxpf')}`
  1229. } catch (e) { }
  1230.  
  1231. if (!animationPropCapable) {
  1232. return (element, cssProp, value) => {
  1233.  
  1234.  
  1235. element.style.setProperty(cssProp, value);
  1236.  
  1237. }
  1238. }
  1239.  
  1240. const propMaps = new Map();
  1241.  
  1242. function setCustomCSSProperty(element, propName, value) {
  1243. let wm = propMaps.get(propName);
  1244. if (!wm) {
  1245.  
  1246. try {
  1247. window.CSS.registerProperty({
  1248. name: propName,
  1249. syntax: "*",
  1250. inherits: false
  1251. });
  1252. } catch (e) {
  1253. console.warn(e);
  1254. }
  1255.  
  1256. propMaps.set(propName, (wm = new WeakMap()));
  1257. }
  1258.  
  1259. // Create the animation keyframes with the provided property and value
  1260. const keyframes = [{
  1261. [propName]: value
  1262. }];
  1263.  
  1264. let currentAnimation = wm.get(element);
  1265. if (currentAnimation) {
  1266.  
  1267. currentAnimation.effect.setKeyframes(keyframes);
  1268.  
  1269. } else {
  1270.  
  1271.  
  1272.  
  1273. // Set the animation on the element and immediately pause it
  1274. const animation = animate.call(element, keyframes, {
  1275. duration: 1, // Very short duration as we just want to set the value
  1276. fill: 'forwards',
  1277. iterationStart: 1,
  1278. iterations: 2,
  1279. direction: 'alternate'
  1280. });
  1281.  
  1282.  
  1283. // animation.currentTime = 1;
  1284. animation.pause();
  1285.  
  1286. wm.set(element, animation);
  1287.  
  1288.  
  1289. }
  1290.  
  1291. }
  1292.  
  1293. return setCustomCSSProperty;
  1294.  
  1295.  
  1296. })();
  1297.  
  1298.  
  1299. const attrUpdateFn = g[k];
  1300. g['$$original$$' + k] = attrUpdateFn;
  1301. g[k] = function (a, b, c) {
  1302.  
  1303. // console.log(140000, a, b, c);
  1304.  
  1305. let transformType = '';
  1306. let transformValue = 0;
  1307. let transformUnit = '';
  1308.  
  1309. let byPassDefaultFn = false;
  1310. if (b === "transform" && typeof c === 'string') {
  1311.  
  1312. byPassDefaultFn = true;
  1313.  
  1314. const aStyle = a.style;
  1315.  
  1316. // let beforeMq = aStyle.getPropertyValue('--mq-transform');
  1317. if (!(a instanceof HTMLElement)) return;
  1318. if (c.length === 0) {
  1319.  
  1320. } else if (c.startsWith('scalex(0.') || (c === 'scalex(0)' || c === 'scalex(1)')) {
  1321. let p = c.substring(7, c.length - 1);
  1322. let q = p.length >= 1 ? parseFloat(p) : -1;
  1323. if (q > -1e-5 && q < 1 + 1e-5) {
  1324. transformType = 'scalex'
  1325. transformValue = q;
  1326. transformUnit = '';
  1327. }
  1328.  
  1329.  
  1330. } else if (c.startsWith('translateX(') && c.endsWith('px)')) {
  1331.  
  1332. let p = c.substring(11, c.length - 3);
  1333. let q = p.length >= 1 ? parseFloat(p) : NaN;
  1334.  
  1335. if (typeof q === 'number' && !isNaN(q)) {
  1336. transformType = 'translateX'
  1337. transformValue = q;
  1338. transformUnit = 'px';
  1339. }
  1340.  
  1341.  
  1342. } else if (c.startsWith('scaley(0.') || (c === 'scaley(0)' || c === 'scaley(1)')) {
  1343. let p = c.substring(7, c.length - 1);
  1344. let q = p.length >= 1 ? parseFloat(p) : -1;
  1345. if (q > -1e-5 && q < 1 + 1e-5) {
  1346. transformType = 'scaley'
  1347. transformValue = q;
  1348. transformUnit = '';
  1349. }
  1350.  
  1351.  
  1352. } else if (c.startsWith('translateY(') && c.endsWith('px)')) {
  1353.  
  1354. let p = c.substring(11, c.length - 3);
  1355. let q = p.length >= 1 ? parseFloat(p) : NaN;
  1356.  
  1357. if (typeof q === 'number' && !isNaN(q)) {
  1358. transformType = 'translateY'
  1359. transformValue = q;
  1360. transformUnit = 'px';
  1361. }
  1362.  
  1363.  
  1364. }
  1365.  
  1366. if (transformType) {
  1367.  
  1368. if (transformType === 'scalex' || transformType === 'scaley') {
  1369.  
  1370. const q = transformValue;
  1371.  
  1372.  
  1373. /*
  1374.  
  1375. let vz = Math.round(steppingScaleN * q);
  1376. const customPropName = '--discrete-'+transformType
  1377.  
  1378. const currentValue = aStyle.getPropertyValue(customPropName);
  1379.  
  1380. const transform = (aStyle.transform || '');
  1381. const u = transform.includes(customPropName)
  1382. if (`${currentValue}` === `${vz}`) {
  1383. if (u) return;
  1384. }
  1385.  
  1386.  
  1387. setCSSProp(a,customPropName, vz);
  1388. // aStyle.setProperty(customPropName, vz)
  1389.  
  1390. let ck = '';
  1391.  
  1392. if (c.length === 9) ck = c;
  1393. else if (!u) ck = c.replace(/[.\d]+/, '0.5');
  1394.  
  1395. if (ck && beforeMq !== ck) {
  1396. aStyle.setProperty('--mq-transform', ck);
  1397. }
  1398.  
  1399. if (u) return;
  1400. c = `${transformType}(calc(var(--discrete-${transformType})/${steppingScaleN}))`;
  1401.  
  1402.  
  1403.  
  1404. */
  1405.  
  1406. const vz = +(Math.round(q * steppingScaleN) / steppingScaleN).toFixed(3);
  1407.  
  1408. c = `${transformType === 'scalex' ? 'scaleX' : 'scaleY'}(${vz})`
  1409. const cv = aStyle.transform;
  1410.  
  1411. // console.log(157, cv,c)
  1412.  
  1413. if (c === cv) return;
  1414. // console.log(257, cv,c)
  1415.  
  1416. aStyle.transform = c;
  1417.  
  1418. // return;
  1419.  
  1420. } else if (transformType === 'translateX' || transformType === 'translateY') {
  1421.  
  1422. const q = transformValue;
  1423.  
  1424. /*
  1425.  
  1426. let vz = q.toFixed(1);
  1427. const customPropName = '--discrete-'+transformType
  1428.  
  1429. const aStyle = a.style;
  1430. const currentValue = (aStyle.getPropertyValue(customPropName) || '').replace('px', '');
  1431.  
  1432.  
  1433. const transform = (aStyle.transform || '');
  1434. const u = transform.includes(customPropName)
  1435. if (parseFloat(currentValue).toFixed(1) === vz) {
  1436. if (u) return;
  1437. }
  1438.  
  1439. setCSSProp(a,customPropName, vz + 'px');
  1440. // aStyle.setProperty(customPropName, vz + 'px')
  1441.  
  1442. let ck = '';
  1443. if (c.length === 15) ck = c;
  1444. else if (!u) ck = c.replace(/[.\d]+/, '0.5');
  1445.  
  1446. if (ck && beforeMq !== ck) {
  1447. aStyle.setProperty('--mq-transform', ck);
  1448. }
  1449.  
  1450. if (u) return;
  1451. c = `${transformType}(var(--discrete-${transformType}))`;
  1452.  
  1453. */
  1454.  
  1455.  
  1456. const vz = +q.toFixed(1);
  1457.  
  1458. c = `${transformType}(${vz}${transformUnit})`
  1459. const cv = aStyle.transform;
  1460.  
  1461. // console.log(158, cv,c)
  1462.  
  1463. if (c === cv) return;
  1464. // console.log(258, cv,c)
  1465.  
  1466. aStyle.transform = c;
  1467.  
  1468. // return;
  1469.  
  1470. } else {
  1471. throw new Error();
  1472. }
  1473.  
  1474. } else {
  1475. // if(beforeMq) a.style.setProperty('--mq-transform', '');
  1476. const cv = aStyle.transform
  1477. if (!c && !cv) return;
  1478. else if (c === cv) return;
  1479. aStyle.transform = c;
  1480. // return;
  1481. }
  1482.  
  1483. } else if (b === "display") {
  1484.  
  1485. const cv = a.style.display;
  1486. if (!cv && !c) return;
  1487. if (cv === c) return;
  1488.  
  1489.  
  1490. } else if (b === "width") {
  1491.  
  1492. const cv = a.style.width;
  1493. if (!cv && !c) return;
  1494. if (cv === c) return;
  1495.  
  1496. }
  1497.  
  1498. // console.log(130000, a, b, c);
  1499.  
  1500. if (byPassDefaultFn) return;
  1501. return attrUpdateFn.call(this, a, b, c);
  1502. }
  1503.  
  1504.  
  1505. /*
  1506.  
  1507. g.zo = function(a, b, c) {
  1508. if ("string" === typeof b)
  1509. (b = yo(a, b)) && (a.style[b] = c);
  1510. else
  1511. for (var d in b) {
  1512. c = a;
  1513. var e = b[d]
  1514. , f = yo(c, d);
  1515. f && (c.style[f] = e)
  1516. }
  1517. }
  1518.  
  1519.  
  1520. */
  1521.  
  1522.  
  1523. }
  1524.  
  1525.  
  1526.  
  1527. const keyuG = PERF_471489_ ? getuG(_yt_player) : null;
  1528.  
  1529. if (keyuG) {
  1530.  
  1531. k = keyuG;
  1532.  
  1533. const gk = g[k];
  1534. const gkp = gk.prototype;
  1535.  
  1536.  
  1537. /** @type { Map<string, WeakMap<any, any>> } */
  1538. const ntLogs = new Map();
  1539.  
  1540. if (typeof gkp.updateValue === 'function' && gkp.updateValue.length === 2 && !gkp.updateValue31) {
  1541.  
  1542. gkp.updateValue31 = gkp.updateValue;
  1543. gkp.updateValue = function (a, b) {
  1544. if (typeof a !== 'string') return this.updateValue31(a, b);
  1545.  
  1546. const element = this.element;
  1547. if (!(element instanceof HTMLElement)) return this.updateValue31(a, b);
  1548.  
  1549. let ntLog = ntLogs.get(a);
  1550. if (!ntLog) ntLogs.set(a, (ntLog = new WeakMap()));
  1551.  
  1552. let cache = ntLog.get(element);
  1553. if (cache && cache.value === b) {
  1554. return;
  1555. }
  1556. if (!cache) {
  1557. this.__oldValueByUpdateValue__ = null;
  1558. ntLog.set(element, cache = { value: b });
  1559. } else {
  1560. this.__oldValueByUpdateValue__ = cache.value;
  1561. cache.value = b;
  1562. }
  1563.  
  1564.  
  1565. return this.updateValue31(a, b);
  1566. }
  1567.  
  1568.  
  1569. /*
  1570. g.k.update = function(a) {
  1571. for (var b = g.u(Object.keys(a)), c = b.next(); !c.done; c = b.next())
  1572. c = c.value,
  1573. this.updateValue(c, a[c])
  1574. }
  1575. ;
  1576. g.k.updateValue = function(a, b) {
  1577. (a = this.Td["{{" + a + "}}"]) && wG(this, a[0], a[1], b)
  1578. }
  1579. */
  1580.  
  1581.  
  1582. }
  1583.  
  1584.  
  1585. }
  1586.  
  1587.  
  1588.  
  1589.  
  1590. })();
  1591.  
  1592.  
  1593.  
  1594. FIX_Animation_n_timeline && (async () => {
  1595.  
  1596.  
  1597. const timeline = await new Promise(resolve => {
  1598.  
  1599. let cid = setInterval(() => {
  1600. let t = (((document || 0).timeline || 0) || 0);
  1601. if (t && typeof t._play === 'function') {
  1602.  
  1603. clearInterval(cid);
  1604. resolve(t);
  1605. }
  1606. }, 1);
  1607.  
  1608. promiseForTamerTimeout.then(() => {
  1609. resolve(null)
  1610. });
  1611.  
  1612. });
  1613.  
  1614.  
  1615. const Animation = await new Promise(resolve => {
  1616.  
  1617. let cid = setInterval(() => {
  1618. let t = (((window || 0).Animation || 0) || 0);
  1619. if (t && typeof t === 'function' && t.length === 2 && typeof t.prototype._updatePromises === 'function') {
  1620.  
  1621. clearInterval(cid);
  1622. resolve(t);
  1623. }
  1624. }, 1);
  1625.  
  1626. promiseForTamerTimeout.then(() => {
  1627. resolve(null)
  1628. });
  1629.  
  1630. });
  1631.  
  1632. if (!timeline) return;
  1633. if (!Animation) return;
  1634.  
  1635. const aniProto = Animation.prototype;
  1636.  
  1637. const getXroto = (x) => {
  1638. try {
  1639. return x.__proto__;
  1640. } catch (e) { }
  1641. return null;
  1642. }
  1643. const timProto = getXroto(timeline);
  1644. if (!timProto) return;
  1645. if (
  1646. (
  1647. typeof timProto.getAnimations === 'function' && typeof timProto.play === 'function' &&
  1648. typeof timProto._discardAnimations === 'function' && typeof timProto._play === 'function' &&
  1649. typeof timProto._updateAnimationsPromises === 'function' && !timProto.nofCQ &&
  1650. typeof aniProto._updatePromises === 'function' && !aniProto.nofYH
  1651. )
  1652.  
  1653. ) {
  1654.  
  1655. timProto.nofCQ = 1;
  1656. aniProto.nofYH = 1;
  1657.  
  1658. const originalAnimationsWithPromises = ((_updateAnimationsPromises) => {
  1659.  
  1660.  
  1661. /*
  1662. v.animationsWithPromises = v.animationsWithPromises.filter(function (c) {
  1663. return c._updatePromises();
  1664. });
  1665. */
  1666.  
  1667. const p = Array.prototype.filter;
  1668.  
  1669. let res = null;
  1670. Array.prototype.filter = function () {
  1671.  
  1672. res = this;
  1673. return this;
  1674.  
  1675. };
  1676.  
  1677. _updateAnimationsPromises.call({});
  1678.  
  1679. Array.prototype.filter = p;
  1680.  
  1681. if (res && typeof res.length === 'number') {
  1682. /** @type {any[]} */
  1683. const _res = res;
  1684. return _res;
  1685. }
  1686.  
  1687.  
  1688. return null;
  1689.  
  1690.  
  1691.  
  1692.  
  1693. })(timProto._updateAnimationsPromises);
  1694.  
  1695. if (!originalAnimationsWithPromises || typeof originalAnimationsWithPromises.length !== 'number') return;
  1696.  
  1697. // console.log('originalAnimationsWithPromises', originalAnimationsWithPromises)
  1698.  
  1699. aniProto._updatePromises31 = aniProto._updatePromises;
  1700.  
  1701. /*
  1702. aniProto._updatePromises = function(){
  1703. console.log('eff',this._oldPlayState, this.playState)
  1704. return this._updatePromises31.apply(this, arguments)
  1705. }
  1706. */
  1707.  
  1708. aniProto._updatePromises = function () {
  1709. var oldPlayState = this._oldPlayState;
  1710. var newPlayState = this.playState;
  1711. // console.log('ett', oldPlayState, newPlayState)
  1712. if (newPlayState !== oldPlayState) {
  1713. this._oldPlayState = newPlayState;
  1714. if (this._readyPromise) {
  1715. if ("idle" == newPlayState) {
  1716. this._rejectReadyPromise();
  1717. this._readyPromise = void 0;
  1718. } else if ("pending" == oldPlayState) {
  1719. this._resolveReadyPromise();
  1720. } else if ("pending" == newPlayState) {
  1721. this._readyPromise = void 0;
  1722. }
  1723. }
  1724. if (this._finishedPromise) {
  1725. if ("idle" == newPlayState) {
  1726. this._rejectFinishedPromise();
  1727. this._finishedPromise = void 0;
  1728. } else if ("finished" == newPlayState) {
  1729. this._resolveFinishedPromise();
  1730. } else if ("finished" == oldPlayState) {
  1731. this._finishedPromise = void 0;
  1732. }
  1733. }
  1734. }
  1735. return this._readyPromise || this._finishedPromise;
  1736. };
  1737.  
  1738.  
  1739. let restartWebAnimationsNextTickFlag = false;
  1740.  
  1741. const looperMethodT = () => {
  1742.  
  1743. const runnerFn = (hRes) => {
  1744. var b = timeline;
  1745. b.currentTime = hRes;
  1746. b._discardAnimations();
  1747. if (0 == b._animations.length) {
  1748. restartWebAnimationsNextTickFlag = false;
  1749. } else {
  1750. getRafPromiseForTickers().then(runnerFn);
  1751. }
  1752. }
  1753.  
  1754. const restartWebAnimationsNextTick = () => {
  1755. if (!restartWebAnimationsNextTickFlag) {
  1756. restartWebAnimationsNextTickFlag = true;
  1757. getRafPromiseForTickers().then(runnerFn);
  1758. }
  1759. }
  1760.  
  1761. return { restartWebAnimationsNextTick }
  1762. };
  1763.  
  1764.  
  1765. const looperMethodN = () => {
  1766.  
  1767. const acs = document.createElement('a-f');
  1768. acs.id = 'a-f';
  1769.  
  1770. const style = document.createElement('style');
  1771. style.textContent = `
  1772. @keyFrames aF1 {
  1773. 0% {
  1774. order: 0;
  1775. }
  1776. 100% {
  1777. order: 6;
  1778. }
  1779. }
  1780. #a-f[id] {
  1781. visibility: collapse !important;
  1782. position: fixed !important;
  1783. top: -100px !important;
  1784. left: -100px !important;
  1785. margin:0 !important;
  1786. padding:0 !important;
  1787. outline:0 !important;
  1788. border:0 !important;
  1789. z-index:-1 !important;
  1790. width: 0px !important;
  1791. height: 0px !important;
  1792. contain: strict !important;
  1793. pointer-events: none !important;
  1794. animation: 1ms steps(2) 0ms infinite alternate forwards running aF1 !important;
  1795. }
  1796. `;
  1797. (document.head || document.documentElement).appendChild(style);
  1798.  
  1799. document.documentElement.insertBefore(acs, document.documentElement.firstChild);
  1800.  
  1801. const _onanimationiteration = function (evt) {
  1802. const hRes = evt.timeStamp;
  1803. var b = timeline;
  1804. b.currentTime = hRes;
  1805. b._discardAnimations();
  1806. if (0 == b._animations.length) {
  1807. restartWebAnimationsNextTickFlag = false;
  1808. acs.onanimationiteration = null;
  1809. } else {
  1810. acs.onanimationiteration = _onanimationiteration;
  1811. }
  1812.  
  1813. }
  1814.  
  1815.  
  1816.  
  1817. const restartWebAnimationsNextTick = () => {
  1818. if (!restartWebAnimationsNextTickFlag) {
  1819. restartWebAnimationsNextTickFlag = true;
  1820. acs.onanimationiteration = _onanimationiteration;
  1821.  
  1822. }
  1823. }
  1824.  
  1825. return { restartWebAnimationsNextTick }
  1826. };
  1827.  
  1828.  
  1829.  
  1830. const { restartWebAnimationsNextTick } = ('onanimationiteration' in document.documentElement) ? looperMethodN() : looperMethodT();
  1831.  
  1832. timProto._play = function (c) {
  1833. c = new Animation(c, this);
  1834. this._animations.push(c);
  1835. restartWebAnimationsNextTick();
  1836. c._updatePromises();
  1837. c._animation.play();
  1838. c._updatePromises();
  1839. return c
  1840. }
  1841.  
  1842. const animationsWithPromisesMap = new Set(originalAnimationsWithPromises);
  1843. originalAnimationsWithPromises.length = 0;
  1844. originalAnimationsWithPromises.push = null;
  1845. originalAnimationsWithPromises.splice = null;
  1846. originalAnimationsWithPromises.slice = null;
  1847. originalAnimationsWithPromises.indexOf = null;
  1848. originalAnimationsWithPromises.unshift = null;
  1849. originalAnimationsWithPromises.shift = null;
  1850. originalAnimationsWithPromises.pop = null;
  1851. originalAnimationsWithPromises.filter = null;
  1852. originalAnimationsWithPromises.forEach = null;
  1853. originalAnimationsWithPromises.map = null;
  1854.  
  1855.  
  1856.  
  1857. const _updateAnimationsPromises = () => {
  1858. animationsWithPromisesMap.forEach(c => {
  1859. if (!c._updatePromises()) animationsWithPromisesMap.delete(c);
  1860. })
  1861. /*
  1862. v.animationsWithPromises = v.animationsWithPromises.filter(function (c) {
  1863. return c._updatePromises();
  1864. });
  1865. */
  1866. }
  1867.  
  1868. timProto._updateAnimationsPromises31 = timProto._updateAnimationsPromises;
  1869.  
  1870. timProto._updateAnimationsPromises = _updateAnimationsPromises;
  1871.  
  1872.  
  1873. let pdFinished = Object.getOwnPropertyDescriptor(aniProto, 'finished');
  1874. aniProto.__finished_native_get__ = pdFinished.get;
  1875. if (typeof pdFinished.get === 'function' && !pdFinished.set && pdFinished.configurable === true && pdFinished.enumerable === true) {
  1876.  
  1877.  
  1878. Object.defineProperty(aniProto, 'finished', {
  1879. get() {
  1880. this._finishedPromise || (!animationsWithPromisesMap.has(this) && animationsWithPromisesMap.add(this),
  1881. this._finishedPromise = new Promise((resolve, reject) => {
  1882. this._resolveFinishedPromise = function () {
  1883. resolve(this)
  1884. };
  1885. this._rejectFinishedPromise = function () {
  1886. reject({
  1887. type: DOMException.ABORT_ERR,
  1888. name: "AbortError"
  1889. })
  1890. };
  1891. }),
  1892. "finished" == this.playState && this._resolveFinishedPromise());
  1893. return this._finishedPromise
  1894. },
  1895. set: undefined,
  1896. enumerable: true,
  1897. configurable: true
  1898. });
  1899.  
  1900. }
  1901.  
  1902.  
  1903.  
  1904. let pdReady = Object.getOwnPropertyDescriptor(aniProto, 'ready');
  1905. aniProto.__ready_native_get__ = pdReady.get;
  1906. if (typeof pdReady.get === 'function' && !pdReady.set && pdReady.configurable === true && pdReady.enumerable === true) {
  1907.  
  1908. Object.defineProperty(aniProto, 'ready', {
  1909. get() {
  1910. this._readyPromise || (!animationsWithPromisesMap.has(this) && animationsWithPromisesMap.add(this),
  1911. this._readyPromise = new Promise((resolve, reject) => {
  1912. this._resolveReadyPromise = function () {
  1913. resolve(this)
  1914. };
  1915. this._rejectReadyPromise = function () {
  1916. reject({
  1917. type: DOMException.ABORT_ERR,
  1918. name: "AbortError"
  1919. })
  1920. };
  1921. }),
  1922. "pending" !== this.playState && this._resolveReadyPromise());
  1923. return this._readyPromise
  1924. },
  1925. set: undefined,
  1926. enumerable: true,
  1927. configurable: true
  1928. });
  1929.  
  1930. }
  1931.  
  1932.  
  1933.  
  1934.  
  1935. /*
  1936.  
  1937.  
  1938. function f(c) {
  1939. var b = v.timeline;
  1940. b.currentTime = c;
  1941. b._discardAnimations();
  1942. 0 == b._animations.length ? d = !1 : requestAnimationFrame(f)
  1943. }
  1944. var h = window.requestAnimationFrame;
  1945. window.requestAnimationFrame = function(c) {
  1946. return h(function(b) {
  1947. v.timeline._updateAnimationsPromises();
  1948. c(b);
  1949. v.timeline._updateAnimationsPromises()
  1950. })
  1951. }
  1952. ;
  1953. v.AnimationTimeline = function() {
  1954. this._animations = [];
  1955. this.currentTime = void 0
  1956. }
  1957. ;
  1958. v.AnimationTimeline.prototype = {
  1959. getAnimations: function() {
  1960. this._discardAnimations();
  1961. return this._animations.slice()
  1962. },
  1963. _updateAnimationsPromises: function() {
  1964. v.animationsWithPromises = v.animationsWithPromises.filter(function(c) {
  1965. return c._updatePromises()
  1966. })
  1967. },
  1968. _discardAnimations: function() {
  1969. this._updateAnimationsPromises();
  1970. this._animations = this._animations.filter(function(c) {
  1971. return "finished" != c.playState && "idle" != c.playState
  1972. })
  1973. },
  1974. _play: function(c) {
  1975. c = new v.Animation(c,this);
  1976. this._animations.push(c);
  1977. v.restartWebAnimationsNextTick();
  1978. c._updatePromises();
  1979. c._animation.play();
  1980. c._updatePromises();
  1981. return c
  1982. },
  1983. play: function(c) {
  1984. c && c.remove();
  1985. return this._play(c)
  1986. }
  1987. };
  1988. var d = !1;
  1989. v.restartWebAnimationsNextTick = function() {
  1990. d || (d = !0,
  1991. requestAnimationFrame(f))
  1992. }
  1993. ;
  1994. var a = new v.AnimationTimeline;
  1995. v.timeline = a;
  1996. try {
  1997. Object.defineProperty(window.document, "timeline", {
  1998. configurable: !0,
  1999. get: function() {
  2000. return a
  2001. }
  2002. })
  2003. } catch (c) {}
  2004. try {
  2005. window.document.timeline = a
  2006. } catch (c) {}
  2007.  
  2008. */
  2009.  
  2010.  
  2011.  
  2012. /*
  2013.  
  2014. var g = window.getComputedStyle;
  2015. Object.defineProperty(window, "getComputedStyle", {
  2016. configurable: !0,
  2017. enumerable: !0,
  2018. value: function() {
  2019. v.timeline._updateAnimationsPromises();
  2020. var e = g.apply(this, arguments);
  2021. h() && (e = g.apply(this, arguments));
  2022. v.timeline._updateAnimationsPromises();
  2023. return e
  2024. }
  2025. });
  2026.  
  2027. */
  2028.  
  2029.  
  2030.  
  2031.  
  2032. }
  2033.  
  2034.  
  2035.  
  2036.  
  2037. })();
  2038.  
  2039.  
  2040.  
  2041. });
  2042.  
  2043.  
  2044. setupEvents();
  2045.  
  2046.  
  2047.  
  2048. if (isMainWindow) {
  2049.  
  2050. console.groupCollapsed(
  2051. "%cYouTube JS Engine Tamer",
  2052. "background-color: #EDE43B ; color: #000 ; font-weight: bold ; padding: 4px ;"
  2053. );
  2054.  
  2055.  
  2056.  
  2057. console.log("Script is loaded.");
  2058. console.log("This script changes the core mechanisms of the YouTube JS engine.");
  2059.  
  2060. console.log("This script is experimental and subject to further changes.");
  2061.  
  2062. console.log("This might boost your YouTube performance.");
  2063.  
  2064. console.log("CAUTION: This might break your YouTube.");
  2065.  
  2066.  
  2067. if (prepareLogs.length >= 1) {
  2068. console.log(" =========================================================================== ");
  2069.  
  2070. for (const msg of prepareLogs) {
  2071. console.log(msg)
  2072. }
  2073.  
  2074. console.log(" =========================================================================== ");
  2075. }
  2076.  
  2077. console.groupEnd();
  2078.  
  2079. }
  2080.  
  2081.  
  2082. })();