YouTube 超快聊天

YouTube直播聊天的终极性能提升

当前为 2023-07-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Super Fast Chat
  3. // @version 0.10.1
  4. // @license MIT
  5. // @name:ja YouTube スーパーファーストチャット
  6. // @name:zh-TW YouTube 超快聊天
  7. // @name:zh-CN YouTube 超快聊天
  8. // @namespace UserScript
  9. // @match https://www.youtube.com/live_chat*
  10. // @author CY Fung
  11. // @require https://greasyfork.org/scripts/465819-api-for-customelements-in-youtube/code/API%20for%20CustomElements%20in%20YouTube.js?version=1215280
  12. // @run-at document-start
  13. // @grant none
  14. // @unwrap
  15. // @allFrames true
  16. // @inject-into page
  17. //
  18. // @description Ultimate Performance Boost for YouTube Live Chats
  19. // @description:ja YouTubeのライブチャットの究極のパフォーマンスブースト
  20. // @description:zh-TW YouTube直播聊天的終極性能提升
  21. // @description:zh-CN YouTube直播聊天的终极性能提升
  22. //
  23. // ==/UserScript==
  24.  
  25. ((__CONTEXT__) => {
  26. 'use strict';
  27.  
  28. const ENABLE_REDUCED_MAXITEMS_FOR_FLUSH = true;
  29. const MAX_ITEMS_FOR_TOTAL_DISPLAY = 90;
  30. const MAX_ITEMS_FOR_FULL_FLUSH = 25;
  31.  
  32. const ENABLE_NO_SMOOTH_TRANSFORM = true;
  33. // const ENABLE_CONTENT_HIDDEN = false;
  34. let ENABLE_FULL_RENDER_REQUIRED = false; // Chrome Only; Firefox Excluded
  35. const USE_OPTIMIZED_ON_SCROLL_ITEMS = true;
  36. const USE_WILL_CHANGE_CONTROLLER = false;
  37. const MODIFY_SCROLL_TO_BOTTOM = false; // NOT REQUIRED in the latest version // and affect atBottom logic
  38.  
  39.  
  40. let cssText1 = '';
  41. let cssText2 = '';
  42. let cssText3 = '';
  43. let cssText4 = '';
  44. let cssText5 = '';
  45. let cssText6 = '';
  46. let cssText7 = '';
  47.  
  48. function dr(s) {
  49. // reserved for future use
  50. return s;
  51. // return window.deWeakJS ? window.deWeakJS(s) : s;
  52. }
  53.  
  54.  
  55. if (ENABLE_NO_SMOOTH_TRANSFORM) {
  56.  
  57. cssText3 = `
  58.  
  59. #item-offset.style-scope.yt-live-chat-item-list-renderer > #items.style-scope.yt-live-chat-item-list-renderer {
  60. position: static !important;
  61. }
  62. `
  63. cssText4 =
  64.  
  65.  
  66. `
  67.  
  68.  
  69. /* optional */
  70. #item-offset.style-scope.yt-live-chat-item-list-renderer {
  71. height: auto !important;
  72. min-height: unset !important;
  73. }
  74.  
  75. #items.style-scope.yt-live-chat-item-list-renderer {
  76. transform: translateY(0px) !important;
  77. }
  78.  
  79. /* optional */
  80.  
  81. `
  82. }
  83.  
  84. if (1) {
  85. cssText5 = `
  86.  
  87.  
  88.  
  89. /* ------------------------------------------------------------------------------------------------------------- */
  90.  
  91. yt-live-chat-author-chip #chat-badges.yt-live-chat-author-chip, yt-live-chat-author-chip #chat-badges.yt-live-chat-author-chip yt-live-chat-author-badge-renderer, yt-live-chat-author-chip #chat-badges.yt-live-chat-author-chip yt-live-chat-author-badge-renderer #image, yt-live-chat-author-chip #chat-badges.yt-live-chat-author-chip yt-live-chat-author-badge-renderer #image img {
  92. contain: layout style;
  93. }
  94.  
  95. #items.style-scope.yt-live-chat-item-list-renderer {
  96. contain: layout paint style;
  97. }
  98.  
  99. #item-offset.style-scope.yt-live-chat-item-list-renderer {
  100. contain: style;
  101. }
  102.  
  103. #item-scroller.style-scope.yt-live-chat-item-list-renderer {
  104. contain: size style;
  105. }
  106.  
  107. #contents.style-scope.yt-live-chat-item-list-renderer, #chat.style-scope.yt-live-chat-renderer, img.style-scope.yt-img-shadow[width][height] {
  108. contain: size layout paint style;
  109. }
  110.  
  111. .style-scope.yt-live-chat-ticker-renderer[role="button"][aria-label], .style-scope.yt-live-chat-ticker-renderer[role="button"][aria-label] > #container {
  112. contain: layout paint style;
  113. }
  114.  
  115. yt-live-chat-text-message-renderer.style-scope.yt-live-chat-item-list-renderer, yt-live-chat-membership-item-renderer.style-scope.yt-live-chat-item-list-renderer, yt-live-chat-paid-message-renderer.style-scope.yt-live-chat-item-list-renderer, yt-live-chat-banner-manager.style-scope.yt-live-chat-item-list-renderer {
  116. contain: layout style;
  117. }
  118.  
  119. tp-yt-paper-tooltip[style*="inset"][role="tooltip"] {
  120. contain: layout paint style;
  121. }
  122.  
  123. /* ------------------------------------------------------------------------------------------------------------- */
  124.  
  125. `
  126. }
  127.  
  128. if (1) {
  129.  
  130. cssText6 = `
  131.  
  132.  
  133. yt-icon[icon="down_arrow"] > *, yt-icon-button#show-more > * {
  134. pointer-events: none !important;
  135. }
  136.  
  137. #continuations, #continuations * {
  138. contain: strict;
  139. position: fixed;
  140. top: 2px;
  141. height: 1px;
  142. width: 2px;
  143. height: 1px;
  144. visibility: collapse;
  145. }
  146.  
  147. yt-live-chat-renderer[has-action-panel-renderer] #show-more.yt-live-chat-item-list-renderer{
  148. top: 4px;
  149. transition-property: top;
  150. bottom: unset;
  151. }
  152.  
  153. yt-live-chat-renderer[has-action-panel-renderer] #show-more.yt-live-chat-item-list-renderer[disabled]{
  154. top: -42px;
  155. }
  156.  
  157. html #panel-pages.yt-live-chat-renderer > #input-panel.yt-live-chat-renderer:not(:empty) {
  158. --yt-live-chat-action-panel-top-border: none;
  159. }
  160.  
  161. html #panel-pages.yt-live-chat-renderer > #input-panel.yt-live-chat-renderer.iron-selected > *:first-child {
  162. border-top: 1px solid var(--yt-live-chat-panel-pages-border-color);
  163. }
  164.  
  165. html #panel-pages.yt-live-chat-renderer {
  166. border-top: 0;
  167. border-bottom: 0;
  168. }
  169.  
  170. #input-panel #picker-buttons yt-live-chat-icon-toggle-button-renderer#product-picker {
  171. /*
  172. overflow: hidden;
  173. contain: layout paint style;
  174. */
  175. contain: layout style;
  176. }
  177.  
  178. #chat.yt-live-chat-renderer ~ #panel-pages.yt-live-chat-renderer {
  179. overflow: visible;
  180. }
  181.  
  182.  
  183. `
  184. }
  185.  
  186. if (1) {
  187. cssText7 = `
  188.  
  189.  
  190. .ytp-contextmenu[class],
  191. .toggle-button.tp-yt-paper-toggle-button[class],
  192. .yt-spec-touch-feedback-shape__fill[class],
  193. .fill.yt-interaction[class],
  194. .ytp-videowall-still-info-content[class],
  195. .ytp-suggestion-image[class] {
  196. will-change: unset !important;
  197. }
  198.  
  199. img {
  200. content-visibility: visible !important;
  201. }
  202.  
  203. yt-img-shadow[height][width],
  204. yt-img-shadow {
  205. content-visibility: visible !important;
  206. }
  207.  
  208. yt-live-chat-item-list-renderer:not([allow-scroll]) #item-scroller.yt-live-chat-item-list-renderer {
  209. overflow-y: scroll;
  210. padding-right: 0;
  211. }
  212.  
  213. `
  214. }
  215.  
  216. function addCssElement() {
  217. let s = document.createElement('style')
  218. s.id = 'ewRvC';
  219. return s;
  220. }
  221.  
  222. const addCss = () => document.head.appendChild(dr(addCssElement())).textContent = `
  223.  
  224.  
  225. @supports (contain:layout paint style) and (content-visibility:auto) and (contain-intrinsic-size:auto var(--wsr94)) {
  226.  
  227. ${cssText1}
  228. }
  229.  
  230. @supports (contain:layout paint style) {
  231.  
  232. ${cssText2}
  233.  
  234.  
  235. ${cssText5}
  236.  
  237. }
  238.  
  239. @supports (color: var(--general)) {
  240.  
  241. ${cssText3}
  242.  
  243. ${cssText7}
  244.  
  245.  
  246. ${cssText4}
  247.  
  248. ${cssText6}
  249.  
  250. .no-anchor * {
  251. overflow-anchor: none;
  252. }
  253. .no-anchor > item-anchor {
  254. overflow-anchor: auto;
  255. }
  256.  
  257. item-anchor {
  258.  
  259. height:1px;
  260. width: 100%;
  261. transform: scaleY(0.00001);
  262. transform-origin:0 0;
  263. contain: strict;
  264. opacity:0;
  265. display:flex;
  266. position:relative;
  267. flex-shrink:0;
  268. flex-grow:0;
  269. margin-bottom:0;
  270. overflow:hidden;
  271. box-sizing:border-box;
  272. visibility: visible;
  273. content-visibility: visible;
  274. contain-intrinsic-size: auto 1px;
  275. pointer-events:none !important;
  276.  
  277. }
  278.  
  279. #item-scroller.style-scope.yt-live-chat-item-list-renderer[class] {
  280. overflow-anchor: initial !important;
  281. }
  282.  
  283. html item-anchor {
  284.  
  285. height: 1px;
  286. width: 1px;
  287. top:auto;
  288. left:auto;
  289. right:auto;
  290. bottom:auto;
  291. transform: translateY(-1px);
  292. position: absolute;
  293. z-index:-1;
  294.  
  295. }
  296.  
  297.  
  298.  
  299. @keyframes dontRenderAnimation {
  300. 0% {
  301. background-position-x: 3px;
  302. }
  303. 100% {
  304. background-position-x: 4px;
  305. }
  306. }
  307.  
  308. /*html[dont-render-enabled] */ .dont-render{
  309. visibility: collapse !important;
  310. transform: scale(0.01) !important;
  311. transform: scale(0.00001) !important;
  312. transform: scale(0.0000001) !important;
  313. transform-origin:0 0 !important;
  314. z-index:-1 !important;
  315. contain: strict !important;
  316. box-sizing: border-box !important;
  317.  
  318. height:1px !important;
  319. height:0.1px !important;
  320. height:0.01px !important;
  321. height:0.0001px !important;
  322. height:0.000001px !important;
  323.  
  324.  
  325. animation: dontRenderAnimation 1ms linear 80ms 1 normal forwards !important;
  326.  
  327. }
  328.  
  329.  
  330.  
  331. }
  332.  
  333. `;
  334.  
  335.  
  336. const { IntersectionObserver } = __CONTEXT__;
  337.  
  338. /** @type {globalThis.PromiseConstructor} */
  339. const Promise = (async () => { })().constructor; // YouTube hacks Promise in WaterFox Classic and "Promise.resolve(0)" nevers resolve.
  340.  
  341. if (!IntersectionObserver) return console.error("Your browser does not support IntersectionObserver.\nPlease upgrade to the latest version.")
  342.  
  343. const isContainSupport = CSS.supports('contain', 'layout paint style');
  344. if (!isContainSupport) {
  345. console.warn("Your browser does not support css property 'contain'.\nPlease upgrade to the latest version.".trim());
  346. } else {
  347.  
  348. ENABLE_FULL_RENDER_REQUIRED = true; // Chromium-based browsers
  349.  
  350. }
  351.  
  352.  
  353.  
  354.  
  355. const win = this || window;
  356.  
  357. // Create a unique key for the script and check if it is already running
  358. const hkey_script = 'mchbwnoasqph';
  359. if (win[hkey_script]) throw new Error('Duplicated Userscript Calling'); // avoid duplicated scripting
  360. win[hkey_script] = true;
  361.  
  362. const cleanContext = async (win) => {
  363. const waitFn = requestAnimationFrame; // shall have been binded to window
  364. try {
  365. const mx = 16; // MAX TRIAL
  366. const frame = document.createElement('iframe');
  367. frame.sandbox = 'allow-same-origin';
  368. const n = document.createElement('noscript'); // wrap into NOSCRPIT to avoid reflow (layouting)
  369. n.appendChild(frame);
  370. while (!document.documentElement && mx-- > 0) await new Promise(waitFn); // requestAnimationFrame here could get modified by YouTube engine
  371. const root = document.documentElement;
  372. root.appendChild(n); // throw error if root is null due to exceeding MAX TRIAL
  373. while (!frame.contentWindow && mx-- > 0) await new Promise(waitFn);
  374. const fc = frame.contentWindow;
  375. if (!fc) throw "window is not found."; // throw error if root is null due to exceeding MAX TRIAL
  376. const { requestAnimationFrame } = fc;
  377. const res = { requestAnimationFrame };
  378. for (let k in res) res[k] = res[k].bind(win); // necessary
  379. n.remove();
  380. return res;
  381. } catch (e) {
  382. console.warn(e);
  383. return null;
  384. }
  385. };
  386.  
  387. cleanContext(win).then(__CONTEXT__ => {
  388. if (!__CONTEXT__) return null;
  389.  
  390.  
  391. const { requestAnimationFrame } = __CONTEXT__;
  392.  
  393.  
  394. ENABLE_FULL_RENDER_REQUIRED && document.addEventListener('animationstart', (evt) => {
  395.  
  396. if (evt.animationName === 'dontRenderAnimation') {
  397. evt.target.classList.remove('dont-render');
  398. }
  399.  
  400. }, true);
  401.  
  402. ENABLE_FULL_RENDER_REQUIRED && (() => {
  403.  
  404. const f = (elm) => {
  405. if (elm && elm.nodeType === 1) {
  406. elm.classList.add('dont-render');
  407. }
  408. }
  409.  
  410. Node.prototype.__appendChild931__ = function (a) {
  411. a = dr(a);
  412. if (this.id === 'items' && this.classList.contains('yt-live-chat-item-list-renderer')) {
  413. if (a && a.nodeType === 1) f(a);
  414. else if (a instanceof DocumentFragment) {
  415. for (let n = a.firstChild; n; n = n.nextSibling) {
  416. f(n);
  417. }
  418. }
  419. }
  420. }
  421.  
  422. Node.prototype.__appendChild932__ = function () {
  423. this.__appendChild931__.apply(this, arguments);
  424. return Node.prototype.appendChild.apply(this, arguments)
  425. }
  426.  
  427. })();
  428.  
  429. // let delayedAppendParentWS = new WeakSet();
  430. // let delayedAppendOperations = [];
  431. // let commonAppendParentStackSet = new Set();
  432.  
  433. // let firstVisibleItemDetected = false; // deprecated
  434.  
  435. const sp7 = Symbol();
  436.  
  437.  
  438. let dt0 = Date.now() - 2000;
  439. const dateNow = () => Date.now() - dt0;
  440. // let lastScroll = 0;
  441. // let lastLShow = 0;
  442. let lastWheel = 0;
  443.  
  444. const proxyHelperFn = (dummy) => ({
  445.  
  446. get(target, prop) {
  447. return (prop in dummy) ? dummy[prop] : prop === sp7 ? target : target[prop];
  448. },
  449. set(target, prop, value) {
  450. if (!(prop in dummy)) {
  451. target[prop] = value;
  452. }
  453. return true;
  454. },
  455. has(target, prop) {
  456. return (prop in target)
  457. },
  458. deleteProperty(target, prop) {
  459. return true;
  460. },
  461. ownKeys(target) {
  462. return Object.keys(target);
  463. },
  464. defineProperty(target, key, descriptor) {
  465. return Object.defineProperty(target, key, descriptor);
  466. },
  467. getOwnPropertyDescriptor(target, key) {
  468. return Object.getOwnPropertyDescriptor(target, key);
  469. },
  470.  
  471. });
  472.  
  473. const tickerContainerSetAttribute = function (attrName, attrValue) { // ensure '14.30000001%'.toFixed(1)
  474.  
  475. let yd = (this.__dataHost || (this.inst || 0).__dataHost).__data;
  476.  
  477. if (arguments.length === 2 && attrName === 'style' && yd && attrValue) {
  478.  
  479. // let v = yd.containerStyle.privateDoNotAccessOrElseSafeStyleWrappedValue_;
  480. let v = `${attrValue}`;
  481. // conside a ticker is 101px width
  482. // 1% = 1.01px
  483. // 0.2% = 0.202px
  484.  
  485.  
  486. const ratio1 = (yd.ratio * 100);
  487. if (ratio1 > -1) { // avoid NaN
  488.  
  489. // countdownDurationMs
  490. // 600000 - 0.2% <1% = 6s> <0.2% = 1.2s>
  491. // 300000 - 0.5% <1% = 3s> <0.5% = 1.5s>
  492. // 150000 - 1% <1% = 1.5s>
  493. // 75000 - 2% <1% =0.75s > <2% = 1.5s>
  494. // 30000 - 5% <1% =0.3s > <5% = 1.5s>
  495.  
  496. // 99px * 5% = 4.95px
  497.  
  498. // 15000 - 10% <1% =0.15s > <10% = 1.5s>
  499.  
  500.  
  501.  
  502.  
  503. // 1% Duration
  504.  
  505. let ratio2 = ratio1;
  506.  
  507. const ydd = yd.data;
  508. const d1 = ydd.durationSec;
  509. const d2 = ydd.fullDurationSec;
  510.  
  511. if (d1 === d2 && d1 > 1) {
  512.  
  513. if (d1 > 400) ratio2 = Math.round(ratio2 * 5) / 5; // 0.2%
  514. else if (d1 > 200) ratio2 = Math.round(ratio2 * 2) / 2; // 0.5%
  515. else if (d1 > 100) ratio2 = Math.round(ratio2 * 1) / 1; // 1%
  516. else if (d1 > 50) ratio2 = Math.round(ratio2 * 0.5) / 0.5; // 2%
  517. else if (d1 > 25) ratio2 = Math.round(ratio2 * 0.2) / 0.2; // 5% (max => 99px * 5% = 4.95px)
  518. else ratio2 = Math.round(ratio2 * 0.2) / 0.2;
  519.  
  520. } else {
  521. ratio2 = Math.round(ratio2 * 5) / 5; // 0.2% (min)
  522. }
  523.  
  524. // ratio2 = Math.round(ratio2 * 5) / 5;
  525. ratio2 = ratio2.toFixed(1)
  526. v = v.replace(`${ratio1}%`, `${ratio2}%`).replace(`${ratio1}%`, `${ratio2}%`)
  527.  
  528. if (yd.__style_last__ === v) return;
  529. yd.__style_last__ = v;
  530. // do not consider any delay here.
  531. // it shall be inside the looping for all properties changes. all the css background ops are in the same microtask.
  532.  
  533. }
  534.  
  535. HTMLElement.prototype.setAttribute.call(dr(this), attrName, v);
  536.  
  537.  
  538. } else {
  539. HTMLElement.prototype.setAttribute.apply(dr(this), arguments);
  540. }
  541.  
  542. };
  543.  
  544. const fxOperator = (proto, propertyName) => {
  545. let propertyDescriptorGetter = null;
  546. try {
  547. propertyDescriptorGetter = Object.getOwnPropertyDescriptor(proto, propertyName).get;
  548. } catch (e) { }
  549. return typeof propertyDescriptorGetter === 'function' ? (e) => {
  550. try {
  551.  
  552. return propertyDescriptorGetter.call(dr(e))
  553. } catch (e) { }
  554. return e[propertyName];
  555. } : (e) => e[propertyName];
  556. };
  557.  
  558. const nodeParent = fxOperator(Node.prototype, 'parentNode');
  559. // const nFirstElem = fxOperator(HTMLElement.prototype, 'firstElementChild');
  560. const nPrevElem = fxOperator(HTMLElement.prototype, 'previousElementSibling');
  561. const nNextElem = fxOperator(HTMLElement.prototype, 'nextElementSibling');
  562. const nLastElem = fxOperator(HTMLElement.prototype, 'lastElementChild');
  563.  
  564.  
  565. /* globals WeakRef:false */
  566.  
  567. /** @type {(o: Object | null) => WeakRef | null} */
  568. const mWeakRef = typeof WeakRef === 'function' ? (o => o ? new WeakRef(o) : null) : (o => o || null); // typeof InvalidVar == 'undefined'
  569.  
  570. /** @type {(wr: Object | null) => Object | null} */
  571. const kRef = (wr => (wr && wr.deref) ? wr.deref() : wr);
  572.  
  573. const watchUserCSS = () => {
  574.  
  575. // if (!CSS.supports('contain-intrinsic-size', 'auto var(--wsr94)')) return;
  576.  
  577. const getElemFromWR = (nr) => {
  578. const n = kRef(nr);
  579. if (n && n.isConnected) return n;
  580. return null;
  581. }
  582.  
  583. const clearContentVisibilitySizing = () => {
  584. Promise.resolve().then(() => {
  585.  
  586. let btnShowMoreWR = mWeakRef(document.querySelector('#show-more[disabled]'));
  587.  
  588. let lastVisibleItemWR = null;
  589. for (const elm of document.querySelectorAll('[wSr93]')) {
  590. if (elm.getAttribute('wSr93') === 'visible') lastVisibleItemWR = mWeakRef(elm);
  591. elm.setAttribute('wSr93', '');
  592. // custom CSS property --wsr94 not working when attribute wSr93 removed
  593. }
  594. requestAnimationFrame(() => {
  595. const btnShowMore = getElemFromWR(btnShowMoreWR); btnShowMoreWR = null;
  596. if (btnShowMore) btnShowMore.click();
  597. else {
  598. // would not work if switch it frequently
  599. const lastVisibleItem = getElemFromWR(lastVisibleItemWR); lastVisibleItemWR = null;
  600. if (lastVisibleItem) {
  601.  
  602. Promise.resolve()
  603. .then(() => lastVisibleItem.scrollIntoView())
  604. .then(() => lastVisibleItem.scrollIntoView(false))
  605. .then(() => lastVisibleItem.scrollIntoView({ behavior: "instant", block: "end", inline: "nearest" }))
  606. .catch(e => { }) // break the chain when method not callable
  607.  
  608. }
  609. }
  610. })
  611.  
  612. })
  613.  
  614. }
  615.  
  616. const mutObserver = new MutationObserver((mutations) => {
  617. for (const mutation of mutations) {
  618. if ((mutation.addedNodes || 0).length >= 1) {
  619. for (const addedNode of mutation.addedNodes) {
  620. if (addedNode.nodeName === 'STYLE') {
  621. clearContentVisibilitySizing();
  622. return;
  623. }
  624. }
  625. }
  626. if ((mutation.removedNodes || 0).length >= 1) {
  627. for (const removedNode of mutation.removedNodes) {
  628. if (removedNode.nodeName === 'STYLE') {
  629. clearContentVisibilitySizing();
  630. return;
  631. }
  632. }
  633. }
  634. }
  635. });
  636.  
  637. mutObserver.observe(document.documentElement, {
  638. childList: true,
  639. subtree: false
  640. })
  641.  
  642. mutObserver.observe(document.head, {
  643. childList: true,
  644. subtree: false
  645. })
  646. mutObserver.observe(document.body, {
  647. childList: true,
  648. subtree: false
  649. });
  650.  
  651. }
  652.  
  653. const setupStyle = (m1, m2) => {
  654. if (!ENABLE_NO_SMOOTH_TRANSFORM) return;
  655.  
  656. const dummy1v = {
  657. transform: '',
  658. height: '',
  659. minHeight: '',
  660. paddingBottom: '',
  661. paddingTop: ''
  662. };
  663.  
  664. for (const k of ['toString', 'getPropertyPriority', 'getPropertyValue', 'item', 'removeProperty', 'setProperty']) {
  665. dummy1v[k] = ((k) => (function () { const style = this[sp7]; return style[k](...arguments); }))(k)
  666. }
  667.  
  668. const dummy1p = proxyHelperFn(dummy1v);
  669. const sp1v = new Proxy(m1.style, dummy1p);
  670. const sp2v = new Proxy(m2.style, dummy1p);
  671. Object.defineProperty(m1, 'style', { get() { return sp1v }, set() { }, enumerable: true, configurable: true });
  672. Object.defineProperty(m2, 'style', { get() { return sp2v }, set() { }, enumerable: true, configurable: true });
  673. m1.removeAttribute("style");
  674. m2.removeAttribute("style");
  675.  
  676. }
  677.  
  678.  
  679. class WillChangeController {
  680. constructor(itemScroller, willChangeValue) {
  681. this.element = itemScroller;
  682. this.counter = 0;
  683. this.active = false;
  684. this.willChangeValue = willChangeValue;
  685. }
  686.  
  687. beforeOper() {
  688. if (!this.active) {
  689. this.active = true;
  690. this.element.style.willChange = this.willChangeValue;
  691. }
  692. this.counter++;
  693. }
  694.  
  695. afterOper() {
  696. const c = this.counter;
  697. requestAnimationFrame(() => {
  698. if (c === this.counter) {
  699. this.active = false;
  700. this.element.style.willChange = '';
  701. }
  702. })
  703. }
  704.  
  705. release() {
  706. const element = this.element;
  707. this.element = null;
  708. this.counter = 1e16;
  709. this.active = false;
  710. try {
  711. element.style.willChange = '';
  712. } catch (e) { }
  713. }
  714.  
  715. }
  716.  
  717. customYtElements.onRegistryReady(() => {
  718.  
  719.  
  720. let scrollWillChangeController = null;
  721. let contensWillChangeController = null;
  722.  
  723. // as it links to event handling, it has to be injected using immediateCallback
  724. customYtElements.whenRegistered('yt-live-chat-item-list-renderer', (cProto) => {
  725.  
  726.  
  727. const mclp = cProto;
  728. console.assert(typeof mclp.scrollToBottom_ === 'function')
  729. console.assert(typeof mclp.scrollToBottom66_ !== 'function')
  730. console.assert(typeof mclp.flushActiveItems_ === 'function')
  731. console.assert(typeof mclp.flushActiveItems66_ !== 'function')
  732. console.assert(typeof mclp.async === 'function')
  733.  
  734.  
  735. mclp.__intermediate_delay__ = null;
  736.  
  737. let mzk = 0;
  738. let myk = 0;
  739. let mlf = 0;
  740. let myw = 0;
  741. let mzt = 0;
  742. let zarr = null;
  743.  
  744. if ((mclp.clearList || 0).length === 0) {
  745. mclp.clearList66 = mclp.clearList;
  746. mclp.clearList = function () {
  747. mzk++;
  748. myk++;
  749. mlf++;
  750. myw++;
  751. mzt++;
  752. zarr = null;
  753. this.__intermediate_delay__ = null;
  754. this.clearList66();
  755. };
  756. }
  757.  
  758. if ((mclp.scrollToBottom_ || 0).length === 0 && ENABLE_NO_SMOOTH_TRANSFORM && MODIFY_SCROLL_TO_BOTTOM) {
  759.  
  760. mclp.scrollToBottom66_ = mclp.scrollToBottom_;
  761.  
  762. mclp.scrollToBottom77_ = async function () {
  763. if (mzk > 1e9) mzk = 9;
  764. let tid = ++mzk;
  765.  
  766. await new Promise(requestAnimationFrame);
  767.  
  768. if (tid !== mzk) {
  769. return;
  770. }
  771.  
  772. const cnt = this;
  773. if (USE_WILL_CHANGE_CONTROLLER) {
  774. const itemScroller = cnt.itemScroller;
  775. if (scrollWillChangeController && scrollWillChangeController.element !== itemScroller) {
  776. scrollWillChangeController.release();
  777. scrollWillChangeController = null;
  778. }
  779. if (!scrollWillChangeController) scrollWillChangeController = new WillChangeController(itemScroller, 'scroll-position');
  780. }
  781. const wcController = scrollWillChangeController;
  782. wcController && wcController.beforeOper();
  783.  
  784. await Promise.resolve();
  785. cnt.scrollToBottom66_();
  786.  
  787. await Promise.resolve();
  788. wcController && wcController.afterOper();
  789.  
  790. }
  791.  
  792. mclp.scrollToBottom_ = function () {
  793. const cnt = this;
  794. cnt.__intermediate_delay__ = new Promise(resolve => {
  795. cnt.scrollToBottom77_().then(() => {
  796. resolve();
  797. });
  798. });
  799. }
  800. }
  801.  
  802.  
  803.  
  804. if ((mclp.showNewItems_ || 0).length === 0 && ENABLE_NO_SMOOTH_TRANSFORM) {
  805.  
  806.  
  807. mclp.showNewItems66_ = mclp.showNewItems_;
  808.  
  809. mclp.showNewItems77_ = async function () {
  810. if (myk > 1e9) myk = 9;
  811. let tid = ++myk;
  812.  
  813. await new Promise(requestAnimationFrame);
  814.  
  815. if (tid !== myk) {
  816. return;
  817. }
  818.  
  819. const cnt = this;
  820.  
  821. await Promise.resolve();
  822. cnt.showNewItems66_();
  823.  
  824. await Promise.resolve();
  825.  
  826. }
  827.  
  828. mclp.showNewItems_ = function () {
  829.  
  830. const cnt = this;
  831. cnt.__intermediate_delay__ = new Promise(resolve => {
  832. cnt.showNewItems77_().then(() => {
  833. resolve();
  834. });
  835. });
  836. }
  837.  
  838. }
  839.  
  840.  
  841.  
  842.  
  843. if ((mclp.flushActiveItems_ || 0).length === 0) {
  844.  
  845.  
  846. mclp.flushActiveItems66_ = mclp.flushActiveItems_;
  847.  
  848.  
  849. mclp.flushActiveItems77_ = async function () {
  850. try {
  851.  
  852. const cnt = this;
  853. if (mlf > 1e9) mlf = 9;
  854. let tid = ++mlf;
  855. if (tid !== mlf || cnt.isAttached === false || (cnt.hostElement || cnt).isConnected === false) return;
  856. if (!cnt.activeItems_ || cnt.activeItems_.length === 0) return;
  857.  
  858. // 4 times to maxItems to avoid frequent trimming.
  859. // 1 ... 10 ... 20 ... 30 ... 40 ... 50 ... 60 => 16 ... 20 ... 30 ..... 60 ... => 16
  860.  
  861. this.activeItems_.length > this.data.maxItemsToDisplay * 4 && this.data.maxItemsToDisplay > 4 && this.activeItems_.splice(0, this.activeItems_.length - this.data.maxItemsToDisplay - 1);
  862. if (cnt.canScrollToBottom_()) {
  863. let immd = cnt.__intermediate_delay__;
  864. await new Promise(requestAnimationFrame);
  865. if (tid !== mlf || cnt.isAttached === false || (cnt.hostElement || cnt).isConnected === false) return;
  866. if (!cnt.activeItems_ || cnt.activeItems_.length === 0) return;
  867.  
  868. const oMaxItemsToDisplay = this.data.maxItemsToDisplay;
  869. const reducedMaxItemsToDisplay = MAX_ITEMS_FOR_FULL_FLUSH;
  870. let changeMaxItemsToDisplay = false;
  871. if (ENABLE_REDUCED_MAXITEMS_FOR_FLUSH && this.activeItems_.length > this.data.maxItemsToDisplay) {
  872. if (this.data.maxItemsToDisplay > reducedMaxItemsToDisplay) {
  873. // as all the rendered chats are already "outdated"
  874. // all old chats shall remove and reduced number of few chats will be rendered
  875. // then restore to the original number
  876. changeMaxItemsToDisplay = true;
  877. this.data.maxItemsToDisplay = reducedMaxItemsToDisplay;
  878. }
  879. this.activeItems_.splice(0, this.activeItems_.length - this.data.maxItemsToDisplay);
  880. // console.log('changeMaxItemsToDisplay 01', this.data.maxItemsToDisplay, oMaxItemsToDisplay, reducedMaxItemsToDisplay)
  881. }
  882.  
  883. // it is found that it will render all stacked chats after switching back from background
  884. // to avoid lagging in popular livestream with massive chats, trim first before rendering.
  885. // this.activeItems_.length > this.data.maxItemsToDisplay && this.activeItems_.splice(0, this.activeItems_.length - this.data.maxItemsToDisplay);
  886.  
  887.  
  888.  
  889. const items = (cnt.$ || 0).items;
  890.  
  891. if (USE_WILL_CHANGE_CONTROLLER) {
  892. if (contensWillChangeController && contensWillChangeController.element !== items) {
  893. contensWillChangeController.release();
  894. contensWillChangeController = null;
  895. }
  896. if (!contensWillChangeController) contensWillChangeController = new WillChangeController(items, 'contents');
  897. }
  898. const wcController = contensWillChangeController;
  899. cnt.__intermediate_delay__ = Promise.all([cnt.__intermediate_delay__ || null, immd || null]);
  900. wcController && wcController.beforeOper();
  901. await Promise.resolve();
  902. const len1 = cnt.activeItems_.length;
  903. cnt.flushActiveItems66_();
  904. const len2 = cnt.activeItems_.length;
  905. let bAsync = len1 !== len2;
  906. await Promise.resolve();
  907. if (wcController) {
  908. if (bAsync) {
  909. cnt.async(() => {
  910. wcController.afterOper();
  911. });
  912. } else {
  913. wcController.afterOper();
  914. }
  915. }
  916. if (changeMaxItemsToDisplay) {
  917. if (this.data.maxItemsToDisplay === reducedMaxItemsToDisplay) {
  918. this.data.maxItemsToDisplay = oMaxItemsToDisplay;
  919. // console.log('changeMaxItemsToDisplay 02', this.data.maxItemsToDisplay, oMaxItemsToDisplay, reducedMaxItemsToDisplay)
  920. }
  921. }
  922.  
  923.  
  924. if (!ENABLE_NO_SMOOTH_TRANSFORM) {
  925.  
  926.  
  927. const ff = () => {
  928.  
  929. if (cnt.isAttached === false || (cnt.hostElement || cnt).isConnected === false) return;
  930. // if (tid !== mlf || cnt.isAttached === false || (cnt.hostElement || cnt).isConnected === false) return;
  931. if (!cnt.atBottom && cnt.allowScroll && cnt.canScrollToBottomDLW_ && cnt.canScrollToBottomDLW_()) {
  932. cnt.scrollToBottom_();
  933.  
  934. Promise.resolve().then(() => {
  935.  
  936. if (cnt.isAttached === false || (cnt.hostElement || cnt).isConnected === false) return;
  937. if (!cnt.canScrollToBottom_()) cnt.scrollToBottom_();
  938. })
  939.  
  940.  
  941. }
  942. }
  943.  
  944. ff();
  945.  
  946.  
  947. Promise.resolve().then(ff)
  948.  
  949. // requestAnimationFrame(ff);
  950. } else if (true) { // it might not be sticky to bottom when there is a full refresh.
  951.  
  952. Promise.resolve().then(() => {
  953.  
  954. if (!cnt.atBottom) {
  955. if (cnt.isAttached === false || (cnt.hostElement || cnt).isConnected === false) return;
  956. cnt.scrollToBottom_();
  957. }
  958.  
  959. }).then(() => { // do twice
  960.  
  961. if (!cnt.atBottom) {
  962. if (cnt.isAttached === false || (cnt.hostElement || cnt).isConnected === false) return;
  963. cnt.scrollToBottom_();
  964. }
  965.  
  966. })
  967. }
  968.  
  969.  
  970. return 1;
  971. } else {
  972. // cnt.flushActiveItems66_();
  973. // this.activeItems_.length > this.data.maxItemsToDisplay && this.activeItems_.splice(0, this.activeItems_.length - this.data.maxItemsToDisplay);
  974. return 2;
  975. }
  976. } catch (e) {
  977. console.warn(e);
  978. }
  979. }
  980.  
  981. mclp.flushActiveItems_ = function () {
  982. const cnt = this;
  983.  
  984. if (arguments.length !== 0 || !cnt.activeItems_ || !cnt.canScrollToBottom_) return cnt.flushActiveItems66_.apply(this, arguments);
  985.  
  986. if (cnt.activeItems_.length === 0) {
  987. cnt.__intermediate_delay__ = null;
  988. return;
  989. }
  990.  
  991. const cntData = ((cnt || 0).data || 0);
  992. if (cntData.maxItemsToDisplay > MAX_ITEMS_FOR_TOTAL_DISPLAY) cntData.maxItemsToDisplay = MAX_ITEMS_FOR_TOTAL_DISPLAY;
  993.  
  994. // ignore previous __intermediate_delay__ and create a new one
  995. cnt.__intermediate_delay__ = new Promise(resolve => {
  996. cnt.flushActiveItems77_().then(rt => {
  997. if (rt === 1) resolve(1); // success, scroll to bottom
  998. else if (rt === 2) resolve(2); // success, trim
  999. else resolve(-1); // skip
  1000. });
  1001. });
  1002.  
  1003. }
  1004. }
  1005.  
  1006. if ((mclp.async || 0).length === 2) {
  1007.  
  1008.  
  1009. mclp.async66 = mclp.async;
  1010. mclp.async = function () {
  1011. // ensure the previous operation is done
  1012. // .async is usually after the time consuming functions like flushActiveItems_ and scrollToBottom_
  1013.  
  1014. const stack = new Error().stack;
  1015. const isFlushAsync = stack.indexOf('flushActiveItems_') >= 0;
  1016. (this.__intermediate_delay__ || Promise.resolve()).then(rk => {
  1017. if (isFlushAsync) {
  1018. if (rk < 0) return;
  1019. if (rk === 2 && arguments[0] === this.maybeScrollToBottom_) return;
  1020. }
  1021. this.async66.apply(this, arguments);
  1022. });
  1023.  
  1024. }
  1025.  
  1026. }
  1027.  
  1028.  
  1029. if ((mclp.onScrollItems_ || 0).length === 1) {
  1030.  
  1031. mclp.onScrollItems66_ = mclp.onScrollItems_;
  1032. mclp.onScrollItems77_ = async function (evt) {
  1033. if (myw > 1e9) myw = 9;
  1034. let tid = ++myw;
  1035.  
  1036. await new Promise(requestAnimationFrame);
  1037.  
  1038. if (tid !== myw) {
  1039. return;
  1040. }
  1041.  
  1042. const cnt = this;
  1043.  
  1044. await Promise.resolve();
  1045. if (USE_OPTIMIZED_ON_SCROLL_ITEMS) {
  1046. await Promise.resolve().then(() => {
  1047. this.ytRendererBehavior.onScroll(evt);
  1048. }).then(() => {
  1049. if (dateNow() - lastWheel < 80) {
  1050. this.setAtBottom();
  1051. }
  1052. }).then(() => {
  1053. this.flushActiveItems_();
  1054. });
  1055. } else {
  1056. cnt.onScrollItems66_(evt);
  1057. }
  1058.  
  1059.  
  1060.  
  1061. await Promise.resolve();
  1062.  
  1063. }
  1064.  
  1065. mclp.onScrollItems_ = function (evt) {
  1066.  
  1067. const cnt = this;
  1068. cnt.__intermediate_delay__ = new Promise(resolve => {
  1069. cnt.onScrollItems77_(evt).then(() => {
  1070. resolve();
  1071. });
  1072. });
  1073. }
  1074. }
  1075.  
  1076. if ((mclp.handleLiveChatActions_ || 0).length === 1) {
  1077. mclp.handleLiveChatActions66_ = mclp.handleLiveChatActions_;
  1078.  
  1079. mclp.handleLiveChatActions77_ = async function (arr) {
  1080. if (typeof (arr || 0).length !== 'number') {
  1081. this.handleLiveChatActions66_(arr);
  1082. return;
  1083. }
  1084. if (mzt > 1e9) mzt = 9;
  1085. let tid = ++mzt;
  1086.  
  1087. if (zarr === null) zarr = arr;
  1088. else Array.prototype.push.apply(zarr, arr);
  1089. arr = null;
  1090.  
  1091. await new Promise(requestAnimationFrame);
  1092.  
  1093. if (tid !== mzt || zarr === null) {
  1094. return;
  1095. }
  1096.  
  1097. const carr = zarr;
  1098. zarr = null;
  1099.  
  1100. await Promise.resolve();
  1101. this.handleLiveChatActions66_(carr);
  1102. await Promise.resolve();
  1103.  
  1104. }
  1105.  
  1106. mclp.handleLiveChatActions_ = function (arr) {
  1107.  
  1108. const cnt = this;
  1109. cnt.__intermediate_delay__ = new Promise(resolve => {
  1110. cnt.handleLiveChatActions77_(arr).then(() => {
  1111. resolve();
  1112. });
  1113. });
  1114. }
  1115.  
  1116. }
  1117.  
  1118.  
  1119.  
  1120.  
  1121. })
  1122.  
  1123. });
  1124.  
  1125. const getProto = (element) => {
  1126. let proto = null;
  1127. if (element) {
  1128. if (element.inst) proto = element.inst.constructor.prototype;
  1129. else proto = element.constructor.prototype;
  1130. }
  1131. return proto || null;
  1132. }
  1133.  
  1134. let done = 0;
  1135. let main = async (q) => {
  1136. if (done) return;
  1137.  
  1138. if (!q) return;
  1139. let m1 = nodeParent(q);
  1140. let m2 = q;
  1141. if (!(m1 && m1.id === 'item-offset' && m2 && m2.id === 'items')) return;
  1142.  
  1143. done = 1;
  1144.  
  1145. // setTimeout(()=>{
  1146. // document.documentElement.setAttribute('dont-render-enabled','')
  1147. // },80)
  1148.  
  1149. Promise.resolve().then(watchUserCSS);
  1150.  
  1151. addCss();
  1152.  
  1153. setupStyle(m1, m2);
  1154.  
  1155. let lcRendererWR = null;
  1156.  
  1157. const lcRendererElm = () => {
  1158. let lcRenderer = kRef(lcRendererWR);
  1159. if (!lcRenderer || !lcRenderer.isConnected) {
  1160. lcRenderer = document.querySelector('yt-live-chat-item-list-renderer.yt-live-chat-renderer');
  1161. lcRendererWR = lcRenderer ? mWeakRef(lcRenderer) : null;
  1162. }
  1163. return lcRenderer
  1164. };
  1165.  
  1166. let hasFirstShowMore = false;
  1167.  
  1168. const visObserverFn = (entry) => {
  1169.  
  1170. const target = entry.target;
  1171. if (!target) return;
  1172. // if(target.classList.contains('dont-render')) return;
  1173. let isVisible = entry.isIntersecting === true && entry.intersectionRatio > 0.5;
  1174. // const h = entry.boundingClientRect.height;
  1175. /*
  1176. if (h < 16) { // wrong: 8 (padding/margin); standard: 32; test: 16 or 20
  1177. // e.g. under fullscreen. the element created but not rendered.
  1178. target.setAttribute('wSr93', '');
  1179. return;
  1180. }
  1181. */
  1182. if (isVisible) {
  1183. // target.style.setProperty('--wsr94', h + 'px');
  1184. target.setAttribute('wSr93', 'visible');
  1185. if (nNextElem(target) === null) {
  1186.  
  1187. // firstVisibleItemDetected = true;
  1188. /*
  1189. if (dateNow() - lastScroll < 80) {
  1190. lastLShow = 0;
  1191. lastScroll = 0;
  1192. Promise.resolve().then(clickShowMore);
  1193. } else {
  1194. lastLShow = dateNow();
  1195. }
  1196. */
  1197. // lastLShow = dateNow();
  1198. } else if (!hasFirstShowMore) { // should more than one item being visible
  1199. // implement inside visObserver to ensure there is sufficient delay
  1200. hasFirstShowMore = true;
  1201. requestAnimationFrame(() => {
  1202. // foreground page
  1203. // page visibly ready -> load the latest comments at initial loading
  1204. const lcRenderer = lcRendererElm();
  1205. if (lcRenderer) {
  1206. (lcRenderer.inst || lcRenderer).scrollToBottom_();
  1207. }
  1208. });
  1209. }
  1210. }
  1211. else if (target.getAttribute('wSr93') === 'visible') { // ignore target.getAttribute('wSr93') === '' to avoid wrong sizing
  1212.  
  1213. // target.style.setProperty('--wsr94', h + 'px');
  1214. target.setAttribute('wSr93', 'hidden');
  1215. } // note: might consider 0 < entry.intersectionRatio < 0.5 and target.getAttribute('wSr93') === '' <new last item>
  1216.  
  1217. }
  1218.  
  1219. const visObserver = new IntersectionObserver((entries) => {
  1220.  
  1221. for (const entry of entries) {
  1222.  
  1223. Promise.resolve(entry).then(visObserverFn);
  1224.  
  1225. }
  1226.  
  1227. }, {
  1228. // root: HTMLElement.prototype.closest.call(m2, '#item-scroller.yt-live-chat-item-list-renderer'), // nullable
  1229. rootMargin: "0px",
  1230. threshold: [0.05, 0.95],
  1231. });
  1232.  
  1233. //m2.style.visibility='';
  1234.  
  1235. const mutFn = (items) => {
  1236. for (let node = nLastElem(items); node !== null; node = nPrevElem(node)) {
  1237. if (node.hasAttribute('wSr93')) break;
  1238. node.setAttribute('wSr93', '');
  1239. visObserver.observe(node);
  1240. }
  1241. }
  1242.  
  1243. const mutObserver = new MutationObserver((mutations) => {
  1244. const items = (mutations[0] || 0).target;
  1245. if (!items) return;
  1246. mutFn(items);
  1247. });
  1248.  
  1249. const setupMutObserver = (m2) => {
  1250. mutObserver.disconnect();
  1251. mutObserver.takeRecords();
  1252. if (m2) {
  1253. if (typeof m2.__appendChild932__ === 'function') {
  1254. if (typeof m2.appendChild === 'function') m2.appendChild = m2.__appendChild932__;
  1255. if (typeof m2.__shady_native_appendChild === 'function') m2.__shady_native_appendChild = m2.__appendChild932__;
  1256. }
  1257. mutObserver.observe(m2, {
  1258. childList: true,
  1259. subtree: false
  1260. });
  1261. mutFn(m2);
  1262.  
  1263.  
  1264. if (ENABLE_NO_SMOOTH_TRANSFORM) {
  1265.  
  1266. let items = m2;
  1267. let addedAnchor = false;
  1268. if (items) {
  1269. if (items.nextElementSibling === null) {
  1270. items.classList.add('no-anchor');
  1271. addedAnchor = true;
  1272. items.parentNode.appendChild(dr(document.createElement('item-anchor')));
  1273. }
  1274. }
  1275.  
  1276.  
  1277.  
  1278. if (addedAnchor) {
  1279. nodeParent(m2).classList.add('no-anchor'); // required
  1280. }
  1281.  
  1282. }
  1283.  
  1284. // let div = document.createElement('div');
  1285. // div.id = 'qwcc';
  1286. // HTMLElement.prototype.appendChild.call(document.querySelector('yt-live-chat-item-list-renderer'), div )
  1287. // bufferRegion =div;
  1288.  
  1289. // buffObserver.takeRecords();
  1290. // buffObserver.disconnect();
  1291. // buffObserver.observe(div, {
  1292. // childList: true,
  1293. // subtree: false
  1294. // })
  1295.  
  1296.  
  1297.  
  1298. }
  1299. }
  1300.  
  1301. setupMutObserver(m2);
  1302.  
  1303. const mclp = getProto(document.querySelector('yt-live-chat-item-list-renderer'));
  1304. if (mclp && mclp.attached) {
  1305.  
  1306. mclp.attached66 = mclp.attached;
  1307. mclp.attached = function () {
  1308. let m2 = document.querySelector('#item-offset.style-scope.yt-live-chat-item-list-renderer > #items.style-scope.yt-live-chat-item-list-renderer');
  1309. let m1 = nodeParent(m2);
  1310. setupStyle(m1, m2);
  1311. setupMutObserver(m2);
  1312. return this.attached66();
  1313. }
  1314.  
  1315. mclp.detached66 = mclp.detached;
  1316. mclp.detached = function () {
  1317. setupMutObserver();
  1318. return this.detached66();
  1319. }
  1320.  
  1321. mclp.canScrollToBottomDLW_ = () => !(dateNow() - lastWheel < 80);
  1322.  
  1323. mclp.canScrollToBottom_ = function () {
  1324. return this.atBottom && this.allowScroll && this.canScrollToBottomDLW_();
  1325. }
  1326.  
  1327. if (ENABLE_NO_SMOOTH_TRANSFORM) {
  1328.  
  1329.  
  1330. mclp.isSmoothScrollEnabled_ = function () {
  1331. return false;
  1332. }
  1333.  
  1334.  
  1335.  
  1336. mclp.maybeResizeScrollContainer_ = function () {
  1337. //
  1338. }
  1339.  
  1340. mclp.refreshOffsetContainerHeight_ = function () {
  1341. //
  1342. }
  1343.  
  1344. mclp.smoothScroll_ = function () {
  1345. //
  1346. }
  1347.  
  1348. mclp.resetSmoothScroll_ = function () {
  1349. //
  1350. }
  1351. }
  1352.  
  1353. } else {
  1354. console.warn(`proto.attached for yt-live-chat-item-list-renderer is unavailable.`)
  1355. }
  1356.  
  1357.  
  1358. let scrollCount = 0;
  1359. document.addEventListener('scroll', (evt) => {
  1360. if (!evt || !evt.isTrusted) return;
  1361. // lastScroll = dateNow();
  1362. if (++scrollCount > 1e9) scrollCount = 9;
  1363. }, { passive: true, capture: true }); // support contain => support passive
  1364.  
  1365. let lastScrollCount = -1;
  1366. document.addEventListener('wheel', (evt) => {
  1367.  
  1368. if (!evt || !evt.isTrusted) return;
  1369. if (lastScrollCount === scrollCount) return;
  1370. lastScrollCount = scrollCount;
  1371. lastWheel = dateNow();
  1372.  
  1373. }, { passive: true, capture: true }); // support contain => support passive
  1374.  
  1375.  
  1376. const fp = (renderer) => {
  1377. const cnt = renderer.inst || renderer;
  1378. const container = (cnt.$ || 0).container;
  1379. if (container) {
  1380. container.setAttribute = tickerContainerSetAttribute;
  1381. }
  1382. };
  1383. const tags = ["yt-live-chat-ticker-paid-message-item-renderer", "yt-live-chat-ticker-paid-sticker-item-renderer",
  1384. "yt-live-chat-ticker-renderer", "yt-live-chat-ticker-sponsor-item-renderer"];
  1385. for (const tag of tags) {
  1386. const dummy = document.createElement(tag);
  1387.  
  1388. const cProto = getProto(dummy);
  1389. if (!cProto || !cProto.attached) {
  1390. console.warn(`proto.attached for ${tag} is unavailable.`)
  1391. continue;
  1392. }
  1393.  
  1394. const __updateTimeout__ = cProto.updateTimeout;
  1395.  
  1396. const canDoUpdateTimeoutReplacement = (() => {
  1397.  
  1398. if (dummy.countdownMs < 1 && dummy.lastCountdownTimeMs < 1 && dummy.countdownMs < 1 && dummy.countdownDurationMs < 1) {
  1399. return typeof dummy.setContainerWidth === 'function' && typeof dummy.slideDown === 'function';
  1400. }
  1401. return false;
  1402.  
  1403. })(dummy.inst || dummy) && ((__updateTimeout__ + "").indexOf("window.requestAnimationFrame(this.updateTimeout.bind(this))") > 0);
  1404.  
  1405.  
  1406.  
  1407. if (canDoUpdateTimeoutReplacement) {
  1408.  
  1409. const killTicker = (cnt) => {
  1410. if ("auto" === cnt.hostElement.style.width) cnt.setContainerWidth();
  1411. cnt.slideDown()
  1412. };
  1413.  
  1414. cProto.__ratio__ = null;
  1415. cProto._updateTimeout21_ = function (a) {
  1416.  
  1417. this.countdownMs -= (a - (this.lastCountdownTimeMs || 0));
  1418.  
  1419. let currentRatio = this.__ratio__;
  1420. let tdv = this.countdownMs / this.countdownDurationMs;
  1421. let nextRatio = Math.round(tdv * 500) / 500; // might generate 0.143000000001
  1422.  
  1423. const validCountDown = nextRatio > 0;
  1424. const isAttached = this.isAttached;
  1425.  
  1426. if (!validCountDown) {
  1427.  
  1428. this.lastCountdownTimeMs = null;
  1429.  
  1430. this.countdownMs = 0;
  1431. this.__ratio__ = null;
  1432. this.ratio = 0;
  1433.  
  1434. if (isAttached) Promise.resolve(this).then(killTicker);
  1435.  
  1436. } else if (!isAttached) {
  1437.  
  1438. this.lastCountdownTimeMs = null;
  1439.  
  1440. } else {
  1441.  
  1442. this.lastCountdownTimeMs = a;
  1443.  
  1444. const ratioDiff = currentRatio - nextRatio; // 0.144 - 0.142 = 0.002
  1445. if (ratioDiff < 0.001 && ratioDiff > -1e-6) {
  1446. // ratioDiff = 0
  1447.  
  1448. } else {
  1449. // ratioDiff = 0.002 / 0.004 ....
  1450. // OR ratioDiff < 0
  1451.  
  1452. this.__ratio__ = nextRatio;
  1453.  
  1454. this.ratio = nextRatio;
  1455. }
  1456.  
  1457. return true;
  1458. }
  1459.  
  1460. };
  1461.  
  1462. cProto._updateTimeout21_ = function (a) {
  1463. this.countdownMs = Math.max(0, this.countdownMs - (a - (this.lastCountdownTimeMs || 0)));
  1464. this.ratio = this.countdownMs / this.countdownDurationMs;
  1465. if (this.isAttached && this.countdownMs) {
  1466. this.lastCountdownTimeMs = a;
  1467. return true;
  1468. } else {
  1469. this.lastCountdownTimeMs = null;
  1470. if (this.isAttached) {
  1471. ("auto" === this.hostElement.style.width && this.setContainerWidth(), this.slideDown())
  1472. }
  1473. }
  1474. }
  1475.  
  1476.  
  1477. }
  1478.  
  1479. cProto.attached77 = cProto.attached
  1480.  
  1481. cProto.attached = function () {
  1482. fp(this.hostElement || this);
  1483. return this.attached77();
  1484. }
  1485.  
  1486. for (const elm of document.getElementsByTagName(tag)) {
  1487. fp(elm);
  1488. }
  1489.  
  1490.  
  1491. }
  1492.  
  1493. };
  1494.  
  1495.  
  1496. function onReady() {
  1497. let tmObserver = new MutationObserver(() => {
  1498.  
  1499. let p = document.getElementById('items'); // fast
  1500. if (!p) return;
  1501. let q = document.querySelector('#item-offset.style-scope.yt-live-chat-item-list-renderer > #items.style-scope.yt-live-chat-item-list-renderer'); // check
  1502.  
  1503. if (q) {
  1504. tmObserver.disconnect();
  1505. tmObserver.takeRecords();
  1506. tmObserver = null;
  1507. Promise.resolve(q).then((q) => {
  1508. // confirm Promis.resolve() is resolveable
  1509. // execute main without direct blocking
  1510. main(q);
  1511. })
  1512. }
  1513.  
  1514. });
  1515.  
  1516. tmObserver.observe(document.body || document.documentElement, {
  1517. childList: true,
  1518. subtree: true
  1519. });
  1520.  
  1521. }
  1522.  
  1523. Promise.resolve().then(() => {
  1524.  
  1525. if (document.readyState !== 'loading') {
  1526. onReady();
  1527. } else {
  1528. window.addEventListener("DOMContentLoaded", onReady, false);
  1529. }
  1530.  
  1531. });
  1532.  
  1533. });
  1534.  
  1535. })({ IntersectionObserver });