YouTube JS Engine Tamer

To enhance YouTube performance by modifying YouTube JS Engine

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

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