YouTube JS Engine Tamer

To enhance YouTube performance by modifying YouTube JS Engine

目前為 2023-09-22 提交的版本,檢視 最新版本

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