Tabview Youtube

Make comments and lists into tabs for YouTube Videos

当前为 2022-07-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Tabview Youtube
  3. // @name:en Tabview Youtube
  4. // @name:jp Tabview Youtube
  5. // @name:zh-tw Tabview Youtube
  6. // @name:zh-cn Tabview Youtube
  7. // @namespace http://tampermonkey.net/
  8. // @version 2.5.2
  9. // @license MIT https://github.com/cyfung1031/Tabview-Youtube/blob/main/LICENSE
  10. // @description Make comments and lists into tabs for YouTube Videos
  11. // @description:en Make comments and lists into tabs for YouTube Videos
  12. // @description:jp YouTube動画のコメントやリストなどをタブに作成します
  13. // @description:zh-tw 把Youtube Videos中的評論及影片清單製作成Tabs
  14. // @description:zh-cn 把Youtube Videos中的评论及视频列表制作成Tabs
  15. // @author CY Fung
  16. // @match https://www.youtube.com/*
  17. // @icon https://github.com/cyfung1031/Tabview-Youtube/raw/main/images/icon128p.png
  18. // @run-at document-start
  19. // @grant GM_getResourceText
  20. // @resource contentCSS https://raw.githubusercontent.com/cyfung1031/Tabview-Youtube/2a93739cb4387c755b20f1f51829a5f59cc21d74/css/style_content.css
  21. // @resource injectionJS1 https://raw.githubusercontent.com/cyfung1031/Tabview-Youtube/c2fd907cd32bbf372e89aa2ce1180b50a5b5359d/js/injection_script_1.js
  22. // @resource injectionFixAutoComplete https://raw.githubusercontent.com/cyfung1031/Tabview-Youtube/c2fd907cd32bbf372e89aa2ce1180b50a5b5359d/js/injectionScript_fixAutoComplete.js
  23. // @require https://cdnjs.cloudflare.com/ajax/libs/cash/8.1.0/cash.min.js
  24. // @noframes
  25. // ==/UserScript==
  26.  
  27. /* jshint esversion:6 */
  28.  
  29. function main($){
  30. // MIT License
  31. // https://github.com/cyfung1031/Tabview-Youtube/raw/main/js/content.js
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. -(function() {
  52.  
  53. function inIframe() {
  54. try {
  55. return window.self !== window.top;
  56. } catch (e) {
  57. return true;
  58. }
  59. }
  60.  
  61. if (inIframe()) return;
  62.  
  63. if (!$) return;
  64.  
  65. /**
  66. * SVG resources:
  67. * <div>Icons made by <a href="https://www.flaticon.com/authors/smashicons" title="Smashicons">Smashicons</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a></div>
  68. */
  69.  
  70. const scriptVersionForExternal = '2022/05/07';
  71.  
  72. const isMyScriptInChromeRuntime = () => typeof((((window || 0).chrome || 0).runtime || 0).getURL) == 'function'
  73.  
  74. const svgComments = `<path d="M40.068 13.465l-34.138.07A5.94 5.94 0 0 0 0 19.465v21.141a5.94 5.94 0 0 0 5.93 5.929H12v10a1
  75. 1 0 0 0 1.74.673l9.704-10.675 16.626-.068A5.94 5.94 0 0 0 46 40.536V19.395a5.94 5.94 0 0 0-5.932-5.93zM10 23.465h13a1
  76. 1 0 1 1 0 2H10a1 1 0 1 1 0-2zm26 14H10a1 1 0 1 1 0-2h26a1 1 0 1 1 0 2zm0-6H10a1 1 0 1 1 0-2h26a1 1 0 1 1 0
  77. 2zm18.072-28.93l-34.142-.07A5.94 5.94 0 0 0 14 8.395v3.124l26.064-.054c4.377 0 7.936 3.557 7.936 7.93v21.07.071
  78. 2.087l3.26 3.586a1 1 0 0 0 1.74-.673v-10h1.07A5.94 5.94 0 0 0 60 29.607V8.465a5.94 5.94 0 0 0-5.928-5.93z"/>`.trim();
  79.  
  80. const svgVideos = `<path d="M298 33c0-13.255-10.745-24-24-24H24C10.745 9 0 19.745 0 33v232c0 13.255 10.745 24 24
  81. 24h250c13.255 0 24-10.745 24-24V33zM91 39h43v34H91V39zM61 259H30v-34h31v34zm0-186H30V39h31v34zm73
  82. 186H91v-34h43v34zm-11-82.292v-55.417c0-8.25 5.868-11.302 12.77-6.783l40.237 26.272c6.902 4.519 6.958 11.914.056
  83. 16.434l-40.321 26.277c-6.902 4.52-12.742 1.467-12.742-6.783zM207 259h-43v-34h43v34zm0-186h-43V39h43v34zm61
  84. 186h-31v-34h31v34zm0-186h-31V39h31v34z"/>`.trim();
  85.  
  86. const svgInfo = `<path d="M11.812 0C5.289 0 0 5.289 0 11.812s5.289 11.813 11.812 11.813 11.813-5.29 11.813-11.813S18.335
  87. 0 11.812 0zm2.459 18.307c-.608.24-1.092.422-1.455.548s-.783.189-1.262.189c-.736
  88. 0-1.309-.18-1.717-.539s-.611-.814-.611-1.367c0-.215.015-.435.045-.659a8.23 8.23 0 0 1
  89. .147-.759l.761-2.688c.067-.258.125-.503.171-.731a3.24 3.24 0 0 0
  90. .068-.633c0-.342-.071-.582-.212-.717s-.412-.201-.813-.201c-.196
  91. 0-.398.029-.605.09s-.383.12-.529.176l.201-.828c.498-.203.975-.377 1.43-.521s.885-.218 1.29-.218c.731 0 1.295.178
  92. 1.692.53s.594.812.594 1.376c0 .117-.014.323-.041.617a4.13 4.13 0 0 1-.152.811l-.757
  93. 2.68c-.062.215-.117.461-.167.736s-.073.485-.073.626c0 .356.079.599.239.728s.435.194.827.194a2.4 2.4 0 0 0 .626-.097
  94. 3.56 3.56 0 0 0 .506-.17l-.203.827zm-.134-10.878c-.353.328-.778.492-1.275.492s-.924-.164-1.28-.492a1.57 1.57 0
  95. 0 1-.533-1.193c0-.465.18-.865.533-1.196a1.81 1.81 0 0 1 1.28-.497 1.79 1.79 0 0 1 1.275.497c.353.331.53.731.53
  96. 1.196s-.177.865-.53 1.193z"/>`.trim();
  97.  
  98. const svgPlayList = `<path d="M0 64h256v42.667H0zm0 85.333h256V192H0zm0 85.334h170.667v42.667H0zm341.333
  99. 0v-85.334h-42.666v85.334h-85.334v42.666h85.334v85.334h42.666v-85.334h85.334v-42.666z"/>`.trim();
  100.  
  101. // --- Youtube Video Testing :
  102. // Square Video: https://www.youtube.com/watch?v=L0RXVnRbFg8
  103. // Square Video: https://www.youtube.com/watch?v=bK_rKhMIotU
  104. // ---
  105.  
  106.  
  107. const LAYOUT_TWO_COLUMNS = 1;
  108. const LAYOUT_THEATER = 2;
  109. const LAYOUT_FULLSCREEN = 4;
  110. const LAYOUT_CHATROOM = 8;
  111. const LAYOUT_CHATROOM_COLLASPED = 16;
  112. const LAYOUT_TAB_EXPANDED = 32;
  113. const LAYOUT_ENGAGEMENT_PANEL_EXPAND = 64;
  114.  
  115. const mtoInterval1 = 40;
  116. const mtoInterval2 = 150;
  117.  
  118. let lastVideoURL = null; // for less attribute set only
  119.  
  120.  
  121.  
  122. const querySelectorFromAnchor = HTMLElement.prototype.querySelector; // nodeType==1 // since 2022/07/12
  123. const querySelectorAllFromAnchor = HTMLElement.prototype.querySelectorAll; // nodeType==1 // since 2022/07/12
  124. const closestDOM = HTMLElement.prototype.closest;
  125. const elementRemove = HTMLElement.prototype.remove;
  126. const elementInsertBefore = HTMLElement.prototype.insertBefore; // since 2022/07/12
  127. const elementContains = HTMLElement.prototype.contains; // since 2022/07/12
  128.  
  129.  
  130.  
  131. function scriptInjector(script_id, url_chrome, response_id){
  132.  
  133. let res={
  134. script_id: script_id,
  135. inject: function(){
  136.  
  137. let res = this, script_id = this.script_id;
  138.  
  139. if(!document.querySelector(`script#${script_id}`)){
  140. if (res.runtime_url){
  141. addScriptByURL(res.runtime_url).id = script_id;
  142. } else {
  143. addScript(`${res.injection_script}`).id = script_id;
  144. }
  145. }
  146.  
  147. }
  148. }
  149. res.script_id = script_id;
  150. if (isMyScriptInChromeRuntime()){
  151. res.runtime_url = window.chrome.runtime.getURL(url_chrome)
  152. } else {
  153. res.injection_script = GM_getResourceText(response_id);
  154. }
  155.  
  156. return res;
  157.  
  158.  
  159.  
  160. }
  161.  
  162. const script_inject_facp = scriptInjector(
  163. 'userscript-tabview-injection-facp',
  164. 'js/injectionScript_fixAutoComplete.js',
  165. "injectionFixAutoComplete");
  166. const script_inject_js1 = scriptInjector(
  167. 'userscript-tabview-injection-1',
  168. 'js/injection_script_1.js',
  169. "injectionJS1");
  170.  
  171.  
  172.  
  173. /** @type {WeakRef | null} */
  174. const WeakRef = window.WeakRef;
  175. const mWeakRef = WeakRef ? (o => o ? new WeakRef(o) : null) : (o => o || null);
  176. /** @type {(wr: Object | null) => Object | null} */
  177. const kRef = (wr => (wr && wr.deref) ? wr.deref() : wr);
  178.  
  179.  
  180.  
  181. const nonCryptoRandStr_base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  182. function nonCryptoRandStr(/** @type {number} */ n){
  183. const result = new Array(n);
  184. const baseStr = nonCryptoRandStr_base;
  185. const bLen = baseStr.length;
  186. for ( let i = 0; i < n; i++ ) {
  187. let t = null
  188. do {
  189. t = baseStr.charAt(Math.floor(Math.random() * bLen));
  190. }while(i===0 && 10-t>0)
  191. result[i]=t;
  192. }
  193. return result.join('');
  194. }
  195.  
  196. /**
  197. * Class definition
  198. * @property {string} propName - propriety description
  199. * ...
  200. */
  201. class ObserverRegister{
  202. constructor(/** @type {()=>MutationObserver | IntersectionObserver} */ observerCreator){
  203. let uid = null;
  204. const uidStore = ObserverRegister.uidStore;
  205. do{
  206. uid = nonCryptoRandStr(5);
  207. }while(uidStore[uid])
  208. uidStore[uid] = true;
  209. /**
  210. * uid is the unique string for each observer
  211. * @type {string}
  212. * @public
  213. */
  214. this.uid = uid;
  215. /**
  216. * observerCreator is a function to create the observer
  217. * @type {Function}
  218. * @public
  219. */
  220. this.observerCreator = observerCreator
  221. /**
  222. * observer is the actual observer object
  223. * @type {MutationObserver | IntersectionObserver}
  224. * @public
  225. */
  226. this.observer = null;
  227. }
  228. bindElement(/** @type {HTMLElement} */ elm, ...args){
  229. if(elm.hasAttribute(`o3r-${this.uid}`))return false;
  230. elm.setAttribute(`o3r-${this.uid}`,'')
  231. if(this.observer===null){
  232. this.observer=this.observerCreator();
  233. }
  234. this.observer.observe(elm, ...args)
  235. return true
  236. }
  237. clear(/** @type {boolean} */ flag){
  238. if(this.observer !== null){
  239. //const uidStore = ObserverRegister.uidStore;
  240. if(flag === true){
  241. this.observer.takeRecords();
  242. this.observer.disconnect();
  243. }
  244. this.observer = null;
  245. for(const s of document.querySelectorAll(`[o3r-${this.uid}]`)) s.removeAttribute(`o3r-${this.uid}`)
  246. //uidStore[this.uid]=false;
  247. //this.uid = null;
  248. }
  249. }
  250. }
  251.  
  252. /**
  253. * 'uidStore' is the static store of strings used.
  254. * @static
  255. */
  256. ObserverRegister.uidStore = {}; //backward compatible with FireFox 55.
  257.  
  258. const mtoMutation_watchFlexy = new ObserverRegister(()=>{
  259. return new MutationObserver(FP.mtoNavF)
  260. });
  261. const sa_wflexy = mtoMutation_watchFlexy.uid;
  262.  
  263. const mtoMutation_body = new ObserverRegister(()=>{
  264. return new MutationObserver(FP.mtoBodyF)
  265. });
  266. const sa_body = mtoMutation_body.uid;
  267.  
  268. const mtoFlexyAttr = new ObserverRegister(()=>{
  269. return new MutationObserver(mtf_attrFlexy)
  270. });
  271. const sa_flexyAttr = mtoFlexyAttr.uid;
  272.  
  273. const mtoVisibility_EngagementPanel = new ObserverRegister(()=>{
  274. return new MutationObserver(FP.mtf_attrEngagementPanel)
  275. });
  276. const sa_epanel = mtoVisibility_EngagementPanel.uid;
  277.  
  278. const mtoVisibility_Playlist = new ObserverRegister(()=>{
  279. return new AttributeMutationObserver({
  280. "hidden": FP.mtf_attrPlaylist
  281. })
  282. })
  283. const sa_playlist = mtoVisibility_Playlist.uid;
  284.  
  285. const mtoVisibility_Comments = new ObserverRegister(()=>{
  286. return new AttributeMutationObserver({
  287. "hidden": FP.mtf_attrComments
  288. })
  289. })
  290. const sa_comments = mtoVisibility_Comments.uid;
  291.  
  292.  
  293. const mtoVisibility_Chatroom = new ObserverRegister(()=>{
  294. return new AttributeMutationObserver({
  295. "collapsed": FP.mtf_attrChatroom
  296. })
  297. })
  298. const sa_chatroom = mtoVisibility_Chatroom.uid;
  299.  
  300.  
  301.  
  302.  
  303.  
  304. function tracer(key, cmp) {
  305. if (cmp > 0) return tracer[key] === cmp;
  306. return (tracer[key] = Date.now());
  307. }
  308.  
  309. function racer(key, f) {
  310. let now = Date.now();
  311. const kTime = `${key}$$1`
  312. let t = racer[kTime] || 0;
  313.  
  314. if (now < t) {
  315. const kCount = `${key}$$2`;
  316. racer[kCount] = (racer[kCount] || 0) + 1;
  317. if (racer[kCount] === 1) {
  318. let g = f;
  319. requestAnimationFrame(() => {
  320. racer[kCount] = 0;
  321. g();
  322. g = null;
  323. })
  324. }
  325. } else {
  326. racer[kTime] = now + 16;
  327. f();
  328. }
  329. }
  330.  
  331. class ScriptEF {
  332. constructor() {
  333. this._id = scriptEC;
  334. }
  335. isValid() {
  336. return this._id === scriptEC;
  337. }
  338. }
  339.  
  340. class Timeout {
  341.  
  342. set(f, d, repeatCount) {
  343. if (this.cid > 0) return;
  344. let sEF = new ScriptEF();
  345. if (repeatCount > 0) {
  346.  
  347. let rc = repeatCount;
  348. const g = () => {
  349. this.cid = 0;
  350. if (!sEF.isValid()) return;
  351. let res = f();
  352. if (--rc <= 0) return;
  353. if (res === true) this.cid = timeline.setTimeout(g, d);
  354. }
  355. g();
  356.  
  357. } else {
  358.  
  359. const g = () => {
  360. this.cid = 0;
  361. if (!sEF.isValid()) return;
  362. if (f() === true) this.cid = timeline.setTimeout(g, d);
  363. }
  364. this.cid = timeline.setTimeout(g, d);
  365. }
  366. }
  367.  
  368. clear() {
  369. if (this.cid > 0) timeline.clearTimeout(this.cid);
  370. }
  371.  
  372. isEmpty() {
  373. return !this.cid
  374. }
  375.  
  376.  
  377. }
  378.  
  379. class Mutex {
  380.  
  381. constructor() {
  382. this.p = Promise.resolve()
  383. }
  384.  
  385. lockWith(f) {
  386.  
  387. this.p = this.p.then(() => {
  388. return new Promise(f)
  389. }).catch(console.warn)
  390. }
  391.  
  392. }
  393.  
  394.  
  395.  
  396. function prettyElm(/** @type {Element} */ elm) {
  397. if (!elm || !elm.nodeName) return null;
  398. const eId = elm.id || null;
  399. const eClsName = elm.className || null;
  400. return [elm.nodeName.toLowerCase(), typeof eId == 'string' ? "#" + eId : '', typeof eClsName == 'string' ? '.' + eClsName.replace(/\s+/g, '.') : ''].join('').trim();
  401. }
  402.  
  403. function extractTextContent(/** @type {Node} */ elm) {
  404. return elm.textContent.replace(/\s+/g, '').replace(/[^\da-zA-Z\u4E00-\u9FFF\u00C0-\u00FF\u00C0-\u02AF\u1E00-\u1EFF\u0590-\u05FF\u0400-\u052F\u0E00-\u0E7F\u0600-\u06FF\u0750-\u077F\u1100-\u11FF\u3130-\u318F\uAC00-\uD7AF\u3040-\u30FF\u31F0-\u31FF]/g, '')
  405. }
  406.  
  407. function addScript(/** @type {string} */ scriptText) {
  408. const scriptNode = document.createElement('script');
  409. scriptNode.type = 'text/javascript';
  410. scriptNode.textContent = scriptText;
  411. try {
  412. document.documentElement.appendChild(scriptNode);
  413. } catch (e) {
  414. console.log('addScript Error', e)
  415. }
  416. return scriptNode;
  417. }
  418.  
  419. function addScriptByURL(/** @type {string} */ scriptURL) {
  420. const scriptNode = document.createElement('script');
  421. scriptNode.type = 'text/javascript';
  422. scriptNode.src = scriptURL;
  423. try {
  424. document.documentElement.appendChild(scriptNode);
  425. } catch (e) {
  426. console.log('addScriptByURL Error', e)
  427. }
  428. return scriptNode;
  429. }
  430.  
  431. function addStyle(/** @type {string} */ styleText, /** @type {HTMLElement | Document} */ container) {
  432. const styleNode = document.createElement('style');
  433. //styleNode.type = 'text/css';
  434. styleNode.textContent = styleText;
  435. (container || document.documentElement).appendChild(styleNode);
  436. return styleNode;
  437. }
  438.  
  439.  
  440. const stopIframePropagation = function(/** @type {Event} */ evt){
  441. if(scriptEnable && evt && evt.target && evt.target.nodeName=='IFRAME'){
  442. evt.stopImmediatePropagation();
  443. evt.stopPropagation();
  444. }
  445. }
  446. document.addEventListener('mouseover', stopIframePropagation, true)
  447. document.addEventListener('mouseout', stopIframePropagation, true)
  448. document.addEventListener('mousedown', stopIframePropagation, true)
  449. document.addEventListener('mouseup', stopIframePropagation, true)
  450. document.addEventListener('keydown', stopIframePropagation, true)
  451. document.addEventListener('keyup', stopIframePropagation, true)
  452. document.addEventListener('mouseenter', stopIframePropagation, true)
  453. document.addEventListener('mouseleave', stopIframePropagation, true)
  454.  
  455.  
  456.  
  457. function isDOMVisible(/** @type {HTMLElement} */ elem) {
  458. // jQuery version : https://github.com/jquery/jquery/blob/a684e6ba836f7c553968d7d026ed7941e1a612d8/src/css/hiddenVisibleSelectors.js
  459. return !!(elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length);
  460. }
  461.  
  462. function isNonEmptyString(s) {
  463. return typeof s == 'string' && s.length > 0;
  464. }
  465.  
  466.  
  467. async function nativeCall(/** @type {EventTarget} */ dom, /** @type {any[]} */ detail) {
  468. //console.log(1231)
  469. dom.dispatchEvent(new CustomEvent("userscript-call-dom", { detail: detail }))
  470. //console.log(1232)
  471. }
  472.  
  473. async function nativeFunc(/** @type {EventTarget} */ dom, /** @type {string} */ property, /** @type {any} */ args) {
  474. dom.dispatchEvent(new CustomEvent("userscript-call-dom-func", { detail: { property, args } }))
  475. }
  476.  
  477. // async function nativeValue(dom, property, args) {
  478. // dom.dispatchEvent(new CustomEvent("userscript-call-dom-value", { detail: { property, args } }))
  479. // }
  480. // async function nativeFuncStacked(/** @type {string} */ selector, /** @type {string} */ property, /** @type {any} */ args){
  481. // document.dispatchEvent(new CustomEvent("userscript-call-dom-func-stacked", { detail: { selector, property, args } }))
  482. // }
  483. // async function nativeValueStacked(selector, property, args){
  484. // document.dispatchEvent(new CustomEvent("userscript-call-dom-value-stacked", { detail: { selector, property, args } }))
  485. // }
  486. // async function nativeConstStacked(selector, property, args){
  487. // document.dispatchEvent(new CustomEvent("userscript-call-dom-const-stacked", { detail: { selector, property, args } }))
  488. // }
  489.  
  490.  
  491. function akAttr(/** @type {HTMLElement} */ cssElm, /** @type {String} */ attrName, /** @type {boolean} */ isNegative, /** @type {string | any} */ flag) {
  492. // isNegative => incomplete loading
  493.  
  494. let u = parseInt(cssElm.getAttribute(attrName) || 0) || 0;
  495. let ak = Math.abs(u);
  496.  
  497. if (ak > 100 && isNegative && u < 0) {
  498.  
  499. } else if (ak > 100 && !isNegative && u > 0) {
  500.  
  501. } else {
  502. if (ak <= 100) {
  503. ak = 101;
  504. } else {
  505. ak++;
  506. if (ak >= 800) ak = 101;
  507. }
  508. // 101, 102, ... 799, 101
  509. }
  510.  
  511. cssElm.setAttribute(attrName, `${ isNegative ? -ak : ak }${ flag || '' }`)
  512. }
  513.  
  514.  
  515.  
  516. let timeout_resize_for_layout_change = new Timeout();
  517.  
  518. function dispatchWindowResize(){
  519. // for youtube to detect layout resize for adjusting Player tools
  520. return window.dispatchEvent(new Event('resize'));
  521. }
  522.  
  523.  
  524.  
  525. function layoutStatusChanged(/** @type {number} */ old_layoutStatus, /** @type {number} */ new_layoutStatus) {
  526.  
  527.  
  528. if (old_layoutStatus === new_layoutStatus) return;
  529.  
  530. const cssElm = kRef(ytdFlexy);
  531.  
  532. if (!cssElm) return;
  533.  
  534.  
  535. const new_isExpandedChat = !(new_layoutStatus & LAYOUT_CHATROOM_COLLASPED) && (new_layoutStatus & LAYOUT_CHATROOM)
  536. const new_isCollaspedChat = (new_layoutStatus & LAYOUT_CHATROOM_COLLASPED) && (new_layoutStatus & LAYOUT_CHATROOM)
  537.  
  538. const new_isTwoColumns = !!(new_layoutStatus & LAYOUT_TWO_COLUMNS);
  539. const new_isTheater = !!(new_layoutStatus & LAYOUT_THEATER);
  540. const new_isTabExpanded = !!(new_layoutStatus & LAYOUT_TAB_EXPANDED);
  541. const new_isFullScreen = !!(new_layoutStatus & LAYOUT_FULLSCREEN);
  542. const new_isExpandEPanel = !!(new_layoutStatus & LAYOUT_ENGAGEMENT_PANEL_EXPAND);
  543.  
  544.  
  545.  
  546. function showTabOrChat() {
  547.  
  548. layoutStatusMutex.lockWith(unlock => {
  549.  
  550. if (lastShowTab == '#chatroom') {
  551.  
  552. if (new_isTabExpanded) switchTabActivity(null)
  553. if (!new_isExpandedChat) ytBtnExpandChat();
  554.  
  555. } else if (lastShowTab && lastShowTab.indexOf('#engagement-panel-') == 0) {
  556.  
  557. if (new_isTabExpanded) switchTabActivity(null)
  558. if (!new_isExpandEPanel) ytBtnOpenEngagementPanel(lastShowTab);
  559.  
  560. } else {
  561.  
  562. if (new_isExpandedChat) ytBtnCollapseChat()
  563. if (!new_isTabExpanded) {setToActiveTab();}
  564.  
  565. }
  566.  
  567. timeline.setTimeout(unlock, 40);
  568.  
  569. })
  570. }
  571.  
  572. function hideTabAndChat() {
  573.  
  574. layoutStatusMutex.lockWith(unlock => {
  575.  
  576. if (new_isTabExpanded) switchTabActivity(null)
  577. if (new_isExpandedChat) ytBtnCollapseChat()
  578. if (new_isExpandEPanel) ytBtnCloseEngagementPanels();
  579.  
  580.  
  581. timeline.setTimeout(unlock, 40);
  582.  
  583. })
  584.  
  585. }
  586.  
  587.  
  588. if (new_isExpandedChat || new_isTabExpanded || new_isExpandEPanel) {
  589. if (statusCollasped !== 1) statusCollasped = 1;
  590. } else {
  591. if (statusCollasped === 1) statusCollasped = 2;
  592. }
  593.  
  594. let changes = 0;
  595.  
  596. if (old_layoutStatus !== null) changes = old_layoutStatus ^ new_layoutStatus;
  597.  
  598. let chat_collasped_changed = !!(changes & LAYOUT_CHATROOM_COLLASPED)
  599. let tab_expanded_changed = !!(changes & LAYOUT_TAB_EXPANDED)
  600. let theater_mode_changed = !!(changes & LAYOUT_THEATER)
  601. let column_mode_changed = !!(changes & LAYOUT_TWO_COLUMNS)
  602. let fullscreen_mode_changed = !!(changes & LAYOUT_FULLSCREEN)
  603. let epanel_expanded_changed = !!(changes & LAYOUT_ENGAGEMENT_PANEL_EXPAND)
  604.  
  605. let tab_change = (tab_expanded_changed ? 1 : 0) | (chat_collasped_changed ? 2 : 0) | (epanel_expanded_changed ? 4 : 0);
  606.  
  607. let isChatOrTabExpandTriggering = tab_change == 0 ? false : (
  608. (tab_expanded_changed && new_isTabExpanded) ||
  609. (chat_collasped_changed && new_isExpandedChat) ||
  610. (epanel_expanded_changed && new_isExpandEPanel)
  611. );
  612.  
  613. let isChatOrTabCollaspeTriggering = tab_change == 0 ? false : (
  614. (tab_expanded_changed && !new_isTabExpanded) ||
  615. (chat_collasped_changed && new_isCollaspedChat) ||
  616. (epanel_expanded_changed && !new_isExpandEPanel)
  617. );
  618.  
  619.  
  620.  
  621. let moreThanOneShown = (new_isTabExpanded + new_isExpandedChat + new_isExpandEPanel) > 1
  622.  
  623. let requestVideoResize = false;
  624.  
  625. if (fullscreen_mode_changed || new_isFullScreen) {
  626.  
  627. } else if(new_isTwoColumns && !new_isTheater && !column_mode_changed && (tab_change==2 || tab_change==4) && !new_isTabExpanded && new_isExpandedChat && new_isExpandEPanel){
  628.  
  629. if(epanel_expanded_changed){
  630. layoutStatusMutex.lockWith(unlock => {
  631. ytBtnCollapseChat();
  632. setTimeout(unlock,13)
  633. })
  634. }else if(chat_collasped_changed){
  635. layoutStatusMutex.lockWith(unlock => {
  636. ytBtnCloseEngagementPanels();
  637. setTimeout(unlock,13)
  638. })
  639.  
  640. }
  641.  
  642. } else if (tab_change == 0 && column_mode_changed && new_isTwoColumns && !new_isTheater && statusCollasped === 1 && moreThanOneShown) {
  643.  
  644. showTabOrChat();
  645. requestVideoResize = true;
  646.  
  647. } else if (tab_change == 2 && new_isExpandedChat && new_isTwoColumns && !new_isTheater && statusCollasped === 1 && new_isTabExpanded && !column_mode_changed) {
  648.  
  649. switchTabActivity(null);
  650. requestVideoResize = true;
  651.  
  652. } else if (isChatOrTabExpandTriggering && new_isTwoColumns && new_isTheater && statusCollasped === 1 && !theater_mode_changed && !column_mode_changed) {
  653.  
  654. ytBtnCancelTheater();
  655. requestVideoResize = true;
  656.  
  657. } else if (new_isTwoColumns && new_isTheater && statusCollasped === 1) {
  658.  
  659. hideTabAndChat();
  660. requestVideoResize = true;
  661.  
  662. } else if (isChatOrTabCollaspeTriggering && new_isTwoColumns && !new_isTheater && statusCollasped === 2 && !column_mode_changed) {
  663.  
  664. ytBtnSetTheater();
  665. requestVideoResize = true;
  666.  
  667. } else if (tab_change == 0 && (column_mode_changed || theater_mode_changed) && new_isTwoColumns && !new_isTheater && statusCollasped !== 1) {
  668.  
  669. showTabOrChat();
  670. requestVideoResize = true;
  671.  
  672. } else if (!new_isFullScreen && new_isTwoColumns && !new_isTheater && !new_isTabExpanded &&
  673. (new_isCollaspedChat || !new_isExpandedChat) &&
  674. !new_isExpandEPanel
  675. ) {
  676. // bug fix for restoring from mini player
  677.  
  678. layoutStatusMutex.lockWith(unlock => {
  679. setToActiveTab();
  680. timeline.setTimeout(unlock, 40);
  681. })
  682.  
  683. requestVideoResize = true;
  684.  
  685. } else if (tab_expanded_changed) {
  686.  
  687. requestVideoResize = true;
  688.  
  689. }
  690.  
  691.  
  692. if (column_mode_changed && !chat_collasped_changed && new_isExpandedChat) {
  693.  
  694. runAfterExpandChat();
  695.  
  696. }
  697.  
  698.  
  699.  
  700. if (requestVideoResize) {
  701.  
  702. timeout_resize_for_layout_change.clear();
  703. timeout_resize_for_layout_change.set(() => {
  704. dispatchWindowResize();
  705. }, 92)
  706.  
  707. } else if (timeout_resize_for_layout_change.isEmpty() && (Date.now()) - lastResizeAt > 600) {
  708. timeout_resize_for_layout_change.set(() => {
  709. if ((Date.now()) - lastResizeAt > 600) dispatchWindowResize();
  710. }, 62)
  711. }
  712.  
  713.  
  714.  
  715.  
  716. }
  717.  
  718.  
  719.  
  720. const wls = new Proxy({
  721. /** @type {number | null} */
  722. layoutStatus:undefined
  723. }, {
  724. get: function(target, prop) {
  725. return target[prop];
  726. },
  727. set: function(target, prop, value) {
  728. if(prop=='layoutStatus'){
  729. if (value === null) {
  730. statusCollasped = 0;
  731. target[prop] = value;
  732. return;
  733. }else if(target[prop]===value){
  734. return;
  735. }else{
  736. if (!target.layoutStatus_pending) {
  737. target.layoutStatus_pending = true;
  738. const old_layoutStatus = target[prop];
  739. target[prop] = value;
  740. layoutStatusMutex.lockWith(unlock => {
  741. target.layoutStatus_pending = false;
  742. layoutStatusChanged(old_layoutStatus, target[prop]);
  743. timeline.setTimeout(unlock, 40)
  744. })
  745. return;
  746. }
  747. }
  748. }
  749. target[prop] = value;
  750. },
  751. has: function(target, prop) {
  752. return (prop in target);
  753. }
  754. });
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761. const svgElm = (w, h, vw, vh, p) => `<svg width="${w}" height="${h}" viewBox="0 0 ${vw} ${vh}" preserveAspectRatio="xMidYMid meet">${p}</svg>`
  762.  
  763. let settings = {
  764. defaultTab: "#tab-videos"
  765. };
  766.  
  767.  
  768. let mtoInterval = mtoInterval1;
  769.  
  770. function isVideoPlaying(video) {
  771. return video.currentTime > 0 && !video.paused && !video.ended && video.readyState > video.HAVE_CURRENT_DATA;
  772. }
  773.  
  774. function wAttr(elm, attr, kv) {
  775. if (!elm || kv === null) {} else if (kv === true) { elm.setAttribute(attr, '') } else if (kv === false) { elm.removeAttribute(attr) } else if (typeof kv == 'string') { elm.setAttribute(attr, kv) }
  776. }
  777.  
  778. function hideTabBtn(tabBtn) {
  779. let isActiveBefore = tabBtn.classList.contains('active');
  780. tabBtn.classList.add("tab-btn-hidden");
  781. if (isActiveBefore) {
  782. setToActiveTab();
  783. }
  784. }
  785.  
  786. function hasAttribute(obj, key) {
  787. return obj && obj.hasAttribute(key);
  788. }
  789.  
  790. function isTheater() {
  791. const cssElm = kRef(ytdFlexy);
  792. return (cssElm && cssElm.hasAttribute('theater'))
  793. }
  794.  
  795. function isFullScreen() {
  796. const cssElm = kRef(ytdFlexy);
  797. return (cssElm && cssElm.hasAttribute('fullscreen'))
  798. }
  799.  
  800. function isChatExpand() {
  801. const cssElm = kRef(ytdFlexy);
  802. return cssElm && cssElm.hasAttribute('userscript-chatblock') && !cssElm.hasAttribute('userscript-chat-collapsed')
  803. }
  804.  
  805. function isWideScreenWithTwoColumns() {
  806. const cssElm = kRef(ytdFlexy);
  807. return (cssElm && cssElm.hasAttribute('is-two-columns_'))
  808. }
  809.  
  810. function isAnyActiveTab() {
  811. return $('#right-tabs .tab-btn.active').length > 0
  812. }
  813.  
  814. function isEngagementPanelExpanded() { //note: not checking the visual elements
  815. const cssElm = kRef(ytdFlexy);
  816. return (cssElm && +cssElm.getAttribute('userscript-engagement-panel') > 0)
  817. }
  818.  
  819. function engagement_panels_() {
  820.  
  821. let res = [];
  822. let shownRes = [];
  823.  
  824. let v = 0,
  825. k = 1,
  826. count = 0;
  827. for (const ePanel of document.querySelectorAll(
  828. `ytd-watch-flexy ytd-engagement-panel-section-list-renderer[o3r-${sa_epanel}]`
  829. )) {
  830.  
  831. let visibility = ePanel.getAttribute('visibility') //ENGAGEMENT_PANEL_VISIBILITY_EXPANDED //ENGAGEMENT_PANEL_VISIBILITY_HIDDEN
  832.  
  833. switch (visibility) {
  834. case 'ENGAGEMENT_PANEL_VISIBILITY_EXPANDED':
  835. v |= k;
  836. count++;
  837. shownRes.push(ePanel)
  838. res.push({ ePanel, k, visible: true });
  839. break;
  840. case 'ENGAGEMENT_PANEL_VISIBILITY_HIDDEN':
  841. res.push({ ePanel, k, visible: false });
  842. break;
  843. default:
  844. res.push({ ePanel, k, visible: false });
  845. }
  846.  
  847. k = k << 1;
  848.  
  849. }
  850. return { list: res, value: v, count: count, shownRes };
  851. }
  852.  
  853.  
  854. function ytBtnOpenEngagementPanel(/** @type {number | string} */ panel_id) {
  855.  
  856. if (typeof panel_id == 'string') {
  857. panel_id = panel_id.replace('#engagement-panel-', '');
  858. panel_id = parseInt(panel_id);
  859. }
  860. if (panel_id >= 0) {} else return false;
  861.  
  862. let panels = engagement_panels_();
  863.  
  864. for (const { ePanel, k, visible } of panels.list) {
  865. if ((panel_id & k) === k) {
  866. if (!visible) ePanel.setAttribute('visibility', "ENGAGEMENT_PANEL_VISIBILITY_EXPANDED");
  867. } else {
  868. if (visible) ytBtnCloseEngagementPanel(ePanel);
  869. }
  870. }
  871.  
  872. }
  873.  
  874. function ytBtnCloseEngagementPanel(/** @type {HTMLElement} */ s) {
  875. //ePanel.setAttribute('visibility',"ENGAGEMENT_PANEL_VISIBILITY_HIDDEN");
  876. let btn = querySelectorFromAnchor.call(s,'ytd-watch-flexy ytd-engagement-panel-title-header-renderer #header > #visibility-button > ytd-button-renderer');
  877. if (btn) {
  878. btn.click();
  879. }
  880. }
  881.  
  882. function ytBtnCloseEngagementPanels() {
  883. if (isEngagementPanelExpanded()) {
  884. for (const s of document.querySelectorAll(
  885. `ytd-watch-flexy ytd-engagement-panel-section-list-renderer[o3r-${sa_epanel}]`
  886. )) {
  887. if (s.getAttribute('visibility') == "ENGAGEMENT_PANEL_VISIBILITY_EXPANDED") ytBtnCloseEngagementPanel(s);
  888. }
  889. }
  890. }
  891.  
  892. function ytBtnSetTheater() {
  893. if (!isTheater()) {
  894. const sizeBtn = document.querySelector('ytd-watch-flexy #ytd-player button.ytp-size-button')
  895. if (sizeBtn) sizeBtn.click();
  896. }
  897. }
  898.  
  899. function ytBtnCancelTheater() {
  900. if (isTheater()) {
  901. const sizeBtn = document.querySelector('ytd-watch-flexy #ytd-player button.ytp-size-button')
  902. if (sizeBtn) sizeBtn.click();
  903. }
  904. }
  905.  
  906. function ytBtnExpandChat() {
  907. let button = document.querySelector('ytd-live-chat-frame#chat[collapsed] > .ytd-live-chat-frame#show-hide-button')
  908. if (button) querySelectorFromAnchor.call(button,'ytd-toggle-button-renderer').click();
  909. }
  910.  
  911. function ytBtnCollapseChat() {
  912. let button = document.querySelector('ytd-live-chat-frame#chat:not([collapsed]) > .ytd-live-chat-frame#show-hide-button')
  913. if (button) querySelectorFromAnchor.call(button, 'ytd-toggle-button-renderer').click();
  914. }
  915.  
  916.  
  917. const Q = {}
  918.  
  919.  
  920.  
  921. function chatFrameContentDocument() {
  922. // non-null if iframe exist && contentDocument && readyState = complete
  923. /** @type {HTMLIFrameElement | null} */
  924. let iframe = document.querySelector('ytd-live-chat-frame iframe#chatframe');
  925. if (!iframe) return null; //iframe must be there
  926. /** @type {Document | null} */
  927. let cDoc = null;
  928. try {
  929. cDoc = iframe.contentDocument;
  930. } catch (e) {}
  931. if (!cDoc) return null;
  932. if (cDoc.readyState != 'complete') return null; //we must wait for its completion
  933.  
  934. return cDoc;
  935.  
  936. }
  937.  
  938. function chatFrameElement(/** @type {string} */ cssSelector) {
  939. let cDoc = chatFrameContentDocument();
  940. if (!cDoc) return null;
  941. /** @type {HTMLElement | null} */
  942. let elm = null;
  943. try {
  944. elm = cDoc.querySelector(cssSelector)
  945. } catch (e) {
  946. console.log('iframe error', e)
  947. }
  948. return elm;
  949. }
  950.  
  951.  
  952.  
  953.  
  954. function fixTabs() {
  955.  
  956.  
  957. if (!scriptEnable) return;
  958.  
  959.  
  960. let queryElement = document.querySelector('*:not(#tab-videos) > #related:not([non-placeholder-videos]) > ytd-watch-next-secondary-results-renderer')
  961.  
  962. let isRelocated = !!queryElement;
  963.  
  964.  
  965.  
  966. if (isRelocated) {
  967.  
  968. let relocatedRelated = closestDOM.call(queryElement, '#related'); // NOT NULL
  969.  
  970. let right_tabs = document.querySelector('#right-tabs');
  971. let tab_videos = querySelectorFromAnchor.call(right_tabs,"#tab-videos");
  972.  
  973. if (!right_tabs || !tab_videos) return;
  974.  
  975. for (const s of querySelectorAllFromAnchor.call(relocatedRelated,'#related')) {
  976. s.setAttribute('non-placeholder-videos', '')
  977. }
  978.  
  979. let target_container = document.querySelector('ytd-watch-flexy:not([is-two-columns_]) #primary-inner, ytd-watch-flexy[is-two-columns_] #secondary-inner')
  980.  
  981. if (target_container) target_container.append(right_tabs) // last-child
  982.  
  983.  
  984. let videos_related = relocatedRelated; // NOT NULL
  985. $('[placeholder-videos]').removeAttr('placeholder-videos');
  986. $('[placeholder-for-youtube-play-next-queue]').removeAttr('placeholder-for-youtube-play-next-queue');
  987.  
  988. tab_videos.appendChild(videos_related);
  989. let videos_results_renderer = querySelectorFromAnchor.call(relocatedRelated,"ytd-watch-next-secondary-results-renderer");
  990. if (videos_results_renderer) videos_results_renderer.setAttribute('data-dom-changed-by-tabview-youtube', scriptVersionForExternal);
  991. videos_related.setAttribute('placeholder-for-youtube-play-next-queue', '')
  992. videos_related.setAttribute('placeholder-videos', '')
  993.  
  994. $('[placeholder-videos]').on("scroll", makeBodyScrollByEvt);
  995.  
  996.  
  997.  
  998.  
  999. }
  1000.  
  1001.  
  1002.  
  1003. /** @type {HTMLElement | null} */
  1004. let chatroom = null;
  1005. if (chatroom = document.querySelector('*:not([data-positioner="before|#chat"]) + ytd-live-chat-frame#chat, ytd-live-chat-frame#chat:first-child')) {
  1006.  
  1007. let positioner = document.querySelector('tabview-youtube-positioner[data-positioner="before|#chat"]');
  1008. if (positioner) positioner.remove();
  1009.  
  1010.  
  1011. if (document.querySelector('.YouTubeLiveFilledUpView')) {
  1012. // no relocation
  1013. } else {
  1014.  
  1015. $(chatroom).insertBefore('#right-tabs')
  1016.  
  1017. }
  1018.  
  1019.  
  1020. $(positioner ? positioner : document.createElement('tabview-youtube-positioner')).attr('data-positioner', 'before|#chat').insertBefore(chatroom)
  1021.  
  1022.  
  1023.  
  1024. }
  1025.  
  1026.  
  1027. }
  1028.  
  1029. function handlerAutoCompleteExist() {
  1030.  
  1031.  
  1032. /** @type {HTMLElement} */
  1033. let autoComplete = this;
  1034.  
  1035. autoComplete.removeEventListener('autocomplete-sc-exist', handlerAutoCompleteExist, false)
  1036.  
  1037. let domId = autoComplete.getAttribute('data-autocomplete-input-id')
  1038. let searchBox = autoComplete.ownerDocument.querySelector(`[data-autocomplete-results-id="${domId}"]`)
  1039.  
  1040. if (!domId || !searchBox) return;
  1041.  
  1042. let positioner = searchBox.nextSibling;
  1043. if (positioner && positioner.nodeName.toLowerCase() == "autocomplete-positioner") {} else if (positioner && positioner.nodeName.toLowerCase() != "autocomplete-positioner") {
  1044. $(positioner = document.createElement("autocomplete-positioner")).insertAfter(searchBox);
  1045. } else {
  1046. $(positioner = document.createElement("autocomplete-positioner")).prependTo(searchBox.parentNode);
  1047. }
  1048. $(autoComplete).prependTo(positioner);
  1049.  
  1050. positioner.style.setProperty('--sb-margin-bottom', getComputedStyle(searchBox).marginBottom)
  1051. positioner.style.setProperty('--height', searchBox.offsetHeight + 'px')
  1052.  
  1053. }
  1054.  
  1055. function mtf_fixAutoCompletePosition(/** @type {HTMLElement} */ elmAutoComplete) {
  1056.  
  1057.  
  1058. elmAutoComplete.setAttribute('autocomplete-disable-updatesc', '')
  1059. elmAutoComplete.addEventListener('autocomplete-sc-exist', handlerAutoCompleteExist, false)
  1060.  
  1061. script_inject_facp.inject();
  1062.  
  1063. }
  1064.  
  1065. function mtf_AfterFixTabs() {
  1066.  
  1067.  
  1068. /** @type {HTMLElement | null} */
  1069. let ytdFlexyElm = kRef(ytdFlexy);
  1070. if (!scriptEnable || !ytdFlexyElm) return;
  1071.  
  1072. /** @type {HTMLElement | null} */
  1073. const rootElement = Q.mutationTarget || ytdFlexyElm;
  1074.  
  1075.  
  1076.  
  1077. const autocomplete = querySelectorFromAnchor.call(rootElement,'[placeholder-for-youtube-play-next-queue] input#suggestions-search + autocomplete-positioner > .autocomplete-suggestions[data-autocomplete-input-id]:not([position-fixed-by-tabview-youtube])')
  1078.  
  1079. if (autocomplete) {
  1080.  
  1081. const searchBox = document.querySelector('[placeholder-for-youtube-play-next-queue] input#suggestions-search')
  1082.  
  1083.  
  1084. if (searchBox) {
  1085.  
  1086.  
  1087. autocomplete.parentNode.setAttribute('position-fixed-by-tabview-youtube', '');
  1088. autocomplete.setAttribute('position-fixed-by-tabview-youtube', '');
  1089. autocomplete.setAttribute('userscript-scrollbar-render', '')
  1090.  
  1091. if (!searchBox.hasAttribute('is-set-click-to-toggle')) {
  1092. searchBox.setAttribute('is-set-click-to-toggle', '')
  1093. searchBox.addEventListener('click', function() {
  1094.  
  1095.  
  1096. setTimeout(() => {
  1097. const autocomplete = document.querySelector(`.autocomplete-suggestions[data-autocomplete-input-id="${ this.getAttribute('data-autocomplete-results-id') }"]`)
  1098.  
  1099. if (!autocomplete) return;
  1100.  
  1101. const isNotEmpty = (autocomplete.textContent || '').length > 0 && (this.value || '').length > 0;
  1102.  
  1103. if (isNotEmpty) {
  1104.  
  1105. let elmVisible = isDOMVisible(autocomplete)
  1106.  
  1107. if (elmVisible) $(autocomplete).hide();
  1108. else $(autocomplete).show();
  1109.  
  1110. }
  1111.  
  1112. }, 20);
  1113.  
  1114. })
  1115.  
  1116. let timeoutOnce_searchbox_keyup = new Timeout();
  1117. searchBox.addEventListener('keyup', function() {
  1118.  
  1119. timeoutOnce_searchbox_keyup.set(() => {
  1120.  
  1121. const autocomplete = document.querySelector(`.autocomplete-suggestions[data-autocomplete-input-id="${ this.getAttribute('data-autocomplete-results-id') }"]`)
  1122.  
  1123. if (!autocomplete) return;
  1124.  
  1125.  
  1126. const isNotEmpty = (autocomplete.textContent || '').length > 0 && (this.value || '').length > 0
  1127.  
  1128. if (isNotEmpty) {
  1129.  
  1130. let elmVisible = isDOMVisible(autocomplete)
  1131.  
  1132. if (!elmVisible) $(autocomplete).show();
  1133.  
  1134. }
  1135.  
  1136. }, 20);
  1137.  
  1138. })
  1139.  
  1140. }
  1141.  
  1142.  
  1143.  
  1144. }
  1145.  
  1146. }
  1147.  
  1148.  
  1149.  
  1150.  
  1151. let currentLastVideo = querySelectorFromAnchor.call(rootElement,'[placeholder-videos] #items ytd-compact-video-renderer:last-of-type')
  1152. let prevLastVideo = kRef(_cachedLastVideo);
  1153.  
  1154. if (prevLastVideo !== currentLastVideo && currentLastVideo) {
  1155. _cachedLastVideo = mWeakRef(currentLastVideo);
  1156. }
  1157.  
  1158. if (prevLastVideo !== currentLastVideo && currentLastVideo && prevLastVideo) {
  1159.  
  1160. let isPrevRemoved = !prevLastVideo.parentNode
  1161.  
  1162.  
  1163. function getVideoListHash() {
  1164.  
  1165. let res = [...document.querySelectorAll('[placeholder-videos] #items ytd-compact-video-renderer')].map(renderer => {
  1166. return querySelectorFromAnchor.call(renderer,'a[href*="watch"][href*="v="]').getAttribute('href')
  1167.  
  1168. }).join('|')
  1169. // /watch?v=XXXXX|/watch?v=XXXXXX|/watch?v=XXXXXX
  1170.  
  1171. // alternative - DOM.data.videoId
  1172. // let elms = document.querySelectorAll('[placeholder-videos] #items ytd-compact-video-renderer')
  1173. // let res = [...elms].map(elm=>elm.data.videoId||'').join('|') ;
  1174.  
  1175. if (res.indexOf('||') >= 0) {
  1176. res = '';
  1177. }
  1178.  
  1179. return res ? res : null;
  1180. }
  1181.  
  1182. if (isPrevRemoved) {
  1183.  
  1184. // this is the replacement of videos instead of addition
  1185.  
  1186. const searchBox = document.querySelector('[placeholder-for-youtube-play-next-queue] input#suggestions-search')
  1187.  
  1188. let currentPlayListHash = getVideoListHash() || null;
  1189.  
  1190. if (!currentPlayListHash) {
  1191.  
  1192. } else if (!videoListBeforeSearch && searchBox) {
  1193.  
  1194. videoListBeforeSearch = currentPlayListHash;
  1195. if (videoListBeforeSearch) {
  1196. //console.log('fromSearch', videoListBeforeSearch)
  1197.  
  1198. requestAnimationFrame(function() {
  1199.  
  1200. let renderer = document.querySelector('[placeholder-videos] ytd-watch-next-secondary-results-renderer');
  1201. if (searchBox && searchBox.parentNode) searchBox.blur();
  1202.  
  1203. if (renderer) {
  1204. let scrollParent = renderer.parentNode;
  1205. if (scrollParent.scrollHeight > scrollParent.offsetHeight) {
  1206. let targetTop = renderer.offsetTop;
  1207. if (searchBox && searchBox.parentNode == scrollParent) targetTop -= searchBox.offsetHeight
  1208. scrollParent.scrollTop = targetTop - scrollParent.firstChild.offsetTop;
  1209. }
  1210. }
  1211.  
  1212. });
  1213.  
  1214. }
  1215.  
  1216. } else if (videoListBeforeSearch) {
  1217.  
  1218. if (currentPlayListHash != videoListBeforeSearch) {
  1219.  
  1220. videoListBeforeSearch = null;
  1221. //console.log('fromSearch', videoListBeforeSearch)
  1222.  
  1223.  
  1224. }
  1225.  
  1226. }
  1227.  
  1228.  
  1229. }
  1230.  
  1231.  
  1232. }
  1233.  
  1234.  
  1235.  
  1236.  
  1237. }
  1238.  
  1239. function base_ChatExist() {
  1240.  
  1241. let ytdFlexyElm = kRef(ytdFlexy);
  1242. if (!scriptEnable || !ytdFlexyElm) return null;
  1243.  
  1244. // no mutation triggering if the changes are inside the iframe
  1245.  
  1246. // 1) Detection of #continuations inside iframe
  1247. // iframe ownerDocument is accessible due to same origin
  1248. // if the chatroom is collasped, no determination of live chat or replay (as no #continuations and somehow a blank iframe doc)
  1249.  
  1250. // 2) Detection of meta tag
  1251. // This is fastest but not reliable. It is somehow a bug that the navigation might not update the meta tag content
  1252.  
  1253. // 3) Detection of HTMLElement inside video player for live video
  1254.  
  1255. // (1)+(3) = solution
  1256.  
  1257. let attr_chatblock = null
  1258. let attr_chatcollapsed = null;
  1259.  
  1260. const elmChat = document.querySelector('ytd-live-chat-frame#chat')
  1261. let elmCont = null;
  1262. if (elmChat) {
  1263. elmCont = chatFrameElement('yt-live-chat-renderer #continuations')
  1264.  
  1265.  
  1266. let s = 0;
  1267. if (elmCont) {
  1268. //not found if it is collasped.
  1269. s |= querySelectorFromAnchor.call(elmCont,'yt-timed-continuation') ? 1 : 0;
  1270. s |= querySelectorFromAnchor.call(elmCont,'yt-live-chat-replay-continuation, yt-player-seek-continuation') ? 2 : 0;
  1271. //s |= elmCont.querySelector('yt-live-chat-restricted-participation-renderer')?4:0;
  1272. if (s == 1) {
  1273. attr_chatblock = 'chat-live';
  1274. }else if (s == 2) attr_chatblock = 'chat-playback';
  1275.  
  1276. if (s == 1) $("span#tab3-txt-loader").text('');
  1277.  
  1278. } else if (!ytdFlexyElm.hasAttribute('userscript-chatblock')) {
  1279. // live chat frame but type not known
  1280. attr_chatblock = '';
  1281.  
  1282. }
  1283. //keep unknown as original
  1284. let isCollapsed = !!elmChat.hasAttribute('collapsed');
  1285. attr_chatcollapsed = isCollapsed;
  1286.  
  1287. } else {
  1288. attr_chatblock = false;
  1289. attr_chatcollapsed = false;
  1290.  
  1291. }
  1292.  
  1293. return { attr_chatblock, attr_chatcollapsed }
  1294.  
  1295. }
  1296.  
  1297.  
  1298. function mtf_ChatExist() {
  1299.  
  1300. let ytdFlexyElm = kRef(ytdFlexy);
  1301. if (!scriptEnable || !ytdFlexyElm) return;
  1302.  
  1303. if (deferredVarYTDHidden) return;
  1304.  
  1305. const elmChat = document.querySelector('ytd-live-chat-frame#chat')
  1306. let elmCont = null;
  1307. if (elmChat) {
  1308. elmCont = chatFrameElement('yt-live-chat-renderer #continuations')
  1309. }
  1310.  
  1311. const chatBlockR = (elmChat ? 1 : 0) + (elmCont ? 2 : 0)
  1312. if (Q.mtf_chatBlockQ !== chatBlockR) {
  1313. Q.mtf_chatBlockQ = chatBlockR
  1314. let rChatExist = base_ChatExist();
  1315. //console.log(2446, rChatExist)
  1316. if (rChatExist) {
  1317. let { attr_chatblock, attr_chatcollapsed } = rChatExist;
  1318. wAttr(ytdFlexyElm, 'userscript-chatblock', attr_chatblock)
  1319. wAttr(ytdFlexyElm, 'userscript-chat-collapsed', attr_chatcollapsed)
  1320. if(attr_chatblock=='chat-live') _disableComments();
  1321. }
  1322. }
  1323.  
  1324. }
  1325.  
  1326.  
  1327.  
  1328.  
  1329. let lastScrollAt1 = 0;
  1330.  
  1331. function makeBodyScrollByEvt() {
  1332. let ct = Date.now();
  1333. if (ct - lastScrollAt1 < 6) return; // avoid duplicate calling
  1334. lastScrollAt1 = ct;
  1335. window.dispatchEvent(new Event("scroll")); // dispatch Scroll Event to Window for content display
  1336. }
  1337.  
  1338. let lastScrollAt2 = 0;
  1339.  
  1340. function makeBodyScroll() {
  1341. let ct = Date.now();
  1342. if (ct - lastScrollAt2 < 30) return; // avoid over triggering
  1343. lastScrollAt2 = ct;
  1344. requestAnimationFrame(() => {
  1345. window.dispatchEvent(new Event("scroll")); // ask youtube to display content
  1346. })
  1347. }
  1348.  
  1349. //let requestingComments = null
  1350. //let scrollForComments_lastStart = 0;
  1351. /*
  1352. function scrollForComments_TF() {
  1353. let comments = requestingComments;
  1354. if (!comments) return;
  1355. if (comments.hasAttribute('hidden')) {
  1356. window.dispatchEvent(new Event("scroll"));
  1357. } else requestingComments = null;
  1358.  
  1359. }
  1360. function scrollForComments() {
  1361. scrollForComments_TF();
  1362. if (!requestingComments) return;
  1363. requestAnimationFrame(scrollForComments_TF);
  1364. let ct = Date.now();
  1365. if (ct - scrollForComments_lastStart < 60) return;
  1366. scrollForComments_lastStart = ct;
  1367. timeline.setTimeout(scrollForComments_TF, 80);
  1368. timeline.setTimeout(scrollForComments_TF, 240);
  1369. timeline.setTimeout(scrollForComments_TF, 870);
  1370. }
  1371. */
  1372.  
  1373.  
  1374.  
  1375. const mtoCs = { mtoNav: null, mtoBody: null };
  1376.  
  1377.  
  1378. const mtoVs = {}
  1379.  
  1380. const mutation_target_id_list = ['ytp-caption-window-container', 'items', 'button', 'movie_player', 'player-ads', 'hover-overlays', 'replies'];
  1381. const mutation_target_class_list = ['ytp-panel-menu', 'ytp-endscreen-content'];
  1382.  
  1383. function isMtoOverallSkip(dTarget) {
  1384.  
  1385. if (!dTarget || dTarget.nodeType !== 1) return true;
  1386.  
  1387. if (mutation_target_id_list.includes(dTarget.id)) return true;
  1388.  
  1389. for (const c of dTarget.classList) {
  1390. if (mutation_target_class_list.includes(c)) return true;
  1391. }
  1392.  
  1393. return false;
  1394. }
  1395.  
  1396.  
  1397. const mutation_div_id_ignorelist = [
  1398. 'metadata-line',
  1399. 'ytp-caption-window-container',
  1400. 'top-level-buttons-computed',
  1401. 'microformat',
  1402. 'visibility-button',
  1403. 'info-strings',
  1404. 'action-menu',
  1405. 'reply-button-end'
  1406. ];
  1407.  
  1408. const mutation_div_class_ignorelist = [
  1409. 'badge', 'tp-yt-paper-tooltip', 'ytp-autonav-endscreen-upnext-header',
  1410. 'ytp-bound-time-left', 'ytp-bound-time-right', 'ytp-share-icon',
  1411. 'ytp-tooltip-title', 'annotation', 'ytp-copylink-icon', 'ytd-thumbnail',
  1412. 'paper-ripple',
  1413. //caption
  1414. 'captions-text', 'caption-visual-line', 'ytp-caption-segment', 'ytp-caption-window-container',
  1415. //menu
  1416. 'ytp-playlist-menu-button-text',
  1417.  
  1418. 'ytp-bezel-icon', 'ytp-bezel-text',
  1419. 'dropdown-content',
  1420. 'tp-yt-paper-menu-button', 'tp-yt-iron-dropdown',
  1421.  
  1422. 'ytd-metadata-row-renderer', // #content.ytd-metadata-row-renderer inside each of ytd-metadata-row-renderer (ytd-expander)
  1423. 'ytd-engagement-panel-section-list-renderer', // {div#content.style-scope.ytd-engagement-panel-section-list-renderer} inside each of ytd-engagement-panel-section-list-renderer
  1424.  
  1425. 'autocomplete-suggestions' // autocomplete-suggestions
  1426. ];
  1427.  
  1428. const mutation_target_tag_ignorelist = [
  1429. 'ytd-channel-name', 'tp-yt-iron-dropdown', 'tp-yt-paper-tooltip',
  1430. 'tp-yt-paper-listbox', 'yt-img-shadow', 'ytd-thumbnail', 'ytd-video-meta-block',
  1431.  
  1432. 'yt-icon-button', 'tp-yt-paper-button', 'yt-formatted-string', 'yt-icon', 'button', 'paper-ripple',
  1433.  
  1434. 'ytd-player-microformat-renderer',
  1435. 'ytd-engagement-panel-section-list-renderer', 'ytd-engagement-panel-title-header-renderer',
  1436. 'ytd-comment-renderer', 'ytd-menu-renderer', 'ytd-badge-supported-renderer',
  1437. 'ytd-subscribe-button-renderer', 'ytd-subscription-notification-toggle-button-renderer',
  1438. 'ytd-button-renderer', 'ytd-toggle-button-renderer',
  1439. 'yt-pdg-comment-chip-renderer', 'ytd-comment-action-buttons-renderer', 'ytd-comment-thread-renderer',
  1440. 'ytd-compact-radio-renderer', 'ytd-compact-video-renderer',
  1441. 'ytd-video-owner-renderer',
  1442. 'ytd-metadata-row-renderer', //ytd-metadata-row-renderer is part of the #collapsible inside ytd-expander
  1443.  
  1444. 'ytd-moving-thumbnail-renderer',
  1445. 'ytd-thumbnail-overlay-toggle-button-renderer',
  1446. 'ytd-thumbnail-overlay-bottom-panel-renderer', 'ytd-thumbnail-overlay-equalizer',
  1447. 'ytd-thumbnail-overlay-now-playing-renderer', 'ytd-thumbnail-overlay-resume-playback-renderer',
  1448. 'ytd-thumbnail-overlay-side-panel-renderer', 'ytd-thumbnail-overlay-time-status-renderer',
  1449. 'ytd-thumbnail-overlay-hover-text-renderer',
  1450.  
  1451. 'yt-interaction',
  1452. 'tp-yt-paper-spinner-lite', 'tp-yt-paper-spinner',
  1453.  
  1454. 'h1', 'h2', 'h3', 'h4', 'h5', 'span', 'a',
  1455.  
  1456. 'meta', 'br', 'script', 'style', 'link', 'dom-module', 'template'
  1457. ];
  1458.  
  1459. function isMtoTargetSkip(mutation) {
  1460. //skip not important mutation tartget
  1461.  
  1462. if (!mutation) return true;
  1463. let { type, target } = mutation
  1464.  
  1465. if (!target || target.nodeType !== 1 || type != 'childList') return true;
  1466.  
  1467. let tagName = target.nodeName.toLowerCase();
  1468.  
  1469. if (mutation_target_tag_ignorelist.includes(tagName)) return true;
  1470.  
  1471. switch (tagName) {
  1472.  
  1473.  
  1474. case 'ytd-expander':
  1475. if (target.id == 'expander' && Q.comments_section_loaded == 1 && target.classList.contains('ytd-comment-renderer')) return true; // load comments
  1476. return false;
  1477.  
  1478. case 'div':
  1479.  
  1480. if (target.id == 'contents') {
  1481. return false;
  1482. }
  1483. if (mutation_div_id_ignorelist.includes(target.id)) return true;
  1484.  
  1485. for (const c of target.classList) {
  1486. if (mutation_div_class_ignorelist.includes(c)) return true;
  1487. }
  1488.  
  1489. return false;
  1490.  
  1491. }
  1492.  
  1493. return false;
  1494.  
  1495. }
  1496.  
  1497.  
  1498. function mtf_forceCheckLiveVideo() {
  1499. // once per $$player-playback-timestamp$$ {#ytd-player .ytp-time-display} && $$chat-frame$$ {ytd-live-chat-frame#chat} detection
  1500. // reset after popstatechange / videochange
  1501.  
  1502. if(mtf_forceCheckLiveVideo_disable) return ;
  1503.  
  1504. let ytdFlexyElm = kRef(ytdFlexy);
  1505. if (!scriptEnable || !ytdFlexyElm) return ;
  1506.  
  1507. const playerLabel = document.querySelector('ytd-watch-flexy:not([hidden]) #ytd-player .ytp-time-display') && document.querySelector('ytd-watch-flexy:not([hidden]) ytd-live-chat-frame#chat')
  1508. if (!playerLabel) return ;
  1509. mtf_forceCheckLiveVideo_disable = 1;
  1510. timeline.setTimeout(FP.fireOnce_forceCheckLiveVideo_tf, 170)
  1511. return ;
  1512. }
  1513.  
  1514. // continuous check for element relocation
  1515. function mtf_append_comments() {
  1516. /** @type {HTMLElement | null} */
  1517. let ytdFlexyElm = kRef(ytdFlexy);
  1518. if (!scriptEnable || !ytdFlexyElm) return;
  1519.  
  1520. /** @type {HTMLElement | null} */
  1521. const rootElement = Q.mutationTarget || ytdFlexyElm;
  1522.  
  1523. let comments = querySelectorFromAnchor.call(rootElement,'#primary ytd-watch-metadata ~ ytd-comments#comments');
  1524. if (comments) $(comments).appendTo('#tab-comments').attr('data-dom-changed-by-tabview-youtube', scriptVersionForExternal)
  1525. }
  1526.  
  1527. // continuous check for element relocation
  1528. function mtf_liveChatBtnF() {
  1529. /** @type {HTMLElement | null} */
  1530. let ytdFlexyElm = kRef(ytdFlexy);
  1531. if (!scriptEnable || !ytdFlexyElm) return;
  1532.  
  1533. /** @type {HTMLElement | null} */
  1534. const rootElement = Q.mutationTarget || ytdFlexyElm;
  1535.  
  1536. let button = querySelectorFromAnchor.call(rootElement,'ytd-live-chat-frame#chat > .ytd-live-chat-frame#show-hide-button:nth-child(n+2)');
  1537. if (button){
  1538. let parentNode = closestDOM.call(button, 'ytd-live-chat-frame#chat');
  1539. if (!parentNode){
  1540. console.log('parentNode failed')
  1541. } else if( HTMLElement.prototype.prepend ){
  1542. // using prepend
  1543. HTMLElement.prototype.prepend.call(parentNode, button)
  1544.  
  1545. } else{
  1546. // using insertBefore
  1547. try{
  1548. elementInsertBefore.call(parentNode, button, parentNode.firstChild);
  1549. }catch(e){
  1550. console.log('element insert failed in old browser CE')
  1551. }
  1552. }
  1553. }
  1554. //if (button) button.parentNode.insertBefore(button, button.parentNode.firstChild)
  1555. }
  1556.  
  1557.  
  1558.  
  1559. // continuous check for element relocation
  1560. // fired at begining & window resize, etc
  1561. function mtf_append_playlist() {
  1562.  
  1563. /** @type {HTMLElement | null} */
  1564. let ytdFlexyElm = kRef(ytdFlexy);
  1565. if (!scriptEnable || !ytdFlexyElm) return;
  1566.  
  1567. /** @type {HTMLElement | null} */
  1568. const rootElement = Q.mutationTarget || ytdFlexyElm;
  1569.  
  1570. let ple1 = querySelectorFromAnchor.call(rootElement,"*:not(#ytd-userscript-playlist) > ytd-playlist-panel-renderer#playlist");
  1571. if (ple1) {
  1572. let ct = Date.now();
  1573. let truePlaylist = null;
  1574. let truePlaylist_items = document.querySelector('ytd-playlist-panel-renderer#playlist #items:not(:empty)');
  1575. if (truePlaylist_items) {
  1576.  
  1577. let truePlaylist = closestDOM.call(truePlaylist_items, '#playlist');
  1578. if(!truePlaylist || truePlaylist.nodeType!==1) truePlaylist = null;
  1579. else {
  1580. truePlaylist.setAttribute('tabview-true-playlist', ct)
  1581. }
  1582.  
  1583. }
  1584.  
  1585. if (!truePlaylist) truePlaylist = ple1; // NOT NULL
  1586.  
  1587. for (const s of document.querySelectorAll(`*:not(#ytd-userscript-playlist) > ytd-playlist-panel-renderer#playlist:not([tabview-true-playlist="${ct}"])`)){
  1588. elementRemove.call(s);
  1589. }
  1590.  
  1591. let $wrapper = getWrapper('ytd-userscript-playlist')
  1592. $wrapper.append(truePlaylist).appendTo(document.querySelector("#tab-list"));
  1593. truePlaylist.setAttribute('data-dom-changed-by-tabview-youtube', scriptVersionForExternal)
  1594. setDisplayedPlaylist(); // relocation after re-layout
  1595.  
  1596. requestAnimationFrame(() => {
  1597. let ytdFlexyElm = kRef(ytdFlexy);
  1598. if (!scriptEnable || !ytdFlexyElm) return;
  1599. if (!switchTabActivity_lastTab && (ytdFlexyElm.getAttribute('tabview-selection') + '').indexOf('#tab-') === 0 && /https\:\/\/www\.youtube\.com\/watch.*[\?\&]list=[\w\-\_]+/.test(location.href)) {
  1600. if (setToActiveTab('#tab-list')) switchTabActivity_lastTab = '#tab-list';
  1601. }
  1602. })
  1603.  
  1604. }
  1605. }
  1606.  
  1607.  
  1608. // content fix - info & playlist
  1609. // fired at begining, and keep for in case any change
  1610. function mtf_fix_details() {
  1611.  
  1612. if (!scriptEnable) return;
  1613.  
  1614. let date_now = Date.now()
  1615.  
  1616. if(no_fix_contents_until<date_now){
  1617.  
  1618. let contentToggleBtn = document.querySelector('ytd-watch-flexy #tab-info ytd-expander tp-yt-paper-button#less.ytd-expander:not([hidden]), #tab-info ytd-expander tp-yt-paper-button#more.ytd-expander:not([hidden])')
  1619.  
  1620. if(contentToggleBtn){
  1621. no_fix_contents_until = date_now + 39;
  1622. timeline.setTimeout(function() {
  1623. const domElement = contentToggleBtn;
  1624. contentToggleBtn = null;
  1625. // if(!domElement.parentElement) return; // not working in pseudo custom element - parentNode = documentFragment
  1626. const expander = closestDOM.call(domElement, 'ytd-watch-flexy #tab-info ytd-expander')
  1627. if(!expander || expander.nodeType!==1) return; // checking whether it is still on the page
  1628. if(expander.style.getPropertyValue('--ytd-expander-collapsed-height')){
  1629. expander.style.setProperty('--ytd-expander-collapsed-height','')
  1630. }
  1631. nativeCall(expander, [
  1632. {'property':'canToggleJobId','value':1}, // false disable calculateCanCollapse in childrenChanged
  1633. {'property':'alwaysToggleable','value':false}, // this is checked in childrenChanged
  1634. {'property':'recomputeOnResize','value':false}, // no need to check toggleable
  1635. {'property':'isToggled','value':true}, // show full content
  1636. {'property':'canToggle','value':false}, // hide show more or less btn
  1637. {'property':'collapsedHeight','value':999999} // disable collapsed height check
  1638. ])
  1639. }, 40);
  1640. }
  1641.  
  1642. }
  1643.  
  1644.  
  1645. if(no_fix_playlist_until<date_now){
  1646.  
  1647.  
  1648. // just in case the playlist is collapsed
  1649. let playlist = document.querySelector('#tab-list ytd-playlist-panel-renderer#playlist')
  1650. if(playlist.matches('[collapsed], [collapsible]')) {
  1651. no_fix_playlist_until= date_now + 39;
  1652. timeline.setTimeout(function() {
  1653. const domElement = playlist;
  1654. playlist = null;
  1655. // if(!domElement.parentElement || domElement.nodeType!==1) return; // not working in pseudo custom element - parentNode = documentFragment
  1656. const tablist = closestDOM.call(domElement, 'ytd-watch-flexy #tab-list')
  1657. if(!tablist || tablist.nodeType!==1) return; // checking whether it is still on the page
  1658.  
  1659. if (domElement.hasAttribute('collapsed')) wAttr(domElement, 'collapsed', false);
  1660. if (domElement.hasAttribute('collapsible')) wAttr(domElement, 'collapsible', false);
  1661. }, 40)
  1662. }
  1663.  
  1664.  
  1665. }
  1666.  
  1667.  
  1668. }
  1669.  
  1670.  
  1671.  
  1672. function isNullComments() {
  1673.  
  1674. let comments = document.querySelector('ytd-comments#comments')
  1675. if (!comments || comments.hasAttribute('hidden')) return true;
  1676.  
  1677. }
  1678.  
  1679. function _innerCommentsLoader( /** @type {HTMLElement} */ rootElement) {
  1680.  
  1681. /** @type {HTMLElement | null} */
  1682. let ytdFlexyElm = kRef(ytdFlexy);
  1683. if (!scriptEnable || !ytdFlexyElm) return;
  1684. if (deferredVarYTDHidden) return;
  1685.  
  1686.  
  1687. let messageElm, messageStr, commentRenderer;
  1688. let diffCSS = `[tabview-cache-time="${sect_hTime}"]`
  1689.  
  1690. //console.log(823100,rootElement)
  1691.  
  1692. if (commentRenderer = querySelectorFromAnchor.call(rootElement,`ytd-comments#comments #count.ytd-comments-header-renderer:not(${diffCSS})`)) {
  1693.  
  1694. scheduledCommentRefresh=false;
  1695. let eTime = Date.now();
  1696.  
  1697. sect_holder = mWeakRef(commentRenderer);
  1698. sect_hText = commentRenderer.textContent;
  1699. sect_hTime = eTime;
  1700. commentRenderer.setAttribute('tabview-cache-time', eTime);
  1701.  
  1702. return {
  1703. status: 1,
  1704. f: () => {
  1705.  
  1706. let span = document.querySelector("span#tab3-txt-loader")
  1707. let r = '0';
  1708. let txt = commentRenderer.textContent
  1709. if (typeof txt == 'string') {
  1710. let m = txt.match(/[\d\,\s]+/)
  1711. if (m) {
  1712. r = m[0].trim()
  1713.  
  1714.  
  1715. }
  1716. }
  1717. if (span){
  1718.  
  1719. let tab_btn = closestDOM.call(span,'.tab-btn[userscript-tab-content="#tab-comments"]')
  1720. if(tab_btn)tab_btn.setAttribute('loaded-comment','normal')
  1721. span.textContent = r;
  1722. }
  1723. //console.log(754)
  1724. mtoInterval = mtoInterval2;
  1725. scheduledCommentRefresh=false;
  1726. setCommentSection(1);
  1727. comments_section_loaded_elm = mWeakRef(document.querySelector('ytd-comments#comments div#contents'))
  1728. }
  1729. }
  1730.  
  1731. } else if ((messageElm = querySelectorFromAnchor.call(rootElement,`ytd-item-section-renderer#sections #header ~ #contents > ytd-message-renderer:not(${diffCSS})`)) && (messageStr = (messageElm.textContent || '').trim())) { //ytd-message-renderer
  1732. // it is possible to get the message before the header generation.
  1733. // sample link - https://www.youtube.com/watch?v=PVUZ8Nvr1ic
  1734. // sample link - https://www.youtube.com/watch?v=yz8AiQc1Bk8
  1735.  
  1736. scheduledCommentRefresh=false;
  1737. let eTime = Date.now();
  1738.  
  1739. sect_holder = mWeakRef(messageElm);
  1740. sect_hText = messageElm.textContent;
  1741. sect_hTime = eTime;
  1742. messageElm.setAttribute('tabview-cache-time', eTime);
  1743.  
  1744. return {
  1745. status: 2,
  1746. f: () => {
  1747. timeline.setTimeout(function() {
  1748. let span = document.querySelector("span#tab3-txt-loader")
  1749. const mainMsg = querySelectorFromAnchor.call(messageElm,'#message, #submessage')
  1750. if (mainMsg && mainMsg.textContent) {
  1751. for (const msg of querySelectorAllFromAnchor.call(mainMsg,'*:not(:empty)')) {
  1752. if (msg.childElementCount === 0 && msg.textContent) {
  1753. messageStr = msg.textContent.trim()
  1754. break
  1755. }
  1756. }
  1757. }
  1758. if (span){
  1759. let tab_btn = closestDOM.call(span,'.tab-btn[userscript-tab-content="#tab-comments"]')
  1760. if(tab_btn)tab_btn.setAttribute('loaded-comment','message')
  1761. span.textContent ='\u200B';
  1762. }
  1763. mtoInterval = mtoInterval2;
  1764. scheduledCommentRefresh=false;
  1765. setCommentSection(1);
  1766. comments_section_loaded_elm = mWeakRef(document.querySelector('ytd-comments#comments div#contents'))
  1767. }, 40);
  1768. }
  1769. }
  1770.  
  1771. }
  1772.  
  1773. }
  1774.  
  1775. const atChange = async ()=>{
  1776.  
  1777. // call when video change / popstate change / switch back from mini-view
  1778.  
  1779. let ytdFlexyElm = kRef(ytdFlexy);
  1780. if(!ytdFlexyElm) return;
  1781.  
  1782. mtf_forceCheckLiveVideo_disable = 0;
  1783. mtf_forceCheckLiveVideo();
  1784.  
  1785. if(Q.comments_section_loaded===0){
  1786.  
  1787. const tabBtn = document.querySelector('[userscript-tab-content="#tab-comments"].tab-btn-hidden')
  1788. if(tabBtn) {
  1789. tabBtn.classList.remove("tab-btn-hidden") //if contains
  1790. }
  1791.  
  1792. }
  1793. }
  1794.  
  1795.  
  1796.  
  1797. function hookSection(){
  1798.  
  1799.  
  1800. //console.log(3300)
  1801.  
  1802. let ytdFlexyElm = kRef(ytdFlexy);
  1803. if (!scriptEnable || !ytdFlexyElm) return ;
  1804.  
  1805. const comments = querySelectorFromAnchor.call(ytdFlexyElm,'ytd-comments#comments')
  1806. if (!comments) return ;
  1807.  
  1808. if(deferredVarYTDHidden) return;
  1809.  
  1810. //console.log(3310)
  1811.  
  1812. //new MutationObserver(function(){console.log(comments.querySelectorAll('*').length)}).observe(comments,{ attributes: true, childList: true, subtree: true })
  1813.  
  1814. function foundSection(sections){
  1815.  
  1816. //console.log(3320)
  1817.  
  1818. function ulfx() {
  1819. //console.log(1220)
  1820.  
  1821.  
  1822. const rootElement = comments.parentNode
  1823.  
  1824.  
  1825. if (pendingOne && querySelectorAllFromAnchor.call(comments,'[tabview-cache-time]').length > 1) return;
  1826. pendingOne = false;
  1827. if(!rootElement) return; //prevent unknown condition
  1828.  
  1829.  
  1830. let innerCommentsLoaderRet = _innerCommentsLoader(rootElement);
  1831.  
  1832. //console.log(1222,_innerCommentsLoader)
  1833.  
  1834. if (!innerCommentsLoaderRet) { //unable to get the status
  1835.  
  1836.  
  1837. let holder = kRef(sect_holder);
  1838. //if (holder && comments.contains(holder)) {
  1839. if (holder && elementContains.call(comments,holder)) { // previous status in page
  1840.  
  1841. if (sect_lastUpdate) return;
  1842. sect_lastUpdate = 1;
  1843.  
  1844. timeline.setTimeout(function() {
  1845. //console.log(2633)
  1846. if (holder.textContent !== sect_hText && sect_hTime > 0) {
  1847.  
  1848. if (querySelectorAllFromAnchor.call(comments,'[tabview-cache-time]').length === 1) {
  1849.  
  1850. sect_hTime--;
  1851.  
  1852. let innerCommentsLoaderRet = _innerCommentsLoader(rootElement);
  1853.  
  1854. if (innerCommentsLoaderRet) {
  1855. //console.log(9442)
  1856. innerCommentsLoaderRet.f();
  1857. fetchCommentsFinished();
  1858. }
  1859.  
  1860. pendingOne = false;
  1861.  
  1862. }
  1863.  
  1864.  
  1865. }
  1866. sect_lastUpdate = 0
  1867. }, 400)
  1868.  
  1869. } else if (sect_holder) { //previous status not in page but we have the previous text
  1870. sect_holder = null
  1871. sect_hTime = 0;
  1872. sect_lastUpdate = 0;
  1873. pendingOne = false;
  1874. timeline.setTimeout(()=>{
  1875. if(!deferredVarYTDHidden && scheduledCommentRefresh && Q.comments_section_loaded>0 && comments.hasAttribute('hidden')){
  1876.  
  1877. scheduledCommentRefresh=false; // logic tbc
  1878. resetCommentSection();
  1879. }
  1880. },142);
  1881. //Q.mutationTarget=null;
  1882. //console.log(87103)
  1883. // try to omit in 2.5.x
  1884. //FP.mtf_attrComments(); // in case for popstate
  1885. }
  1886.  
  1887.  
  1888. } else {
  1889.  
  1890. //console.log(9443)
  1891. innerCommentsLoaderRet.f();
  1892. fetchCommentsFinished();
  1893.  
  1894. pendingOne = true;
  1895.  
  1896. }
  1897.  
  1898.  
  1899.  
  1900. }
  1901.  
  1902. function observerHook(mutationsList, observer){
  1903.  
  1904. let valid = false
  1905. for (const mutation of mutationsList) {
  1906.  
  1907. let target = mutation.target;
  1908. if (!target) break;
  1909. if (!target.id) break;
  1910. let classList = target.classList;
  1911.  
  1912.  
  1913. if (
  1914. classList.contains('ytd-item-section-renderer') ||
  1915. classList.contains('ytd-comments-header-renderer') ||
  1916. classList.contains('ytd-message-renderer')) {
  1917. valid = true
  1918. break;
  1919. }
  1920.  
  1921. break; // only outest mutation
  1922.  
  1923.  
  1924. }
  1925. if (valid) ulfx();
  1926. }
  1927.  
  1928. if(sections.hasAttribute('tabview-comment-section-checked')){
  1929.  
  1930. //console.log(3612)
  1931. ulfx();
  1932.  
  1933. }else{
  1934.  
  1935. pendingOne = false;
  1936. //console.log(3611)
  1937. const observer = new MutationObserver(observerHook)
  1938. sections.setAttribute('tabview-comment-section-checked','')
  1939. const config = { childList: true, subtree: true };
  1940. observer.observe(sections, config);
  1941. ulfx();
  1942.  
  1943.  
  1944. }
  1945.  
  1946.  
  1947.  
  1948. }
  1949. //console.log(3241, location.href)
  1950. //console.log('cid_render_section',1)
  1951. let cid_render_section = timeline.setInterval(function() {
  1952.  
  1953. // console.log(3244, location.href)
  1954.  
  1955.  
  1956. const rootElement = comments.parentNode
  1957.  
  1958. let sections = querySelectorFromAnchor.call(rootElement,'ytd-comments#comments > ytd-item-section-renderer#sections');
  1959.  
  1960.  
  1961. if (sections) {
  1962.  
  1963. //console.log('cid_render_section',0)
  1964. timeline.clearInterval(cid_render_section);
  1965. foundSection(sections);
  1966.  
  1967. }
  1968.  
  1969. }, 30)
  1970.  
  1971.  
  1972.  
  1973. }
  1974.  
  1975.  
  1976. let mtc_store=0;
  1977. let mtc_cid = 0;
  1978. const cssOnceFunc = {
  1979. [`ytd-comments#comments:not([o3r-${sa_comments}])`]:(comments)=>{
  1980. // once per {ytd-comments#comments} detection
  1981.  
  1982. let ytdFlexyElm = kRef(ytdFlexy);
  1983. if (!scriptEnable || !ytdFlexyElm) return;
  1984.  
  1985. if (!comments) return;
  1986.  
  1987.  
  1988.  
  1989.  
  1990. if(mtoVisibility_Comments.bindElement(comments)){
  1991. mtoVisibility_Comments.observer.check(9);
  1992. }
  1993. pageInit_attrComments = true
  1994. hookSection();
  1995.  
  1996. //let dComments = comments
  1997. //timeline.setTimeout(() => nativeFunc(dComments, "loadComments"), 20)
  1998. //comments = null;
  1999.  
  2000. },
  2001. [`ytd-playlist-panel-renderer#playlist:not([o3r-${sa_playlist}])`]:(playlist)=>{
  2002.  
  2003. // once per {ytd-playlist-panel-renderer#playlist} detection
  2004.  
  2005. let ytdFlexyElm = kRef(ytdFlexy);
  2006. if (!scriptEnable || !ytdFlexyElm) return;
  2007.  
  2008. if (!playlist) return;
  2009.  
  2010. if(mtoVisibility_Playlist.bindElement(playlist)){
  2011. mtoVisibility_Playlist.observer.check(9) //delay check required for browser bug - hidden changed not triggered
  2012. }
  2013. playlist = null;
  2014.  
  2015. return;
  2016.  
  2017. },
  2018. '#meta-contents ytd-expander:not([tabview-info-expander])':(expander)=>{
  2019.  
  2020. // once per $$native-info-description$$ {#meta-contents ytd-expander} detection
  2021.  
  2022. let ytdFlexyElm = kRef(ytdFlexy);
  2023. if (!scriptEnable || !ytdFlexyElm) return ;
  2024.  
  2025. if (!expander) return ;
  2026. expander.setAttribute('tabview-info-expander','')
  2027. $(expander).appendTo("#tab-info").attr('data-dom-changed-by-tabview-youtube', scriptVersionForExternal)
  2028.  
  2029. },
  2030. '#description-and-actions.style-scope.ytd-watch-metadata > #description ytd-text-inline-expander:not([tabview-removed-duplicate])': (teaserInfo)=>{
  2031.  
  2032.  
  2033. // for Teaser UI
  2034. // once per {#description-and-actions.style-scope.ytd-watch-metadata > #description > ytd-text-inline-expander} detection
  2035.  
  2036. let ytdFlexyElm = kRef(ytdFlexy);
  2037. if (!scriptEnable || !ytdFlexyElm) return ;
  2038. let addedInfo = document.querySelector('#tab-info ytd-expander[tabview-info-expander]');
  2039.  
  2040. if(!addedInfo) return;
  2041.  
  2042. if(!document.documentElement.hasAttribute('tabview-injection-js-1-ready')) return;
  2043.  
  2044. teaserInfo.setAttribute('tabview-removed-duplicate','')
  2045.  
  2046.  
  2047. teaserInfo.dispatchEvent(new CustomEvent('tabview-no-duplicate-info'))
  2048. //console.log(56)
  2049.  
  2050. }
  2051. }
  2052. const cssOnce = Object.keys(cssOnceFunc).join(', ')
  2053.  
  2054. const regularCheck = (addP, removeP, mutationTarget)=>{
  2055.  
  2056.  
  2057. let isInvalidAdding = mutationTarget && !mutationTarget.parentNode
  2058.  
  2059. let promisesForAddition = !scriptEnable ? [] : addP > 0 && !isInvalidAdding ? [
  2060.  
  2061. (async () => {
  2062.  
  2063. //$callOnce('mtf_checkFlexy');
  2064.  
  2065. const rootElement = Q.mutationTarget || kRef(ytdFlexy);
  2066.  
  2067. if(rootElement){
  2068.  
  2069. let fElms = querySelectorAllFromAnchor.call(rootElement,cssOnce)
  2070. if(fElms.length>0){
  2071.  
  2072. for(const fElm of fElms){
  2073. for(let cssSelctor in cssOnceFunc){
  2074. if(fElm.matches(cssSelctor)){
  2075. let f= cssOnceFunc[cssSelctor]
  2076. if(typeof f=='function') f(fElm);
  2077. break;
  2078. }
  2079.  
  2080. }
  2081. }
  2082.  
  2083. //$callOnce('mtf_initalAttr_comments');
  2084. //$callOnce('mtf_initalAttr_playlist');
  2085. // $callOnce('mtf_checkDescriptionLoaded');
  2086. }
  2087.  
  2088. }
  2089.  
  2090. })(),
  2091. (async () => {
  2092.  
  2093.  
  2094. FP.mtf_initalAttr_chatroom();
  2095. FP.mtf_initalAttr_engagement_panel();
  2096. //$callOnce('mtf_initalAttr_chatroom');
  2097. // $callOnce('mtf_initalAttr_engagement_panel');
  2098.  
  2099. mtf_forceCheckLiveVideo();
  2100. mtf_append_comments();
  2101. mtf_liveChatBtnF();
  2102. fixTabs();
  2103. mtf_AfterFixTabs();
  2104. mtf_append_playlist();
  2105.  
  2106. })()
  2107.  
  2108. ] : [];
  2109. let promisesForEveryMutation = !scriptEnable ? [] : [
  2110. (async () => {
  2111. mtf_fix_details();
  2112. mtf_ChatExist();
  2113. })()
  2114. ];
  2115.  
  2116. return [...promisesForAddition, ...promisesForEveryMutation]
  2117.  
  2118.  
  2119. }
  2120.  
  2121. function setOnlyOneEPanel(ePanel){
  2122.  
  2123. layoutStatusMutex.lockWith(unlock => {
  2124.  
  2125. let cPanels = engagement_panels_();
  2126. for (const entry of cPanels.list) {
  2127. if (entry.ePanel != ePanel && entry.visible) ytBtnCloseEngagementPanel(entry.ePanel);
  2128. }
  2129. setTimeout(unlock, 30)
  2130.  
  2131. })
  2132.  
  2133. }
  2134.  
  2135. const FP = {
  2136.  
  2137. mtoNav_delayedF: () => {
  2138.  
  2139. let { addP, removeP, mutationTarget } = Q;
  2140.  
  2141. Q.addP = 0;
  2142. Q.removeP = 0;
  2143. mtc_store=Date.now()+1870
  2144.  
  2145.  
  2146. let regularChecks = regularCheck(addP, removeP, mutationTarget);
  2147.  
  2148. Promise.all(regularChecks).then(() => {
  2149. regularChecks = null;
  2150. mtc_store= Date.now() + 3870 // pending Comments start to load
  2151. Q.mutationTarget = null;
  2152.  
  2153. Q.mtoNav_requestNo--;
  2154. //console.log('motnav reduced to', mtoNav_requestNo)
  2155. if (Q.mtoNav_requestNo > 0) {
  2156. Q.mtoNav_requestNo = 1;
  2157. setTimeout(FP.mtoNav_delayedF, mtoInterval);
  2158. }
  2159. })
  2160.  
  2161.  
  2162. },
  2163.  
  2164. mtoNavF: ( /** @type {MutationRecord[]} */ mutations, /** @type {MutationObserver} */ observer) => {
  2165. //subtree DOM mutation checker - {ytd-watch-flexy} \single \subtree
  2166.  
  2167. newVideoPageCheck()
  2168.  
  2169. if (!scriptEnable) return;
  2170. if (deferredVarYTDHidden) return;
  2171.  
  2172. let ch = false;
  2173.  
  2174. let reg = [];
  2175. let dTarget = null;
  2176.  
  2177. let wAddP = 0,
  2178. wRemoveP = 0;
  2179.  
  2180. let _last_mto_target = null;
  2181. let _last_mto_target_valid = null;
  2182.  
  2183. for (const mutation of mutations) {
  2184. if (!mutation || !mutation.target || !mutation.target.parentNode) continue;
  2185.  
  2186. let elementalMutation = false;
  2187. let tAddP = 0,
  2188. tRemoveP = 0;
  2189.  
  2190. for (const addedNode of mutation.addedNodes) { //theoretically faster: only reading of states
  2191. if (addedNode.nodeType === 1) {
  2192. tAddP++;
  2193. elementalMutation = true;
  2194. }
  2195. }
  2196.  
  2197. for (const removedNode of mutation.removedNodes) { //theoretically faster: only reading of states
  2198. if (removedNode.nodeType === 1) {
  2199. tRemoveP++;
  2200. elementalMutation = true;
  2201. }
  2202. }
  2203.  
  2204. if (elementalMutation) { //skip all addition and removal operations without elemental changes (e.g. textNode modification)
  2205.  
  2206. if (_last_mto_target === mutation.target) {
  2207. // due to addition and removal operations to the same DOM
  2208. if (_last_mto_target_valid) {
  2209. // AddP & RemoveP is still valid
  2210. wAddP += tAddP;
  2211. wRemoveP += tRemoveP;
  2212. }
  2213. continue;
  2214. }
  2215. _last_mto_target = mutation.target;
  2216.  
  2217. if (isMtoTargetSkip(mutation)) {
  2218. _last_mto_target_valid = false;
  2219. continue; //theoretically slower: creation of string variables
  2220. } else {
  2221. _last_mto_target_valid = true;
  2222. wAddP += tAddP;
  2223. wRemoveP += tRemoveP;
  2224. }
  2225.  
  2226.  
  2227.  
  2228. ch = true;
  2229.  
  2230. reg.push(mutation);
  2231.  
  2232. if (dTarget === null) dTarget = mutation.target; //first
  2233. else if (dTarget === true) {} //ytdFlexy
  2234. else if ( elementContains.call(dTarget, mutation.target)) {} //first node is the container to all subsequential targets
  2235. else { dTarget = true; } //the target is not the child of first node
  2236.  
  2237. }
  2238.  
  2239. }
  2240.  
  2241. if (!ch) return; // dTarget must be true OR HTMLElement
  2242.  
  2243. if (dTarget === true) dTarget = kRef(ytdFlexy); // major mutation occurance
  2244. else if (dTarget === kRef(comments_section_loaded_elm) && wAddP > wRemoveP) return true; // ignore if comments are loaded (adding comments)
  2245. else if (isMtoOverallSkip(dTarget)) return; // allow for multiple mutations at the same time - determinated after looping
  2246.  
  2247.  
  2248.  
  2249. // 4 ~ 16 times per full page loading
  2250.  
  2251. Q.addP += wAddP;
  2252. Q.removeP += wRemoveP;
  2253.  
  2254. if (Q.mutationTarget === null) Q.mutationTarget = dTarget;
  2255. else if (Q.mutationTarget != dTarget) Q.mutationTarget = kRef(ytdFlexy);
  2256.  
  2257. //console.log(prettyElm(dTarget), wAddP , wRemoveP, mtoInterval)
  2258. //console.log(prettyElm(dTarget), reg.map(m=>prettyElm(m.target)))
  2259. //console.log(7015, performance.now())
  2260.  
  2261.  
  2262.  
  2263. Q.mtoNav_requestNo++;
  2264. if (Q.mtoNav_requestNo == 1) setTimeout(FP.mtoNav_delayedF, mtoInterval);
  2265.  
  2266. },
  2267.  
  2268.  
  2269. mtoBodyF: ( /** @type {MutationRecord[]} */ mutations, /** @type {MutationObserver} */ observer) => {
  2270. //subtree DOM mutation checker - {body} \single \subtree
  2271.  
  2272. if (!scriptEnable) return;
  2273. if (deferredVarYTDHidden) return;
  2274.  
  2275. for (const mutation of mutations) {
  2276. for (const addedNode of mutation.addedNodes)
  2277. if (addedNode.nodeType === 1) {
  2278. if (addedNode.nodeName == "DIV" && addedNode.matches('.autocomplete-suggestion:not([autocomplete-disable-updatesc])') ) {
  2279. mtf_fixAutoCompletePosition(addedNode)
  2280. }else if(addedNode.nodeName == "DIV" && (addedNode.id==='lyricscontainer' || addedNode.id==='showlyricsbutton')){
  2281.  
  2282. goYoutubeGeniusLyrics();
  2283.  
  2284. }
  2285. }
  2286. }
  2287.  
  2288. },
  2289.  
  2290. mtf_attrPlaylist: (attrName, newValue) => {
  2291. //attr mutation checker - {ytd-playlist-panel-renderer#playlist} \single
  2292. //::attr ~ hidden
  2293. //console.log(1210)
  2294.  
  2295. if (!scriptEnable) return;
  2296. if (deferredVarYTDHidden) return;
  2297. let cssElm = kRef(ytdFlexy);
  2298. if (!cssElm) return;
  2299.  
  2300. let playlist = document.querySelector('ytd-playlist-panel-renderer#playlist')
  2301. const tabBtn = document.querySelector('[userscript-tab-content="#tab-list"]');
  2302. let isPlaylistHidden = playlist.hasAttribute('hidden')
  2303. //console.log(1212.2, isPlaylistHidden, playlist.getAttribute('hidden'))
  2304. if (tabBtn) {
  2305. //console.log('attr playlist changed')
  2306. if (tabBtn.classList.contains('tab-btn-hidden') && !isPlaylistHidden) {
  2307. //console.log('attr playlist changed - no hide')
  2308. tabBtn.classList.remove("tab-btn-hidden");
  2309. } else if (!tabBtn.classList.contains('tab-btn-hidden') && isPlaylistHidden) {
  2310. //console.log('attr playlist changed - add hide')
  2311. hideTabBtn(tabBtn);
  2312. }
  2313. }
  2314. /* visible layout for triggering hidden removal */
  2315. akAttr(cssElm, 'tabview-youtube-playlist', isPlaylistHidden);
  2316. },
  2317. mtf_attrComments: (attrName, newValue) => {
  2318. //attr mutation checker - {ytd-comments#comments} \single
  2319. //::attr ~ hidden
  2320.  
  2321. let ytdFlexyElm = kRef(ytdFlexy);
  2322. if (!scriptEnable || !ytdFlexyElm) return;
  2323. if (deferredVarYTDHidden) return;
  2324.  
  2325. let comments = document.querySelector('ytd-comments#comments')
  2326. const tabBtn = document.querySelector('[userscript-tab-content="#tab-comments"]');
  2327. if (!comments || !tabBtn) return;
  2328. let isCommentHidden = comments.hasAttribute('hidden')
  2329. //console.log('attr comments changed')
  2330.  
  2331. mtoInterval = mtoInterval1;
  2332. mtc_store=Date.now()+2870
  2333.  
  2334. if( mtf_forceCheckLiveVideo_disable === 2 ){
  2335.  
  2336. }else if (!isCommentHidden) {
  2337.  
  2338. akAttr(ytdFlexyElm, 'tabview-youtube-comments', false, 'K');
  2339.  
  2340.  
  2341.  
  2342. //console.log('attr comments changed - no hide')
  2343. tabBtn.classList.remove("tab-btn-hidden") //if contains
  2344.  
  2345. //console.log(703)
  2346.  
  2347. mtc_cid&&timeline.clearInterval(mtc_cid);
  2348.  
  2349. } else if (isCommentHidden) {
  2350.  
  2351. akAttr(ytdFlexyElm, 'tabview-youtube-comments', true, 'K');
  2352. timeline.setTimeout(function(){
  2353. if(!deferredVarYTDHidden && scheduledCommentRefresh && Q.comments_section_loaded>0 && comments.hasAttribute('hidden')){
  2354. //console.log(3434)
  2355. scheduledCommentRefresh=false; //logic tbc
  2356. resetCommentSection();
  2357. //console.log(8022)
  2358. }
  2359. },142)
  2360.  
  2361. if(!mtc_cid) mtc_cid=timeline.setInterval(()=>{
  2362. if(mtc_store>Date.now()) return;
  2363. timeline.clearInterval(mtc_cid)
  2364. mtc_cid=0;
  2365. if(mtf_forceCheckLiveVideo_disable===2)return;
  2366. if(isNullComments()) _disableComments();
  2367.  
  2368. },80)
  2369.  
  2370. }
  2371.  
  2372.  
  2373. },
  2374.  
  2375.  
  2376. mtf_attrChatroom: (attrName, newValue) => {
  2377. //attr mutation checker - {ytd-live-chat-frame#chat} \single
  2378. //::attr ~ collapsed
  2379.  
  2380. let ytdFlexyElm = kRef(ytdFlexy);
  2381. if (!scriptEnable || !ytdFlexyElm) return;
  2382. if (deferredVarYTDHidden) return;
  2383.  
  2384. layoutStatusMutex.lockWith(unlock => {
  2385.  
  2386. const chatBlock = document.querySelector('ytd-live-chat-frame#chat')
  2387. const cssElm = kRef(ytdFlexy)
  2388.  
  2389. if (!chatBlock || !cssElm) {
  2390. unlock();
  2391. return;
  2392. }
  2393.  
  2394. if (deferredVarYTDHidden) {
  2395. unlock();
  2396. return;
  2397. }
  2398.  
  2399. if (!cssElm.hasAttribute('userscript-chatblock')) wAttr(cssElm, 'userscript-chatblock', true);
  2400. let isCollapsed = !!chatBlock.hasAttribute('collapsed');
  2401. wAttr(cssElm, 'userscript-chat-collapsed', isCollapsed);
  2402.  
  2403. if (cssElm.hasAttribute('userscript-chatblock') && !isCollapsed) lastShowTab = '#chatroom';
  2404.  
  2405. if (!isCollapsed && document.querySelector('#right-tabs .tab-btn.active') && isWideScreenWithTwoColumns() && !isTheater()) {
  2406. switchTabActivity(null);
  2407. timeline.setTimeout(unlock, 40);
  2408. } else {
  2409. unlock();
  2410. }
  2411.  
  2412. if (!isCollapsed) {
  2413. runAfterExpandChat();
  2414. } else {
  2415. chatBlock.removeAttribute('yt-userscript-iframe-loaded');
  2416. }
  2417.  
  2418. })
  2419.  
  2420.  
  2421.  
  2422.  
  2423. },
  2424.  
  2425. mtf_attrEngagementPanel: ( /** @type {MutationRecord[]} */ mutations, /** @type {MutationObserver} */ observer) => {
  2426. //attr mutation checker - {ytd-engagement-panel-section-list-renderer} \mutiple
  2427. //::attr ~ visibility
  2428.  
  2429. let ytdFlexyElm = kRef(ytdFlexy);
  2430. if (!scriptEnable || !ytdFlexyElm) return;
  2431.  
  2432. //multiple instance
  2433. if (mutations) {
  2434. for (const mutation of mutations) {
  2435. let ePanel = mutation.target;
  2436. if (ePanel.getAttribute('visibility') == 'ENGAGEMENT_PANEL_VISIBILITY_EXPANDED') {
  2437. setOnlyOneEPanel(ePanel);
  2438. break;
  2439. }
  2440. }
  2441. }
  2442. if (deferredVarYTDHidden) return;
  2443.  
  2444. layoutStatusMutex.lockWith(unlock => {
  2445.  
  2446. const ePanel = document.querySelector('ytd-watch-flexy ytd-engagement-panel-section-list-renderer')
  2447. const cssElm = kRef(ytdFlexy)
  2448.  
  2449. if (!ePanel || !cssElm) {
  2450. unlock();
  2451. return;
  2452. }
  2453. let previousValue = +cssElm.getAttribute('userscript-engagement-panel') || 0;
  2454.  
  2455. let { shownRes, value, count } = engagement_panels_();
  2456. let nextValue = value;
  2457. let nextCount = count;
  2458.  
  2459.  
  2460. if (nextCount == 0 && cssElm.hasAttribute('userscript-engagement-panel')) {
  2461. storeLastPanel=null;
  2462. wAttr(cssElm, 'userscript-engagement-panel', false);
  2463. unlock();
  2464. } else {
  2465.  
  2466. if ((nextCount > 1) || (cssElm.hasAttribute('userscript-engagement-panel') && previousValue === nextValue)) {
  2467. unlock();
  2468. return;
  2469. }
  2470.  
  2471. cssElm.setAttribute('userscript-engagement-panel', nextValue);
  2472.  
  2473. let b = false;
  2474. if (previousValue != nextValue && nextValue > 0) {
  2475. lastShowTab = `#engagement-panel-${nextValue}`;
  2476. b = true;
  2477. storeLastPanel = mWeakRef( shownRes[0])
  2478. //console.log(9999, shownRes[0])
  2479. }
  2480.  
  2481. if (b && document.querySelector('#right-tabs .tab-btn.active') && isWideScreenWithTwoColumns() && !isTheater()) {
  2482. switchTabActivity(null);
  2483. timeline.setTimeout(unlock, 40);
  2484. } else {
  2485. unlock();
  2486. }
  2487. }
  2488.  
  2489. })
  2490.  
  2491.  
  2492.  
  2493.  
  2494. },
  2495.  
  2496. mtf_initalAttr_playlist: () => {
  2497. },
  2498.  
  2499. mtf_initalAttr_comments: () => {
  2500. },
  2501.  
  2502. mtf_initalAttr_chatroom: () => {
  2503. // every per [new] {ytd-live-chat-frame#chat} detection - reset after mini-playview
  2504. let ytdFlexyElm = kRef(ytdFlexy);
  2505. if (!scriptEnable || !ytdFlexyElm) return true;
  2506.  
  2507. const rootElement = Q.mutationTarget || ytdFlexyElm;
  2508.  
  2509.  
  2510. if (!mgChatFrame.inPage()) {
  2511.  
  2512. mtoVisibility_Chatroom.clear(true);
  2513.  
  2514. let chatroom = querySelectorFromAnchor.call(rootElement,`ytd-live-chat-frame#chat:not([${sa_chatroom}]`)
  2515. if (chatroom) {
  2516. mgChatFrame.setVar(chatroom);
  2517.  
  2518. //console.log(124,chatroom)
  2519. if(mtoVisibility_Chatroom.bindElement(chatroom)){
  2520. mtoVisibility_Chatroom.observer.check(9)
  2521. }
  2522.  
  2523. chatroom = null
  2524. }
  2525. }
  2526. return true;
  2527.  
  2528. },
  2529.  
  2530. mtf_initalAttr_engagement_panel: () => {
  2531. // every per $$non-checked-section-list$$ {ytd-watch-flexy ytd-engagement-panel-section-list-renderer:not([o3r-XXXXX])} detection
  2532. let ytdFlexyElm = kRef(ytdFlexy);
  2533. if (!scriptEnable || !ytdFlexyElm) return true;
  2534.  
  2535. const rootElement = Q.mutationTarget || ytdFlexyElm;
  2536.  
  2537. let toCheck = false;
  2538. for (const engagement_panel of querySelectorAllFromAnchor.call(rootElement,
  2539. `ytd-watch-flexy ytd-engagement-panel-section-list-renderer:not([o3r-${sa_epanel}])`
  2540. )) {
  2541.  
  2542.  
  2543.  
  2544. let bindRes = mtoVisibility_EngagementPanel.bindElement(engagement_panel, {
  2545. attributes: true,
  2546. attributeFilter: ['visibility'],
  2547. attributeOldValue: true
  2548. })
  2549.  
  2550. if(bindRes) toCheck = true;
  2551. }
  2552.  
  2553. if (toCheck) FP.mtf_attrEngagementPanel()
  2554.  
  2555. return true;
  2556. },
  2557.  
  2558.  
  2559.  
  2560. //live-chat / chat-replay
  2561.  
  2562. fireOnce_forceCheckLiveVideo_tf: () => {
  2563. // once per $$player-playback-timestamp$$ {#ytd-player .ytp-time-display} && $$chat-frame$$ {ytd-live-chat-frame#chat} detection
  2564.  
  2565. let ytdFlexyElm = kRef(ytdFlexy);
  2566. if (!scriptEnable || !ytdFlexyElm) return;
  2567.  
  2568. let elm = document.querySelector('#ytd-player .ytp-time-display');
  2569. if (elm && elm.classList.contains('ytp-live')) {
  2570. //console.log(7006)
  2571. ytdFlexyElm.setAttribute('userscript-chatblock', 'chat-live')
  2572. //console.log(2441)
  2573. mtf_forceCheckLiveVideo_disable = 2;
  2574. _disableComments();
  2575. //console.log(701)
  2576. //disableComments_LiveChat();
  2577. }
  2578. },
  2579.  
  2580.  
  2581. //comments
  2582. /*
  2583. mtf_advancedComments: () => {
  2584. // once per {ytd-comments#comments #continuations} detection
  2585.  
  2586. //let ytdFlexyElm = kRef(ytdFlexy);
  2587. //if (!scriptEnable || !ytdFlexyElm) return true;
  2588.  
  2589. //const rootElement = Q.mutationTarget || ytdFlexyElm;
  2590. //const continuations = document.querySelector("ytd-comments#comments #continuations");
  2591. //if (!continuations) return true;
  2592. //requestingComments = document.querySelector('ytd-comments#comments');
  2593. //scrollForComments();
  2594. return false;
  2595. }
  2596. */
  2597.  
  2598.  
  2599. }
  2600.  
  2601.  
  2602.  
  2603.  
  2604.  
  2605. /** @type {WeakRef | null} */
  2606. let displayedPlaylist = null;
  2607. /** @type {WeakRef | null} */
  2608. let scrollingVideosList = null;
  2609.  
  2610. let scriptEnable = false;
  2611. let scriptEC = 0;
  2612. let lastShowTab = null;
  2613.  
  2614. let _cachedLastVideo = null;
  2615. let videoListBeforeSearch = null;
  2616. let no_fix_contents_until = 0;
  2617. let no_fix_playlist_until = 0;
  2618. let statusCollasped = 0;
  2619.  
  2620. /** @type {WeakRef | null} */
  2621. let ytdFlexy = null;
  2622.  
  2623. function pluginUnhook() {
  2624. _pluginUnhook();
  2625. resetCommentSection();
  2626. }
  2627.  
  2628. function _pluginUnhook() {
  2629.  
  2630. sect_lastUpdate = 0;
  2631. sect_holder = null;
  2632. sect_hTime = 0;
  2633.  
  2634. //console.log(8001)
  2635.  
  2636. videoListBeforeSearch = null;
  2637. statusCollasped = 0;
  2638. _cachedLastVideo = null;
  2639. lastShowTab = null;
  2640. displayedPlaylist = null;
  2641. scrollingVideosList = null;
  2642. scriptEnable = false;
  2643. scriptEC++;
  2644. if (scriptEC > 788888888) scriptEC = 188888888;
  2645. ytdFlexy = null;
  2646. wls.layoutStatus = null;
  2647.  
  2648. //console.log('unc01')
  2649.  
  2650. mtoVisibility_EngagementPanel.clear(true)
  2651. mtoVisibility_Playlist.clear(true)
  2652. mtoVisibility_Comments.clear(true)
  2653.  
  2654. mgChatFrame.kVar = null;
  2655. mtoVisibility_Chatroom.clear(true)
  2656. mtoFlexyAttr.clear(true)
  2657.  
  2658.  
  2659. for (const elem of document.querySelectorAll(
  2660. ['ytd-expander[tabview-info-expander]'].join(', ')
  2661. )) {
  2662. elem.removeAttribute('tabview-info-expander');
  2663. }
  2664.  
  2665. mtoMutation_body.clear(true)
  2666. mtoMutation_watchFlexy.clear(true)
  2667. //FOnce.mtf_checkFlexy = null;
  2668. //FOnce.mtf_initalAttr_comments = null;
  2669. //FOnce.mtf_initalAttr_playlist = null;
  2670. //FOnce.mtf_initalAttr_chatroom = null;
  2671. //FOnce.mtf_initalAttr_engagement_panel = null;
  2672. //FOnce.mtf_advancedComments = null;
  2673. //FOnce.mtf_checkDescriptionLoaded = null;
  2674. //FOnce.mtf_forceCheckLiveVideo = null;
  2675. Q.mtf_chatBlockQ = null;
  2676.  
  2677.  
  2678. mtoInterval = mtoInterval1;
  2679.  
  2680. }
  2681.  
  2682. let comments_section_loaded_elm = null;
  2683.  
  2684.  
  2685.  
  2686. function getTabsHTML() {
  2687.  
  2688. const sTabBtnVideos = `${svgElm(16,16,298,298,svgVideos)}<span>Videos</span>`
  2689. const sTabBtnInfo = `${svgElm(16,16,23.625,23.625,svgInfo)}<span>Info</span>`
  2690. const sTabBtnPlayList = `${svgElm(16,16,426.667,426.667,svgPlayList)}<span>Playlist</span>`
  2691.  
  2692. let str1 = `
  2693. <paper-ripple class="style-scope yt-icon-button">
  2694. <div id="background" class="style-scope paper-ripple" style="opacity:0;"></div>
  2695. <div id="waves" class="style-scope paper-ripple"></div>
  2696. </paper-ripple>
  2697. `;
  2698.  
  2699. let str_fbtns = `
  2700. <div class="font-size-right">
  2701. <div class="font-size-btn font-size-plus">
  2702. <svg width="12" height="12" viewbox="0 0 50 50" preserveAspectRatio="xMidYMid meet"
  2703. stroke="currentColor" stroke-width="6" stroke-linecap="round" vector-effect="non-scaling-size">
  2704. <path d="M12 25h26"/><path d="M25 12v26"/>
  2705. </svg>
  2706. </div><div class="font-size-btn font-size-minus">
  2707. <svg width="12" height="12" viewbox="0 0 50 50" preserveAspectRatio="xMidYMid meet"
  2708. stroke="currentColor" stroke-width="6" stroke-linecap="round" vector-effect="non-scaling-size">
  2709. <path d="M12 25h26"/>
  2710. </svg>
  2711. </div>
  2712. </div>
  2713. `.replace(/[\r\n]+/g,'')
  2714.  
  2715. const str_tabs = [
  2716. `<a id="tab-btn1" data-name="info" userscript-tab-content="#tab-info" class="tab-btn">${sTabBtnInfo}${str1}${str_fbtns}</a>`,
  2717. `<a id="tab-btn3" userscript-tab-content="#tab-comments" data-name="comments" class="tab-btn">${svgElm(16,16,60,60,svgComments)}<span id="tab3-txt-loader"></span>${str1}${str_fbtns}</a>`,
  2718. `<a id="tab-btn4" userscript-tab-content="#tab-videos" data-name="videos" class="tab-btn">${sTabBtnVideos}${str1}${str_fbtns}</a>`,
  2719. `<a id="tab-btn5" userscript-tab-content="#tab-list" class="tab-btn">${sTabBtnPlayList}${str1}${str_fbtns}</a>`
  2720. ].join('')
  2721.  
  2722. let addHTML = `
  2723. <div id="right-tabs">
  2724. <header>
  2725. <div id="material-tabs">
  2726. ${str_tabs}
  2727. </div>
  2728. </header>
  2729. <div class="tab-content">
  2730. <div id="tab-info" class="tab-content-cld tab-content-hidden" userscript-scrollbar-render></div>
  2731. <div id="tab-comments" class="tab-content-cld tab-content-hidden" userscript-scrollbar-render></div>
  2732. <div id="tab-videos" class="tab-content-cld tab-content-hidden" userscript-scrollbar-render></div>
  2733. <div id="tab-list" class="tab-content-cld tab-content-hidden" userscript-scrollbar-render></div>
  2734. </div>
  2735. </div>
  2736. `;
  2737.  
  2738. return addHTML
  2739.  
  2740. }
  2741.  
  2742.  
  2743. function onVideoChange() {
  2744. // for popstate & next video
  2745.  
  2746. newVideoPageCheck();
  2747.  
  2748. scheduledCommentRefresh=true;
  2749. let comments = document.querySelector('ytd-comments#comments')
  2750. if(!deferredVarYTDHidden && scheduledCommentRefresh && Q.comments_section_loaded>0 && comments.hasAttribute('hidden')){
  2751.  
  2752. scheduledCommentRefresh=false;
  2753. // comment tab btn - hide by timer in attribute change
  2754. resetCommentSection();
  2755. }
  2756.  
  2757. mtf_forceCheckLiveVideo_disable = 0
  2758. mtc_store= Date.now()+3870
  2759.  
  2760. if (deferredVarYTDHidden) { // reset info when hidden
  2761. resetCommentSection();
  2762. }else{
  2763. checkVisibleEngagementPanel();
  2764. atChange(); // in case no mutation occurance
  2765. }
  2766.  
  2767.  
  2768.  
  2769. }
  2770.  
  2771. function onNavigationEnd(evt) {
  2772. console.log('yt-navigate-finish')
  2773. no_fix_contents_until = 0;
  2774. no_fix_playlist_until = 0;
  2775. document.documentElement.setAttribute('youtube-ready','')
  2776. script_inject_js1.inject();
  2777. document.documentElement.dispatchEvent(new CustomEvent('tabview-ce-hack'))
  2778.  
  2779. forceConfig();
  2780. /*
  2781. console.log(window.ytcfg)
  2782. try{
  2783. window.ytcfg.set({
  2784. "EXPERIMENT_FLAGS": {"kevlar_watch_metadata_refresh":false}})
  2785. }catch(e){}
  2786. */
  2787.  
  2788. newVideoPageCheck(); // required for init
  2789.  
  2790. let mRet =
  2791. (/^https?\:\/\/(\w+\.)*youtube\.com\/watch\?(\w+\=[^\/\?\&]+\&|\w+\=?\&)*v=[\w\-\_]+/.test(window.location.href) ? 1 : 0) +
  2792. (document.querySelector('ytd-watch-flexy[tabview-selection]') ? 2 : 0);
  2793.  
  2794. if (mRet === 1) {
  2795.  
  2796. document.addEventListener('loadedmetadata', function(evt) {
  2797. if (evt.target.matches('video[tabview-mainvideo]')) {
  2798. setTimeout(onVideoChange, 30)
  2799. }
  2800. }, true)
  2801.  
  2802. pluginUnhook(); // in case not triggered by popstate - say mini playing
  2803.  
  2804. let timeout = 4; // max. 4 animation frames
  2805.  
  2806. let tf = () => {
  2807.  
  2808. if (--timeout > 0 && !document.querySelector('#player video')) return requestAnimationFrame(tf);
  2809.  
  2810. let ytdFlexyElm = document.querySelector('ytd-watch-flexy')
  2811.  
  2812. if(!ytdFlexyElm) return;
  2813.  
  2814.  
  2815. scriptEnable = true;
  2816. scriptEC++;
  2817.  
  2818. no_fix_contents_until = 0;
  2819. no_fix_playlist_until = 0;
  2820.  
  2821. ytdFlexy = mWeakRef(ytdFlexyElm)
  2822.  
  2823. let timeoutR_findRelated = new Timeout();
  2824. timeoutR_findRelated.set(function() {
  2825. let ytdFlexyElm = kRef(ytdFlexy);
  2826. if(!ytdFlexyElm) return true;
  2827. let related = querySelectorFromAnchor.call(ytdFlexyElm,"#related");
  2828. if (!related) return true;
  2829. foundRelated(related);
  2830. }, 100, 10)
  2831.  
  2832. function foundRelated(related) {
  2833. let promise = Promise.resolve();
  2834. if (!document.querySelector("#right-tabs")) {
  2835. promise = promise.then(() => {
  2836. $(getTabsHTML()).insertBefore(related).attr('data-dom-created-by-tabview-youtube', scriptVersionForExternal);
  2837. })
  2838. }
  2839. promise.then(runAfterTabAppended)
  2840. }
  2841.  
  2842. /*
  2843. setTimeout(() => {
  2844. for (const s of document.querySelectorAll('#right-tabs [userscript-scrollbar-render]')) {
  2845. Promise.resolve(s).then(s => {
  2846. if (s && s.scrollTop > 0) s.scrollTop = 0;
  2847. let child = s.firstElementChild;
  2848. if (child && child.scrollTop > 0) child.scrollTop = 0;
  2849. });
  2850. }
  2851. }, 90)
  2852. */
  2853.  
  2854.  
  2855. }
  2856.  
  2857. tf();
  2858.  
  2859. } else if (mRet === 3) {
  2860.  
  2861. if(!scriptEnable) return;
  2862.  
  2863. let elmComments = document.querySelector('ytd-comments#comments')
  2864.  
  2865. if (elmComments) {
  2866. nativeFunc(elmComments, "loadComments")
  2867. }
  2868.  
  2869. } else if (mRet === 0) {
  2870.  
  2871. pluginUnhook(); // in case not triggered by popstate - say mini playing
  2872.  
  2873. }
  2874.  
  2875. if(mRet & 1){
  2876. setTimeout(()=>{
  2877. Q.mutationTarget=null;
  2878. atChange(); // in case no mutation occurance
  2879. //console.log(87100)
  2880. // try to omit in 2.5.x
  2881. //FP.mtf_attrComments()
  2882. },160);
  2883. }
  2884.  
  2885. }
  2886.  
  2887.  
  2888. function setToActiveTab(defaultTab) {
  2889. if (isTheater() && isWideScreenWithTwoColumns()) return;
  2890. const jElm = document.querySelector(`a[userscript-tab-content="${switchTabActivity_lastTab}"]:not(.tab-btn-hidden)`) ||
  2891. document.querySelector(`a[userscript-tab-content="${(defaultTab||settings.defaultTab)}"]:not(.tab-btn-hidden)`) ||
  2892. document.querySelector("a[userscript-tab-content]:not(.tab-btn-hidden)") ||
  2893. null;
  2894. switchTabActivity(jElm);
  2895. return !!jElm;
  2896. }
  2897.  
  2898. function getWrapper(wrapperId) {
  2899. let $wrapper = $(`#${wrapperId}`);
  2900. if (!$wrapper[0]) $wrapper = $(`<div id="${wrapperId}"></div>`)
  2901. return $wrapper.first();
  2902. }
  2903.  
  2904. function runAfterTabAppended() {
  2905.  
  2906. document.documentElement.setAttribute('plugin-tabview-youtube', '')
  2907.  
  2908. const ytdFlexyElm = kRef(ytdFlexy)
  2909. if(!ytdFlexyElm) return;
  2910. if (!ytdFlexyElm.hasAttribute('tabview-selection')) ytdFlexyElm.setAttribute('tabview-selection', '')
  2911.  
  2912.  
  2913. //console.log('unc02')
  2914.  
  2915. // append the next videos
  2916. // it exists as "related" is already here
  2917. fixTabs();
  2918.  
  2919. // just switch to the default tab
  2920. setToActiveTab();
  2921.  
  2922.  
  2923. prepareTabBtn();
  2924.  
  2925.  
  2926. // append the detailed meta contents to the tab-info
  2927. // ** FOnce.mtf_checkDescriptionLoaded = FP.mtf_checkDescriptionLoaded;
  2928. // ** if (Q.mutationTarget === null) $callOnceAsync('mtf_checkDescriptionLoaded');
  2929.  
  2930. // force window scroll when #continuations is first detected and #comments still [hidden]
  2931. //FOnce.mtf_advancedComments = FP.mtf_advancedComments;
  2932. //if (Q.mutationTarget === null) $callOnceAsync('mtf_advancedComments');
  2933.  
  2934. // use video player's element to detect the live-chat situation (no commenting section)
  2935. // this would be very useful if the live chat is collapsed, i.e. iframe has no indication on the where it is live or replay
  2936. //FOnce.mtf_forceCheckLiveVideo = FP.mtf_forceCheckLiveVideo;
  2937. //if (Q.mutationTarget === null) $callOnceAsync('mtf_forceCheckLiveVideo');
  2938. //mtf_forceCheckLiveVideo();
  2939.  
  2940.  
  2941. // Attr Mutation Observer - #playlist - hidden
  2942. //clearMutationObserver(mtoVs, 'mtoVisibility_Playlist')
  2943. // Attr Mutation Observer callback - #playlist - hidden
  2944.  
  2945. // pending for #playlist and set Attribute Observer
  2946. // ** FOnce.mtf_initalAttr_playlist = FP.mtf_initalAttr_playlist
  2947. // ** if (Q.mutationTarget === null) $callOnceAsync('mtf_initalAttr_playlist');
  2948.  
  2949. // Attr Mutation Observer - ytd-comments#comments - hidden
  2950. //clearMutationObserver(mtoVs, 'mtoVisibility_Comments')
  2951. // Attr Mutation Observer callback - ytd-comments#comments - hidden
  2952.  
  2953. // pending for #comments and set Attribute Observer
  2954. // ** FOnce.mtf_initalAttr_comments = FP.mtf_initalAttr_comments;
  2955. // ** if (Q.mutationTarget === null) $callOnceAsync('mtf_initalAttr_comments');
  2956.  
  2957.  
  2958. //clearMutationObserver(mtoVs, 'mtoVisibility_Chatroom');
  2959. //FOnce.mtf_initalAttr_chatroom = FP.mtf_initalAttr_chatroom
  2960. //if (Q.mutationTarget === null) $callOnceAsync('mtf_initalAttr_chatroom');
  2961.  
  2962. // clearMutationObserver(mtoVs, 'mtoVisibility_EngagementPanel');
  2963. // for (const engagement_panel of document.querySelectorAll('ytd-watch-flexy ytd-engagement-panel-section-list-renderer:not([tabview-attr-checked])')) {
  2964. // engagement_panel.removeAttribute('tabview-attr-checked');
  2965. // }
  2966. //FOnce.mtf_initalAttr_engagement_panel = FP.mtf_initalAttr_engagement_panel
  2967. //if (Q.mutationTarget === null) $callOnceAsync('mtf_initalAttr_engagement_panel');
  2968.  
  2969.  
  2970. mtoFlexyAttr.clear(true)
  2971. mtf_checkFlexy()
  2972.  
  2973.  
  2974. document.querySelector("#right-tabs .tab-content").addEventListener('scroll', makeBodyScrollByEvt, true);
  2975.  
  2976.  
  2977. Q.addP = 0;
  2978. Q.removeP = 0;
  2979. Q.mutationTarget = null;
  2980.  
  2981. Q.mtoNav_requestNo = 0;
  2982.  
  2983.  
  2984. mtoMutation_watchFlexy.bindElement(ytdFlexyElm, {
  2985. childList: true,
  2986. subtree: true,
  2987. attributes: false
  2988. })
  2989.  
  2990. // for automcomplete plugin or other userscript plugins
  2991. // document.body for Firefox >= 60
  2992. mtoMutation_body.bindElement(document.querySelector('body'), {
  2993. childList: true,
  2994. subtree: false,
  2995. attributes: false
  2996. })
  2997.  
  2998.  
  2999. no_fix_contents_until = 0;
  3000. no_fix_playlist_until = 0;
  3001. scriptEnable = true;
  3002. regularCheck(1, 0, null);
  3003.  
  3004.  
  3005.  
  3006.  
  3007. }
  3008.  
  3009.  
  3010. function fetchCommentsFinished() {
  3011. let ytdFlexyElm = kRef(ytdFlexy);
  3012. if (!scriptEnable || !ytdFlexyElm) return;
  3013. if(mtf_forceCheckLiveVideo_disable===2) return;
  3014. akAttr(ytdFlexyElm, 'tabview-youtube-comments', false, 'LS')
  3015. }
  3016.  
  3017. function setCommentSection( /** @type {number} */ value) {
  3018.  
  3019. Q.comments_section_loaded = value;
  3020.  
  3021. if (value === 0) {
  3022. sect_lastUpdate = 0;
  3023. comments_section_loaded_elm = null;
  3024. }
  3025.  
  3026. }
  3027.  
  3028. function resetCommentSection(){
  3029.  
  3030. let tab_btn = document.querySelector('.tab-btn[userscript-tab-content="#tab-comments"]')
  3031.  
  3032.  
  3033. if(tab_btn){
  3034.  
  3035. let span = querySelectorFromAnchor.call(tab_btn,'span#tab3-txt-loader');
  3036.  
  3037. tab_btn.removeAttribute('loaded-comment')
  3038. tab_btn.classList.remove('tab-btn-hidden')
  3039. if(span){
  3040. span.textContent='';
  3041. }
  3042. }
  3043.  
  3044.  
  3045. setCommentSection(0);
  3046.  
  3047.  
  3048. }
  3049.  
  3050. function _disableComments() {
  3051. //requestingComments = null;
  3052. mtc_cid && timeline.clearInterval(mtc_cid)
  3053. mtc_cid=0;
  3054.  
  3055. if (!scriptEnable) return;
  3056. let cssElm = kRef(ytdFlexy);
  3057. if (!cssElm) return;
  3058.  
  3059. mtoInterval = mtoInterval2;
  3060.  
  3061. if(Q.comments_section_loaded>0) return; //already displayed / disabled
  3062.  
  3063. scheduledCommentRefresh=false;
  3064. setCommentSection(2);
  3065.  
  3066. comments_section_loaded_elm = mWeakRef(document.querySelector('ytd-comments#comments div#contents') || null)
  3067.  
  3068. let tabBtn = document.querySelector('.tab-btn[userscript-tab-content="#tab-comments"]');
  3069. if(tabBtn) {
  3070. tabBtn.removeAttribute('loaded-comment')
  3071. if (!tabBtn.classList.contains('tab-btn-hidden')) {
  3072. hideTabBtn(tabBtn)
  3073. }
  3074. }
  3075.  
  3076. akAttr(cssElm, 'tabview-youtube-comments', true, 'D');
  3077.  
  3078.  
  3079. }
  3080.  
  3081.  
  3082.  
  3083.  
  3084. let layoutStatusMutex = new Mutex();
  3085.  
  3086. function forceDisplayChatReplay() {
  3087. let items = chatFrameElement('yt-live-chat-item-list-renderer #items');
  3088. if (items && items.childElementCount !== 0) return;
  3089.  
  3090. let ytd_player = document.querySelector('ytd-player#ytd-player');
  3091. if (!ytd_player) return;
  3092. let videoElm = querySelectorFromAnchor.call(ytd_player,'video');
  3093. if (!videoElm) return;
  3094.  
  3095. let video = videoElm;
  3096. if (videoElm && video.currentTime > 0 && !video.ended && video.readyState > video.HAVE_CURRENT_DATA) {
  3097. let chat = document.querySelector('ytd-live-chat-frame#chat');
  3098. if (chat) {
  3099. nativeFunc(chat, "postToContentWindow", [{ "yt-player-video-progress": videoElm.currentTime }])
  3100. }
  3101. }
  3102.  
  3103. }
  3104.  
  3105.  
  3106.  
  3107. function runAfterExpandChat() {
  3108.  
  3109.  
  3110. if(runAfterExpandChat.cid_chatFrameCheck1) timeline.clearInterval(runAfterExpandChat.cid_chatFrameCheck1);
  3111. if(runAfterExpandChat.cid_chatFrameCheck2) timeline.clearInterval(runAfterExpandChat.cid_chatFrameCheck2);
  3112. runAfterExpandChat.cid_chatFrameCheck1=0;
  3113. runAfterExpandChat.cid_chatFrameCheck2=0;
  3114.  
  3115. new Promise(resolve => {
  3116.  
  3117. let chatFrame_st = Date.now();
  3118. runAfterExpandChat.cid_chatFrameCheck1 = 0;
  3119.  
  3120. let sEF = new ScriptEF();
  3121. runAfterExpandChat.cid_chatFrameCheck1 = timeline.setInterval(() => {
  3122. if (!sEF.isValid()) return runAfterExpandChat.cid_chatFrameCheck1 = timeline.clearInterval(runAfterExpandChat.cid_chatFrameCheck1);
  3123. let cDoc = chatFrameContentDocument();
  3124. if (cDoc) {
  3125. runAfterExpandChat.cid_chatFrameCheck1 = timeline.clearInterval(runAfterExpandChat.cid_chatFrameCheck1);
  3126. resolve();
  3127. } else if (!scriptEnable || !isChatExpand() || Date.now() - chatFrame_st > 6750) {
  3128. runAfterExpandChat.cid_chatFrameCheck1 = timeline.clearInterval(runAfterExpandChat.cid_chatFrameCheck1);
  3129. }
  3130. }, 60);
  3131.  
  3132.  
  3133. }).then(() => new Promise(resolve => {
  3134.  
  3135.  
  3136. let chatFrame_st = Date.now();
  3137. runAfterExpandChat.cid_chatFrameCheck2 = 0;
  3138.  
  3139. let sEF = new ScriptEF();
  3140. runAfterExpandChat.cid_chatFrameCheck2 = timeline.setInterval(() => {
  3141. if (!sEF.isValid()) return runAfterExpandChat.cid_chatFrameCheck2 = timeline.clearInterval(runAfterExpandChat.cid_chatFrameCheck2);
  3142. let app = chatFrameElement('yt-live-chat-app');
  3143. if (app) {
  3144. runAfterExpandChat.cid_chatFrameCheck2 = timeline.clearInterval(runAfterExpandChat.cid_chatFrameCheck2);
  3145. resolve(app);
  3146. } else if (!scriptEnable || !isChatExpand() || Date.now() - chatFrame_st > 6750*3) {
  3147. runAfterExpandChat.cid_chatFrameCheck2 = timeline.clearInterval(runAfterExpandChat.cid_chatFrameCheck2);
  3148. }
  3149. }, 60);
  3150.  
  3151.  
  3152. })).then(app => {
  3153.  
  3154.  
  3155. let cDoc = app.ownerDocument;
  3156.  
  3157. if (!scriptEnable || !isChatExpand()) return;
  3158. if(cDoc.querySelector('#userscript-tabview-chatroom-css')) return;
  3159. addStyle(`
  3160. body #input-panel.yt-live-chat-renderer::after {
  3161. background: transparent;
  3162. }
  3163. #items.yt-live-chat-item-list-renderer{
  3164. contain: content;
  3165. }
  3166. yt-live-chat-text-message-renderer{
  3167. contain: content;
  3168. }
  3169. #item-offset.yt-live-chat-item-list-renderer{
  3170. contain: content;
  3171. }
  3172. #item-scroller.yt-live-chat-item-list-renderer{
  3173. contain: strict;
  3174. }
  3175. img[width][height]{
  3176. contain: strict;
  3177. }
  3178. #item-list > yt-live-chat-item-list-renderer, #item-list > yt-live-chat-item-list-renderer > #contents{
  3179. contain: strict;
  3180. }
  3181.  
  3182.  
  3183. #chat.style-scope.yt-live-chat-renderer,
  3184. yt-live-chat-text-message-renderer.style-scope.yt-live-chat-item-list-renderer,
  3185. #item-scroller,
  3186. yt-live-chat-text-message-renderer.style-scope.yt-live-chat-item-list-renderer .style-scope.yt-live-chat-text-message-renderer,
  3187. yt-live-chat-ticker-paid-message-item-renderer
  3188. {
  3189. contain: layout paint style;
  3190. }
  3191. yt-img-shadow#author-photo.style-scope{
  3192. contain: layout paint style;
  3193. content-visibility: auto;
  3194. contain-intrinsic-size: 24px 24px;
  3195. }
  3196. #item-offset.style-scope.yt-live-chat-item-list-renderer,
  3197. #items.style-scope.yt-live-chat-item-list-renderer {
  3198. contain: layout paint;
  3199. }
  3200. .style-scope.yt-live-chat-text-message-renderer {
  3201. cursor: default;
  3202. }
  3203. #author-photo.style-scope.yt-live-chat-text-message-renderer,
  3204. yt-live-chat-author-chip.style-scope.yt-live-chat-text-message-renderer,
  3205. yt-live-chat-author-chip.style-scope.yt-live-chat-text-message-renderer ~ span#message.style-scope.yt-live-chat-text-message-renderer
  3206. {
  3207. pointer-events: none;
  3208. }
  3209. span#message.style-scope.yt-live-chat-text-message-renderer > img.emoji.yt-formatted-string.style-scope.yt-live-chat-text-message-renderer{
  3210. contain: layout paint style;
  3211. cursor: default;
  3212. pointer-events: none;
  3213. }
  3214. body yt-live-chat-app{
  3215. contain: size layout paint style;
  3216. content-visibility: auto;
  3217. transform: translate3d(0,0,0);
  3218. overflow: hidden;
  3219. }
  3220.  
  3221.  
  3222. /*
  3223. yt-live-chat-text-message-renderer.style-scope.yt-live-chat-item-list-renderer:nth-last-of-type(n+20){
  3224. content-visibility: auto;
  3225. }
  3226. */
  3227.  
  3228. /*
  3229. @supports (contain-intrinsic-size: auto 1px){
  3230.  
  3231. yt-live-chat-text-message-renderer.style-scope.yt-live-chat-item-list-renderer{
  3232. content-visibility: auto;
  3233. contain-intrinsic-size: auto 24px;
  3234. }
  3235.  
  3236. }
  3237. */
  3238. .style-scope.yt-live-chat-item-list-renderer{
  3239. box-sizing: border-box;
  3240. }
  3241.  
  3242. /*
  3243. .style-scope.yt-live-chat-item-list-renderer[style*="--tabview-crm-height"]{
  3244. content-visibility: auto;
  3245. contain-intrinsic-size: 1px var(--tabview-crm-height, 24px);
  3246. }
  3247.  
  3248. #item-offset.style-scope.yt-live-chat-item-list-renderer{
  3249. height: auto !important;
  3250. }
  3251. #item-offset.style-scope.yt-live-chat-item-list-renderer > #items:only-child{
  3252. position: relative !important;
  3253. }*/
  3254.  
  3255. #item-offset.style-scope.yt-live-chat-item-list-renderer{
  3256. contain: size layout paint style;
  3257. }
  3258. #item-offset.style-scope.yt-live-chat-item-list-renderer > #items:only-child{
  3259. contain: layout paint style;
  3260. }
  3261.  
  3262. `, cDoc.documentElement).id='userscript-tabview-chatroom-css'
  3263.  
  3264. let frc= 0;
  3265. let fullReady = ()=>{
  3266.  
  3267.  
  3268. if(!cDoc.documentElement.hasAttribute('style') && ++frc<900) return timeline.setTimeout(fullReady,10);
  3269.  
  3270. if (cDoc.querySelector('yt-live-chat-renderer #continuations')) {
  3271. timeline.setTimeout(() => mtf_ChatExist(), 40);
  3272. $(document.querySelector('ytd-live-chat-frame#chat')).attr('yt-userscript-iframe-loaded', '')
  3273. }
  3274.  
  3275. forceDisplayChatReplay();
  3276.  
  3277. let iframe = document.querySelector('ytd-live-chat-frame iframe#chatframe');
  3278.  
  3279.  
  3280. iframe.dispatchEvent(new CustomEvent("tabview-chatroom-ready"))
  3281.  
  3282.  
  3283.  
  3284. }
  3285. fullReady();
  3286.  
  3287.  
  3288.  
  3289. })
  3290.  
  3291. }
  3292.  
  3293. function flexyAttr_toggleFlag(mFlag, b, flag) {
  3294. if (b) mFlag = mFlag | flag;
  3295. else mFlag = mFlag & ~flag;
  3296. return mFlag;
  3297. }
  3298.  
  3299. function flexAttr_toLayoutStatus(nls, attributeName) {
  3300.  
  3301. let attrElm, b, v;
  3302. switch (attributeName) {
  3303. case 'theater':
  3304. nls = flexyAttr_toggleFlag(nls, isTheater(), LAYOUT_THEATER);
  3305. break;
  3306. case 'userscript-chat-collapsed':
  3307. case 'userscript-chatblock':
  3308. attrElm = kRef(ytdFlexy);
  3309. if (hasAttribute(attrElm, 'userscript-chat-collapsed')) {
  3310. nls = flexyAttr_toggleFlag(nls, true, LAYOUT_CHATROOM | LAYOUT_CHATROOM_COLLASPED);
  3311. } else {
  3312. nls = flexyAttr_toggleFlag(nls, hasAttribute(attrElm, 'userscript-chatblock'), LAYOUT_CHATROOM);
  3313. nls = flexyAttr_toggleFlag(nls, false, LAYOUT_CHATROOM_COLLASPED);
  3314. }
  3315. break;
  3316. case 'is-two-columns_':
  3317. nls = flexyAttr_toggleFlag(nls, isWideScreenWithTwoColumns(), LAYOUT_TWO_COLUMNS);
  3318. break;
  3319.  
  3320. case 'tabview-selection':
  3321. b = isNonEmptyString(kRef(ytdFlexy).getAttribute('tabview-selection'));
  3322. nls = flexyAttr_toggleFlag(nls, b, LAYOUT_TAB_EXPANDED);
  3323. break;
  3324.  
  3325. case 'fullscreen':
  3326. b = isNonEmptyString(kRef(ytdFlexy).getAttribute('fullscreen'));
  3327. nls = flexyAttr_toggleFlag(nls, b, LAYOUT_FULLSCREEN);
  3328. break;
  3329.  
  3330. case 'userscript-engagement-panel':
  3331. v = kRef(ytdFlexy).getAttribute('userscript-engagement-panel');
  3332. b = (+v > 0)
  3333. nls = flexyAttr_toggleFlag(nls, b, LAYOUT_ENGAGEMENT_PANEL_EXPAND);
  3334. break;
  3335.  
  3336. }
  3337.  
  3338. return nls;
  3339.  
  3340.  
  3341. }
  3342.  
  3343. let mtf_attrFlexy = (mutations, observer) => {
  3344.  
  3345. //attr mutation checker - $$ytdFlexyElm$$ {ytd-watch-flexy} \single
  3346. //::attr
  3347. // ~ 'userscript-chat-collapsed', 'userscript-chatblock', 'theater', 'is-two-columns_',
  3348. // ~ 'tabview-selection', 'fullscreen', 'userscript-engagement-panel',
  3349. // ~ 'hidden'
  3350.  
  3351. //console.log(15330, scriptEnable, kRef(ytdFlexy), mutations)
  3352.  
  3353. if (!scriptEnable) return;
  3354.  
  3355. const cssElm = kRef(ytdFlexy)
  3356. if (!cssElm) return;
  3357.  
  3358. if (!mutations) return;
  3359.  
  3360. //console.log(15340)
  3361. ytdFlexyHiddenCheck(mutations);
  3362.  
  3363. const old_layoutStatus = wls.layoutStatus
  3364. if (old_layoutStatus === null) return;
  3365. let new_layoutStatus = old_layoutStatus;
  3366.  
  3367. let checkedChat = false;
  3368.  
  3369. for (const mutation of mutations) {
  3370. new_layoutStatus = flexAttr_toLayoutStatus(new_layoutStatus, mutation.attributeName);
  3371. if (mutation.attributeName == 'userscript-chat-collapsed' || mutation.attributeName == 'userscript-chatblock') {
  3372.  
  3373. if(!checkedChat){
  3374. checkedChat = true; // avoid double call
  3375.  
  3376. if (cssElm.getAttribute('userscript-chatblock') === 'chat-live') {
  3377. // assigned new attribute - "chat-live" => disable comments section
  3378. _disableComments();
  3379. }
  3380.  
  3381. if (!cssElm.hasAttribute('userscript-chatblock')) {
  3382. // might or might not collapsed before
  3383. timeline.setTimeout(() => {
  3384. if (!scriptEnable) return;
  3385. //delayed call => check with the "no active focus" condition with chatroom status
  3386. if (!isAnyActiveTab() && !isChatExpand() && !isTheater() && isWideScreenWithTwoColumns() && !isFullScreen()) {
  3387. setToActiveTab();
  3388. }
  3389. }, 240);
  3390. }
  3391. }
  3392.  
  3393. }else if(mutation.attributeName == 'userscript-engagement-panel'){
  3394. // assume any other active component such as tab content and chatroom
  3395. if (+(cssElm.getAttribute('userscript-engagement-panel')||0)===0 && +mutation.oldValue>0) {
  3396. timeline.setTimeout(() => {
  3397. if (!scriptEnable) return;
  3398. //delayed call => check with the "no active focus" condition with engagement panel status
  3399. if (!isAnyActiveTab() && !isEngagementPanelExpanded() && !isTheater() && isWideScreenWithTwoColumns() && !isFullScreen() && !isChatExpand()) {
  3400. setToActiveTab();
  3401. }
  3402. }, 240);
  3403. }
  3404. }
  3405. }
  3406.  
  3407. if (new_layoutStatus !== old_layoutStatus) wls.layoutStatus = new_layoutStatus
  3408.  
  3409. }
  3410.  
  3411. const mtf_checkFlexy = () => {
  3412. // once per $$native-ytd-watch-flexy$$ {ytd-watch-flexy} detection
  3413.  
  3414. let ytdFlexyElm = kRef(ytdFlexy);
  3415. if (!scriptEnable || !ytdFlexyElm) return true;
  3416.  
  3417.  
  3418. wls.layoutStatus = null;
  3419.  
  3420. let isFlexyHidden = (deferredVarYTDHidden = ytdFlexyElm.hasAttribute('hidden'));
  3421.  
  3422. if (!isFlexyHidden) {
  3423. let rChatExist = base_ChatExist();
  3424. if (rChatExist) {
  3425. let { attr_chatblock, attr_chatcollapsed } = rChatExist;
  3426. if (attr_chatblock === null) {
  3427. //remove attribute if it is unknown
  3428. attr_chatblock = false;
  3429. attr_chatcollapsed = false;
  3430. }
  3431. wAttr(ytdFlexyElm, 'userscript-chatblock', attr_chatblock)
  3432. wAttr(ytdFlexyElm, 'userscript-chat-collapsed', attr_chatcollapsed)
  3433. }
  3434. }
  3435.  
  3436. let rTabSelection = [...querySelectorAllFromAnchor.call(ytdFlexyElm,'.tab-btn[userscript-tab-content]')]
  3437. .map(elm => ({ elm, hidden: elm.classList.contains('tab-btn-hidden') }));
  3438.  
  3439. if(rTabSelection.length === 0){
  3440. wAttr(ytdFlexyElm, 'tabview-selection', false);
  3441. }else{
  3442. rTabSelection=rTabSelection.filter(entry => entry.hidden !== true); // all available tabs
  3443. if (rTabSelection.length === 0) wAttr(ytdFlexyElm, 'tabview-selection', '');
  3444. }
  3445. rTabSelection = null;
  3446. let rEP = engagement_panels_();
  3447. if (rEP && rEP.count > 0) wAttr(ytdFlexyElm, 'userscript-engagement-panel', false);
  3448. //else wAttr(ytdFlexyElm, 'userscript-engagement-panel', rEP.value + ""); // can be 0
  3449. else if(rEP.value>0) wAttr(ytdFlexyElm, 'userscript-engagement-panel', rEP.value + ""); // can be 0
  3450.  
  3451. let ls = 0;
  3452. ls = flexAttr_toLayoutStatus(ls, 'theater')
  3453. ls = flexAttr_toLayoutStatus(ls, 'userscript-chat-collapsed')
  3454. ls = flexAttr_toLayoutStatus(ls, 'userscript-chatblock')
  3455. ls = flexAttr_toLayoutStatus(ls, 'is-two-columns_')
  3456. ls = flexAttr_toLayoutStatus(ls, 'tabview-selection')
  3457. ls = flexAttr_toLayoutStatus(ls, 'fullscreen')
  3458. ls = flexAttr_toLayoutStatus(ls, 'userscript-engagement-panel')
  3459.  
  3460. wls.layoutStatus = ls
  3461.  
  3462. mtoFlexyAttr.bindElement(ytdFlexyElm,{
  3463. attributes: true,
  3464. attributeFilter: ['userscript-chat-collapsed', 'userscript-chatblock', 'theater', 'is-two-columns_', 'tabview-selection', 'fullscreen', 'userscript-engagement-panel', 'hidden'],
  3465. attributeOldValue: true
  3466. })
  3467.  
  3468. ytdFlexyHiddenCheckBasic(ytdFlexyElm);
  3469.  
  3470.  
  3471. let columns = document.querySelector('ytd-page-manager#page-manager #columns')
  3472. if (columns) {
  3473. wAttr(columns, 'userscript-scrollbar-render', true);
  3474. }
  3475.  
  3476. return false;
  3477. }
  3478.  
  3479. function ytdFlexyHiddenCheckBasic(ytdFlexyElm){
  3480. // for both hidden and non-hidden ytd-watch-flexy
  3481.  
  3482. deferredVarYTDHidden = ytdFlexyElm.hasAttribute('hidden');
  3483.  
  3484. // console.log(15403)
  3485. document.documentElement.classList.toggle('tabview-normal-player',!deferredVarYTDHidden)
  3486.  
  3487. }
  3488.  
  3489. function checkVisibleEngagementPanel(){
  3490. if(storeLastPanel){
  3491.  
  3492. let elm_storeLastPanel = kRef(storeLastPanel);
  3493.  
  3494. if(elm_storeLastPanel && !isDOMVisible(elm_storeLastPanel) ){
  3495. storeLastPanel=null;
  3496. ytBtnCloseEngagementPanels();
  3497. }
  3498.  
  3499. }
  3500.  
  3501. }
  3502.  
  3503. function ytdFlexyHiddenCheck(mutations) {
  3504.  
  3505. //console.log(15350)
  3506.  
  3507. const ytdFlexyElm = kRef(ytdFlexy)
  3508. if (!ytdFlexyElm) return;
  3509. let muHidden = false
  3510.  
  3511. for (const mutation of mutations) {
  3512. if (mutation.attributeName === 'hidden') {
  3513. //console.log(343)
  3514. muHidden = true;
  3515. break;
  3516. }
  3517. }
  3518.  
  3519. if (!muHidden) return;
  3520.  
  3521. mtc_store= Date.now()+2870
  3522. //console.log(7004)
  3523. deferredVarYTDHidden = ytdFlexyElm.hasAttribute('hidden');
  3524.  
  3525. if(deferredVarYTDHidden && scheduledCommentRefresh && Q.comments_section_loaded>0){
  3526. scheduledCommentRefresh=false;
  3527. }
  3528.  
  3529. //console.log(15400)
  3530. if(!deferredVarYTDHidden){
  3531. mtf_forceCheckLiveVideo_disable = 0
  3532. timeline.setTimeout(function() {
  3533. mtf_ChatExist();
  3534. layoutStatusMutex.lockWith(unlock=>{
  3535. atChange(); // in case no mutation occurance
  3536. ytdFlexyHiddenCheckBasic(ytdFlexyElm);
  3537. dispatchWindowResize(); // player control positioning
  3538. timeline.setTimeout(unlock, 40);
  3539. })
  3540. }, 40);
  3541. }else{
  3542. //console.log(15401)
  3543. timeline.setTimeout(function() {
  3544. //console.log(15402)
  3545. ytdFlexyHiddenCheckBasic(ytdFlexyElm);
  3546. }, 40);
  3547. }
  3548.  
  3549. }
  3550.  
  3551.  
  3552.  
  3553.  
  3554. let switchTabActivity_lastTab = null
  3555.  
  3556. function setDisplayedPlaylist() {
  3557. //override the default youtube coding event prevention
  3558. let cssElm = kRef(ytdFlexy);
  3559. if (!scriptEnable || !cssElm) return;
  3560. displayedPlaylist = mWeakRef(document.querySelector('ytd-watch-flexy #tab-list:not(.tab-content-hidden) ytd-playlist-panel-renderer') || null);
  3561. }
  3562.  
  3563.  
  3564. let recordScrollTop = 0
  3565.  
  3566. function switchTabActivity(activeLink) {
  3567. if (!scriptEnable) return;
  3568.  
  3569. const ytdFlexyElm = kRef(ytdFlexy);
  3570.  
  3571. if (!ytdFlexyElm) return;
  3572.  
  3573. if (activeLink && activeLink.classList.contains('tab-btn-hidden')) return; // not allow to switch to hide tab
  3574.  
  3575. if (isTheater() && isWideScreenWithTwoColumns()) activeLink = null;
  3576.  
  3577. /*
  3578. let isPrevTabComments = document.querySelector('.tab-content-cld:not(.tab-content-hidden)');
  3579. isPrevTabComments=isPrevTabComments&&isPrevTabComments.matches('#tab-comments')
  3580.  
  3581. if(isPrevTabComments){
  3582. let comments_tab = document.querySelector('#tab-comments');
  3583. if(comments_tab){
  3584. let st = comments_tab.scrollTop;
  3585. if(st>=0) recordScrollTop = st;
  3586. }
  3587.  
  3588.  
  3589. }
  3590. */
  3591.  
  3592. function runAtEnd() {
  3593.  
  3594. //console.log(12312)
  3595.  
  3596. if (activeLink) lastShowTab = activeLink.getAttribute('userscript-tab-content')
  3597.  
  3598. displayedPlaylist = null;
  3599. scrollingVideosList = null;
  3600.  
  3601. if (activeLink && lastShowTab == '#tab-list') {
  3602. setDisplayedPlaylist();
  3603. } else if (activeLink && lastShowTab == '#tab-videos') {
  3604. scrollingVideosList = mWeakRef(document.querySelector('ytd-watch-flexy #tab-videos:not(.tab-content-hidden) [placeholder-videos]'));
  3605. }
  3606.  
  3607.  
  3608. ytdFlexyElm.setAttribute('tabview-selection', activeLink ? lastShowTab : '')
  3609.  
  3610. if (lastShowTab == '#tab-comments' && (ytdFlexyElm.getAttribute('tabview-youtube-comments') || '').lastIndexOf('S') >= 0) {
  3611.  
  3612. if(mtf_forceCheckLiveVideo_disable===2) {}
  3613. else{
  3614. akAttr(ytdFlexyElm, 'tabview-youtube-comments', false, 'L');
  3615.  
  3616. /*
  3617. requestAnimationFrame(() => {
  3618. let comments_tab = document.querySelector('#tab-comments');
  3619. if (comments_tab && comments_tab.scrollTop > 0) comments_tab.scrollTop = 0;
  3620. });
  3621. */
  3622. }
  3623.  
  3624. }
  3625.  
  3626. /*
  3627. let isNewTabComments = document.querySelector('.tab-content-cld:not(.tab-content-hidden)')
  3628. isNewTabComments=isNewTabComments&&isNewTabComments.matches('#tab-comments')
  3629.  
  3630. if(isNewTabComments){
  3631. let comments_tab = document.querySelector('#tab-comments');
  3632. if(comments_tab){
  3633. let st = comments_tab.scrollTop;
  3634. if(Math.abs(st-recordScrollTop)>0.9) comments_tab.scrollTop=recordScrollTop;
  3635. }
  3636.  
  3637. }
  3638. */
  3639.  
  3640.  
  3641. }
  3642.  
  3643. const links = document.querySelectorAll('#material-tabs a[userscript-tab-content]');
  3644.  
  3645.  
  3646.  
  3647. for (const link of links) {
  3648. let content = document.querySelector(link.getAttribute('userscript-tab-content'));
  3649. if (link && content) {
  3650. if (link !== activeLink) {
  3651. link.classList.remove("active");
  3652. content.classList.add("tab-content-hidden");
  3653. } else {
  3654. link.classList.add("active");
  3655. content.classList.remove("tab-content-hidden");
  3656. //timeline.setTimeout(()=>content.focus(),400);
  3657.  
  3658. }
  3659. }
  3660. }
  3661.  
  3662. runAtEnd();
  3663.  
  3664.  
  3665. }
  3666.  
  3667.  
  3668. const STORE_VERSION = 1;
  3669. const STORE_key = 'userscript-tabview-settings';
  3670. function getStore(){
  3671. let s = localStorage[STORE_key];
  3672. function resetStore(){
  3673. let ret = {
  3674. version: 1,
  3675. };
  3676. localStorage[STORE_key]=JSON.stringify(ret);
  3677. return ret;
  3678. }
  3679. if(!s) return resetStore();
  3680. let obj = null;
  3681. try{
  3682. obj = JSON.parse(s);
  3683. }catch(e){}
  3684. return obj && obj.version === STORE_VERSION ? obj : resetStore();
  3685. }
  3686.  
  3687. function setStore(obj){
  3688. if(!obj || typeof obj !=='object') return false;
  3689. if(obj.version !== STORE_VERSION) return false;
  3690. localStorage[STORE_key]=JSON.stringify(obj);
  3691. return true;
  3692. }
  3693.  
  3694. let tabsUiScript_setclick = false;
  3695.  
  3696. function prepareTabBtn() {
  3697.  
  3698. const materialTab = document.querySelector("#material-tabs")
  3699. if (!materialTab) return;
  3700.  
  3701. let noActiveTab = !!document.querySelector('ytd-watch-flexy[userscript-chatblock]:not([userscript-chat-collapsed])')
  3702.  
  3703. const activeLink = querySelectorFromAnchor.call(materialTab,'a[userscript-tab-content].active:not(.tab-btn-hidden)')
  3704. if (activeLink) switchTabActivity(noActiveTab ? null : activeLink)
  3705.  
  3706. if (!tabsUiScript_setclick) {
  3707. tabsUiScript_setclick = true;
  3708. $(materialTab).on("click", "a", function(evt) {
  3709.  
  3710. //console.log(8510)
  3711. let ytdFlexyElm = kRef(ytdFlexy);
  3712. if (!scriptEnable || !ytdFlexyElm) return null;
  3713.  
  3714. if (!this.hasAttribute('userscript-tab-content')) return;
  3715.  
  3716. if (evt.target.matches('.font-size-btn')) return;
  3717.  
  3718.  
  3719. evt.preventDefault();
  3720.  
  3721. //console.log(8511)
  3722.  
  3723. /*
  3724. if (this.getAttribute('userscript-tab-content') == '#tab-comments' && parseInt(ytdFlexyElm.getAttribute('tabview-youtube-comments') || '') < 0) {
  3725. console.log(8512)
  3726. return;
  3727. }*/
  3728.  
  3729. //console.log(8513)
  3730. new Promise(requestAnimationFrame).then(() => {
  3731.  
  3732.  
  3733. //console.log(8514)
  3734. layoutStatusMutex.lockWith(unlock => {
  3735.  
  3736. //console.log(8515)
  3737. switchTabActivity_lastTab = this.getAttribute('userscript-tab-content');
  3738.  
  3739. let isActiveAndVisible = this.classList.contains('tab-btn') && this.classList.contains('active') && !this.classList.contains('tab-btn-hidden')
  3740.  
  3741. if (isWideScreenWithTwoColumns() && !isTheater() && isActiveAndVisible) {
  3742. //optional
  3743. timeline.setTimeout(unlock, 80);
  3744. switchTabActivity(null);
  3745. ytBtnSetTheater();
  3746. } else if (isActiveAndVisible) {
  3747. timeline.setTimeout(unlock, 80);
  3748. switchTabActivity(null);
  3749. } else {
  3750. let pInterval = 60;
  3751. if (isChatExpand() && isWideScreenWithTwoColumns()) {
  3752. ytBtnCollapseChat();
  3753. } else if (isEngagementPanelExpanded() && isWideScreenWithTwoColumns()) {
  3754. ytBtnCloseEngagementPanels();
  3755. } else if (isWideScreenWithTwoColumns() && isTheater() && !isFullScreen()) {
  3756. ytBtnCancelTheater();
  3757. } else {
  3758. pInterval = 20;
  3759. }
  3760. //console.log(8518)
  3761. timeline.setTimeout(() => {
  3762. timeline.setTimeout(makeBodyScroll, 20); // this is to make the image render
  3763.  
  3764. timeline.setTimeout(() => {
  3765. let rightTabs = document.querySelector('#right-tabs');
  3766. if (!isWideScreenWithTwoColumns() && rightTabs && rightTabs.offsetTop > 0 && this.classList.contains('active')) {
  3767. let tabButtonBar = document.querySelector('#material-tabs');
  3768. let tabButtonBarHeight = tabButtonBar ? tabButtonBar.offsetHeight : 0;
  3769. window.scrollTo(0, rightTabs.offsetTop - tabButtonBarHeight);
  3770. }
  3771. }, 60)
  3772. // console.log(8519)
  3773.  
  3774. timeline.setTimeout(unlock, 80)
  3775. switchTabActivity(this)
  3776.  
  3777. }, pInterval);
  3778. }
  3779.  
  3780.  
  3781. })
  3782.  
  3783. })
  3784.  
  3785.  
  3786.  
  3787.  
  3788. });
  3789. /*
  3790.  
  3791. $(materialTab).on("mousedown", ".font-size-btn", function(evt){
  3792.  
  3793. evt.stopPropagation();
  3794. evt.stopImmediatePropagation();
  3795.  
  3796. });
  3797.  
  3798. $(materialTab).on("pointerdown", ".font-size-btn", function(evt){
  3799.  
  3800. evt.stopPropagation();
  3801. evt.stopImmediatePropagation();
  3802.  
  3803. });
  3804. $(materialTab).on("mouseup", ".font-size-btn", function(evt){
  3805.  
  3806. evt.stopPropagation();
  3807. evt.stopImmediatePropagation();
  3808.  
  3809. });
  3810.  
  3811. $(materialTab).on("pointerup", ".font-size-btn", function(evt){
  3812.  
  3813. evt.stopPropagation();
  3814. evt.stopImmediatePropagation();
  3815.  
  3816. });
  3817. */
  3818.  
  3819. function updateCSS_fontsize(){
  3820.  
  3821. let store = getStore();
  3822. let ytdFlexyElm = kRef(ytdFlexy);
  3823. if(ytdFlexyElm){
  3824. if(store['font-size-#tab-info'])
  3825. ytdFlexyElm.style.setProperty('--ut2257-info', store['font-size-#tab-info'])
  3826. if(store['font-size-#tab-comments'])
  3827. ytdFlexyElm.style.setProperty('--ut2257-comments', store['font-size-#tab-comments'])
  3828. if(store['font-size-#tab-videos'])
  3829. ytdFlexyElm.style.setProperty('--ut2257-videos', store['font-size-#tab-videos'])
  3830. if(store['font-size-#tab-list'])
  3831. ytdFlexyElm.style.setProperty('--ut2257-list', store['font-size-#tab-list'])
  3832. }
  3833.  
  3834. }
  3835.  
  3836. $(materialTab).on("click", ".font-size-btn", function(evt){
  3837.  
  3838. evt.preventDefault();
  3839. evt.stopPropagation();
  3840. evt.stopImmediatePropagation();
  3841.  
  3842. let value = evt.target.matches('.font-size-plus')?1: evt.target.matches('.font-size-minus')?-1 :0;
  3843.  
  3844. let active_tab_content = closestDOM.call(evt.target,'[userscript-tab-content]').getAttribute('userscript-tab-content');
  3845.  
  3846. let store = getStore();
  3847. let settingKey = `font-size-${active_tab_content}`
  3848. if(!store[settingKey]) store[settingKey] = 1.0;
  3849. if(value<0) store[settingKey] -= 0.05;
  3850. else if(value>0) store[settingKey] += 0.05;
  3851. if(store[settingKey]<0.1) store[settingKey] = 0.1;
  3852. else if(store[settingKey]>10) store[settingKey] = 10.0;
  3853. setStore(store);
  3854.  
  3855.  
  3856. updateCSS_fontsize();
  3857.  
  3858.  
  3859. //console.log(this.textContent)
  3860.  
  3861.  
  3862. });
  3863.  
  3864. updateCSS_fontsize();
  3865.  
  3866.  
  3867.  
  3868. }
  3869.  
  3870. }
  3871.  
  3872.  
  3873. // ---------------------------------------------------------------------------------------------
  3874. document.addEventListener("yt-navigate-finish", onNavigationEnd)
  3875. //yt-navigate-redirect
  3876. //"yt-page-data-fetched"
  3877. //yt-navigate-error
  3878. //yt-navigate-start
  3879. //yt-page-manager-navigate-start
  3880. //"yt-navigate"
  3881. //"yt-navigate-cache
  3882.  
  3883. document.addEventListener("yt-navigate-cache",()=>{
  3884. console.log('yt-navigate-cache')
  3885. })
  3886. document.addEventListener("yt-navigate-redirect",()=>{
  3887. console.log('yt-navigate-redirect')
  3888. })
  3889.  
  3890. function onReady(){
  3891. //might be earlier than yt-navigation-finish
  3892. console.log('html-onReady')
  3893. if(location.pathname=='/watch') script_inject_js1.inject();
  3894. }
  3895.  
  3896. if(document.readyState!=='loading'){
  3897. onReady();
  3898. }else{
  3899. document.addEventListener('DOMContentLoaded', onReady)
  3900. }
  3901.  
  3902. function forceConfig(){
  3903. let trial = false;
  3904. /*
  3905. let f=()=>{
  3906. if(trial) return;
  3907. let b = true;
  3908. try{
  3909. document.dispatchEvent(new CustomEvent("me-func"))
  3910. window.yt.config_.EXPERIMENT_FLAGS.kevlar_watch_metadata_refresh=true
  3911. }catch(e){
  3912. b=false;
  3913. }
  3914. if(b) trial = true;
  3915. }
  3916. f();
  3917. requestAnimationFrame(f)
  3918. setTimeout(f,30)
  3919. setTimeout(f,300)
  3920. */
  3921. }
  3922.  
  3923. document.addEventListener("yt-navigate-start",()=>{
  3924. console.log('yt-navigate-start') // not always trigger before navigate-end
  3925. no_fix_contents_until = 0;
  3926. no_fix_playlist_until = 0;
  3927.  
  3928. script_inject_js1.inject();
  3929. forceConfig();
  3930. })
  3931.  
  3932. document.addEventListener("yt-page-manager-navigate-start",()=>{
  3933. console.log('yt-page-manager-navigate-start')
  3934. forceConfig();
  3935. })
  3936.  
  3937.  
  3938. document.addEventListener("loadstart", (evt) => {
  3939. if (!evt || !evt.target || evt.target.nodeName !== "VIDEO") return;
  3940. let elm = evt.target;
  3941. if (!elm.matches('#player video, #movie_player video, video[tabview-mainvideo]')) return;
  3942.  
  3943. mtc_store= Date.now()+2870
  3944.  
  3945. let src = elm.src;
  3946. if (src !== lastVideoURL) {
  3947. lastVideoURL = elm.src;
  3948. elm.setAttribute('tabview-mainvideo', ''); // mainly for mini playing
  3949. no_fix_contents_until = 0;
  3950. no_fix_playlist_until = 0;
  3951. regularCheck(1,0,null); // mutation happens when the page is not ready; call again as the page is ready.
  3952.  
  3953. }
  3954.  
  3955. }, true)
  3956.  
  3957. // ---------------------------------------------------------------------------------------------
  3958.  
  3959. let scrolling_lastD = 0;
  3960.  
  3961. const singleColumnScrolling = function(/** @type {boolean} */ scrolling_lastF) {
  3962. if (!scriptEnable || deferredVarYTDHidden) return;
  3963.  
  3964. let pageY = scrollY;
  3965. if (pageY < 10 && scrolling_lastD === 0 && !scrolling_lastF) return;
  3966.  
  3967. let targetElm, header, navElm;
  3968.  
  3969. Promise.resolve().then(() => {
  3970.  
  3971. targetElm = document.querySelector("#right-tabs");
  3972. if (!targetElm) return;
  3973. header = querySelectorFromAnchor.call(targetElm,"header");
  3974. if (!header) return;
  3975. navElm = document.querySelector('#masthead-container, #masthead')
  3976. if (!navElm) return;
  3977. let navHeight = navElm ? navElm.offsetHeight : 0
  3978.  
  3979. let elmY = targetElm.offsetTop
  3980.  
  3981. let xyz = [elmY + navHeight, pageY, elmY - navHeight]
  3982.  
  3983. let xyStatus = 0
  3984. if (xyz[1] < xyz[2] && xyz[2] < xyz[0]) {
  3985. // 1
  3986. xyStatus = 1
  3987. }
  3988.  
  3989. if (xyz[0] > xyz[1] && xyz[1] > xyz[2]) {
  3990.  
  3991. //2
  3992. xyStatus = 2
  3993.  
  3994. }
  3995.  
  3996. if (xyz[2] < xyz[0] && xyz[0] < xyz[1]) {
  3997. // 3
  3998.  
  3999. xyStatus = 3
  4000.  
  4001.  
  4002. }
  4003.  
  4004. return xyStatus;
  4005.  
  4006. }).then((xyStatus) => {
  4007.  
  4008. if ((xyStatus == 2 || xyStatus == 3) && (scrolling_lastD === 0 || scrolling_lastF)) {
  4009. scrolling_lastD = 1;
  4010. let {
  4011. offsetHeight
  4012. } = header
  4013. let {
  4014. offsetWidth
  4015. } = targetElm
  4016.  
  4017. targetElm.style.setProperty("--userscript-sticky-width", offsetWidth + 'px')
  4018. targetElm.style.setProperty("--userscript-sticky", offsetHeight + 'px')
  4019.  
  4020. wAttr(targetElm, 'userscript-sticky', true);
  4021.  
  4022. } else if ((xyStatus == 1) && (scrolling_lastD === 1 || scrolling_lastF)) {
  4023. scrolling_lastD = 0;
  4024.  
  4025. wAttr(targetElm, 'userscript-sticky', false);
  4026. }
  4027.  
  4028.  
  4029. targetElm = null;
  4030. header = null;
  4031. navElm = null;
  4032.  
  4033. });
  4034.  
  4035. };
  4036.  
  4037. window.addEventListener("scroll", function() {
  4038. singleColumnScrolling(false)
  4039. }, {
  4040. capture: false,
  4041. passive: true
  4042. })
  4043.  
  4044. let lastResizeAt = 0;
  4045. window.addEventListener('resize', function() {
  4046.  
  4047. if (!scriptEnable) return;
  4048. if (deferredVarYTDHidden) return;
  4049. lastResizeAt = Date.now();
  4050.  
  4051. requestAnimationFrame(() => {
  4052. singleColumnScrolling(true)
  4053. })
  4054.  
  4055. }, {
  4056. capture: false,
  4057. passive: true
  4058. })
  4059.  
  4060.  
  4061.  
  4062. let hist = null
  4063. const getHistState = (history)=>`${history.length}|${history.state?history.state.entryTime:null}`;
  4064. function newVideoPageCheck(){
  4065. let hState = getHistState(history)
  4066. if(hist!==hState) {
  4067. hist=hState;
  4068. if(scriptEnable){
  4069. //scriptEnable = false;
  4070. let callTimeout = !newVideoPage.entryTime
  4071. newVideoPage.entryTime = 1
  4072. if(callTimeout) setTimeout(newVideoPage,0)
  4073. }
  4074. return true;
  4075. }
  4076. }
  4077. function newVideoPage(){
  4078. newVideoPage.entryTime = 0;
  4079.  
  4080. console.log('newVideoPage')
  4081. //console.log('newVideoPage-', 150, location.href)
  4082.  
  4083. let ytdFlexyElm = kRef(ytdFlexy);
  4084. if(!ytdFlexyElm) return;
  4085.  
  4086. timeline.reset();
  4087. layoutStatusMutex = new Mutex();
  4088.  
  4089. //console.log('newVideoPage-', 350, location.href)
  4090. let flag1= /^https?\:\/\/(\w+\.)*youtube\.com\/watch\?(\w+\=[^\/\?\&]+\&|\w+\=?\&)*v=[\w\-\_]+/.test(location.href)
  4091. let tf = ()=>{
  4092.  
  4093. let isFlexyHidden = (deferredVarYTDHidden = ytdFlexyElm.hasAttribute('hidden'));
  4094.  
  4095. let flag2 = !isFlexyHidden; // flexy is visible
  4096.  
  4097. if(flag1^flag2) return timeline.setTimeout(tf,7); else funcVisibleWatch();
  4098.  
  4099. tf= null;
  4100. funcVisibleWatch = null;
  4101. ytdFlexyElm=null
  4102.  
  4103. }
  4104.  
  4105. let funcVisibleWatch = ()=>{
  4106.  
  4107. //(flexy is visible and watch video page) || (flexy is inivisible and it is not watch video page)
  4108. //scriptEnable = true;
  4109.  
  4110. let isFlexyHidden = (deferredVarYTDHidden = ytdFlexyElm.hasAttribute('hidden'));
  4111. ytdFlexyHiddenCheckBasic(ytdFlexyElm)
  4112.  
  4113. if(!isFlexyHidden) {
  4114. //(flexy is visible and watch video page)
  4115.  
  4116. mtf_forceCheckLiveVideo_disable = 0;
  4117. mtc_store= Date.now()+2870
  4118.  
  4119. sect_lastUpdate = 0;
  4120. if(pendingOne){
  4121. // reset pendingOne = false if the element is already removed.
  4122. pendingOne = document.querySelectorAll(`ytd-watch-flexy ytd-comments#comments [tabview-cache-time]`).length>1;
  4123. }
  4124.  
  4125. /*
  4126. // try to omit in 2.5.x
  4127. timeline.setTimeout(()=>{
  4128. Q.mutationTarget=null;
  4129. console.log(87101)
  4130. FP.mtf_attrComments()
  4131. },2870)
  4132. */
  4133. if(pageInit_attrComments) hookSection();
  4134.  
  4135.  
  4136.  
  4137.  
  4138.  
  4139. let new_layoutStatus = wls.layoutStatus
  4140.  
  4141. const new_isExpandedChat = !(new_layoutStatus & LAYOUT_CHATROOM_COLLASPED) && (new_layoutStatus & LAYOUT_CHATROOM)
  4142. const new_isCollaspedChat = (new_layoutStatus & LAYOUT_CHATROOM_COLLASPED) && (new_layoutStatus & LAYOUT_CHATROOM)
  4143. const new_isTwoColumns = new_layoutStatus & LAYOUT_TWO_COLUMNS;
  4144. const new_isTheater = new_layoutStatus & LAYOUT_THEATER;
  4145. const new_isTabExpanded = new_layoutStatus & LAYOUT_TAB_EXPANDED;
  4146. const new_isFullScreen = new_layoutStatus & LAYOUT_FULLSCREEN;
  4147. const new_isExpandEPanel = new_layoutStatus & LAYOUT_ENGAGEMENT_PANEL_EXPAND;
  4148. if( ytdFlexyElm.getAttribute('tabview-selection')==='' && new_isTwoColumns && !new_isTheater && !new_isTabExpanded && !new_isFullScreen && !new_isExpandEPanel && !new_isExpandedChat ){
  4149. // e.g. engage panel removed after miniview and change video
  4150. setToActiveTab();
  4151. }else if(new_isExpandEPanel && querySelectorAllFromAnchor.call(ytdFlexyElm,'ytd-engagement-panel-section-list-renderer[visibility="ENGAGEMENT_PANEL_VISIBILITY_EXPANDED"]').length===0){
  4152. wls.layoutStatus = new_layoutStatus & (~LAYOUT_ENGAGEMENT_PANEL_EXPAND)
  4153. }
  4154.  
  4155. //mtf_attrFlexy();
  4156.  
  4157. FP.mtf_attrEngagementPanel();
  4158. FP.mtf_attrPlaylist();
  4159. //console.log(87102)
  4160. // try to omit in 2.5.x
  4161. //FP.mtf_attrComments();
  4162. FP.mtf_attrChatroom();
  4163.  
  4164.  
  4165. }
  4166.  
  4167. no_fix_contents_until = 0;
  4168. no_fix_playlist_until = 0;
  4169. scriptEnable = true;
  4170. regularCheck(1,0,null); // mutation happens when the page is not ready; call again as the page is ready.
  4171.  
  4172.  
  4173.  
  4174.  
  4175. }
  4176.  
  4177. tf();
  4178.  
  4179.  
  4180. }
  4181.  
  4182.  
  4183.  
  4184. function browserHistoryState(){
  4185. let maxCount = 5
  4186.  
  4187. if (!scriptEnable) return;
  4188. let ytdFlexyElm = kRef(ytdFlexy);
  4189. if(!ytdFlexyElm) return;
  4190.  
  4191. let cid = timeline.setInterval(function(){
  4192. if(newVideoPageCheck()){
  4193. timeline.clearInterval(cid);
  4194. }
  4195. if(--maxCount===0) timeline.clearInterval(cid);
  4196. },7)
  4197.  
  4198. }
  4199.  
  4200.  
  4201. window.addEventListener('hashchange', function() {
  4202. if (!scriptEnable) return;
  4203. console.log('hashchange');
  4204. browserHistoryState();
  4205. }, { capture: true })
  4206.  
  4207. window.addEventListener('popstate', function() {
  4208. if (!scriptEnable) return;
  4209. console.log('popstate');
  4210. browserHistoryState();
  4211. }, { capture: true })
  4212.  
  4213.  
  4214. // function clearMutationObserver(o, key) {
  4215. // if (o[key]) {
  4216. // o[key].takeRecords();
  4217. // o[key].disconnect();
  4218. // o[key] = null;
  4219. // return true;
  4220. // }
  4221. // }
  4222.  
  4223. // function initMutationObserver(o, key, callback) {
  4224. // clearMutationObserver(o, key);
  4225. // const mto = new MutationObserver(callback);
  4226. // o[key] = mto;
  4227. // return mto;
  4228. // }
  4229. // function initMutationObserver2(o, key, mtoFunc) {
  4230. // clearMutationObserver(o, key);
  4231. // const mto = mtoFunc();
  4232. // o[key] = mto;
  4233. // return mto;
  4234. // }
  4235.  
  4236. document.addEventListener('wheel', function(evt) {
  4237.  
  4238. if (!scriptEnable) return;
  4239. const displayedPlaylist_element = kRef(displayedPlaylist);
  4240. if (displayedPlaylist_element && elementContains.call(displayedPlaylist_element, evt.target)) {
  4241. evt.stopPropagation();
  4242. evt.stopImmediatePropagation();
  4243. }
  4244. }, { capture: true, passive: true });
  4245.  
  4246.  
  4247. function setVideosTwoColumns(/** @type {number} */ flag, /** @type {boolean} */ bool) {
  4248.  
  4249. //two columns to one column
  4250.  
  4251. /*
  4252. [placeholder-videos] ytd-watch-next-secondary-results-renderer.style-scope.ytd-watch-flexy
  4253.  
  4254. is-two-columns ="" => no is-two-columns
  4255. [placeholder-videos] tp-yt-paper-spinner#spinner.style-scope.ytd-continuation-item-renderer
  4256. no hidden => hidden =""
  4257. [placeholder-videos] div#button.style-scope.ytd-continuation-item-renderer
  4258. hidden ="" => no hidden
  4259.  
  4260. */
  4261.  
  4262. let cssSelector1 = '[placeholder-videos] ytd-watch-next-secondary-results-renderer.style-scope.ytd-watch-flexy'
  4263.  
  4264. let cssSelector2 = '[placeholder-videos] tp-yt-paper-spinner#spinner.style-scope.ytd-continuation-item-renderer'
  4265.  
  4266. let cssSelector3 = '[placeholder-videos] div#button.style-scope.ytd-continuation-item-renderer'
  4267.  
  4268. let res = {}
  4269. if (flag & 1) {
  4270. res.m1 = document.querySelector(cssSelector1)
  4271. if (res.m1) wAttr(res.m1, 'is-two-columns', bool ? '' : false);
  4272. }
  4273.  
  4274. if (flag & 2) {
  4275. res.m2 = document.querySelector(cssSelector2)
  4276. if (res.m2) wAttr(res.m2, 'hidden', bool ? false : '');
  4277. }
  4278.  
  4279. if (flag & 4) {
  4280. res.m3 = document.querySelector(cssSelector3)
  4281. if (res.m3) wAttr(res.m3, 'hidden', bool ? '' : false);
  4282. }
  4283.  
  4284.  
  4285. return res
  4286.  
  4287.  
  4288.  
  4289.  
  4290. }
  4291.  
  4292. let lastScrollFetch = 0;
  4293. // function isScrolledToEnd(){
  4294. // return (window.innerHeight + window.pageYOffset) >= document.scrollingElement.scrollHeight - 2;
  4295. // }
  4296. let lastOffsetTop = 0;
  4297. window.addEventListener('scroll', function(evt) {
  4298.  
  4299. //console.log(evt.target)
  4300.  
  4301. if (!scriptEnable) return;
  4302.  
  4303.  
  4304. if (!kRef(scrollingVideosList)) return;
  4305. if (videoListBeforeSearch) return;
  4306.  
  4307.  
  4308.  
  4309. let visibleHeight = document.scrollingElement.clientHeight;
  4310. let totalHeight = document.scrollingElement.scrollHeight;
  4311.  
  4312. if (totalHeight < visibleHeight * 1.5) return; // filter out two column view;
  4313.  
  4314. let z = window.pageYOffset + visibleHeight;
  4315. let h_advanced = totalHeight - (visibleHeight > 5 * 40 ? visibleHeight * 0.5 : 40);
  4316.  
  4317.  
  4318.  
  4319. if (z > h_advanced && !isWideScreenWithTwoColumns()) {
  4320.  
  4321. let ct = Date.now();
  4322. if (ct - lastScrollFetch < 500) return; //prevent continuous calling
  4323.  
  4324. lastScrollFetch = ct;
  4325.  
  4326. let res = setVideosTwoColumns(2 | 4, true)
  4327. if (res.m3 && res.m2) {
  4328.  
  4329. //wait for DOM change, just in case
  4330. requestAnimationFrame(() => {
  4331. let { offsetTop } = res.m2 // as visibility of m2 & m3 switched.
  4332.  
  4333. if (offsetTop - lastOffsetTop < 40) return; // in case bug, or repeating calling. // the next button shall below the this button
  4334. lastOffsetTop = offsetTop
  4335.  
  4336. res.m2.parentNode.dispatchEvent(new Event('yt-service-request-sent-button-renderer'))
  4337.  
  4338. res = null
  4339. })
  4340.  
  4341. } else {
  4342.  
  4343. res = null
  4344. }
  4345.  
  4346.  
  4347. }
  4348.  
  4349.  
  4350.  
  4351.  
  4352. }, { passive: true })
  4353.  
  4354. let sect_lastUpdate = 0;
  4355.  
  4356. let sect_holder = null;
  4357. let sect_hText = null;
  4358. let sect_hTime = 0;
  4359.  
  4360. let pendingOne = false;
  4361. let mtf_forceCheckLiveVideo_disable = 0;
  4362.  
  4363. let storeLastPanel = null;
  4364.  
  4365. /** @type {boolean | null} */
  4366. let deferredVarYTDHidden = null;
  4367. let scheduledCommentRefresh = false;
  4368.  
  4369. let pageInit_attrComments = false;
  4370.  
  4371.  
  4372.  
  4373. let mgChatFrame = {
  4374. setVar(elm) {
  4375. mgChatFrame.kVar = mWeakRef(elm)
  4376. },
  4377. getVar() {
  4378. return kRef(mgChatFrame.kVar)
  4379. },
  4380. inPage() {
  4381. let elm = mgChatFrame.getVar();
  4382. if (!elm) return false;
  4383. let ytdFlexyElm = kRef(ytdFlexy);
  4384. if(!ytdFlexyElm) return false;
  4385. return elementContains.call(ytdFlexyElm, elm)
  4386. }
  4387. };
  4388.  
  4389. const timeline = {
  4390. // after initialized (initObserver)
  4391. cn1:{},
  4392. cn2:{},
  4393. setTimeout( /** @type {TimerHandler} */ f,/** @type {number} */ d){
  4394. let cid = setTimeout(f,d)
  4395. timeline.cn1[cid]=true
  4396. return cid;
  4397. },
  4398. clearTimeout(/** @type {number} */ cid){
  4399. timeline.cn1[cid]=false; return clearTimeout(cid)
  4400. },
  4401. setInterval(/** @type {TimerHandler} */ f,/** @type {number} */ d){
  4402. let cid = setInterval(f,d);
  4403. timeline.cn2[cid]=true
  4404. return cid;
  4405. },
  4406. clearInterval(/** @type {number} */ cid){
  4407. timeline.cn2[cid]=false; return clearInterval(cid)
  4408. },
  4409. reset(){
  4410. for(let cid in timeline.cn1) timeline.cn1[cid] && clearTimeout(cid)
  4411. for(let cid in timeline.cn2) timeline.cn2[cid] && clearInterval(cid)
  4412. timeline.cn1={}
  4413. timeline.cn2={}
  4414. }
  4415. }
  4416.  
  4417. class AttributeMutationObserver extends MutationObserver {
  4418. constructor(flist){
  4419. super((mutations, observer)=>{
  4420. for(const mutation of mutations){
  4421. if (mutation.type === 'attributes') {
  4422. this.checker(mutation.target, mutation.attributeName)
  4423. }
  4424. }
  4425. })
  4426. this.flist=flist;
  4427. this.res={}
  4428. }
  4429. takeRecords(){
  4430. super.takeRecords();
  4431. }
  4432. disconnect(){
  4433. this._target = null;
  4434. super.disconnect();
  4435. }
  4436. observe(/** @type {Node} */ target){
  4437. if(this._target) return;
  4438. //console.log(123124, target)
  4439. this._target = mWeakRef(target);
  4440. //console.log(123125, kRef(this._target))
  4441. const options = {
  4442. attributes: true,
  4443. attributeFilter: Object.keys(this.flist),
  4444. //attributeFilter: [ "status", "username" ],
  4445. attributeOldValue: true
  4446. }
  4447. super.observe(target, options)
  4448. }
  4449. checker(/** @type {Node} */ target,/** @type {string} */ attributeName){
  4450. let nv = target.getAttribute(attributeName);
  4451. if(this.res[attributeName]!==nv){
  4452. this.res[attributeName] = nv
  4453. let f = this.flist[attributeName];
  4454. if(f) f(attributeName, nv);
  4455.  
  4456. }
  4457. }
  4458. check(delay = 0){
  4459. setTimeout(()=>{
  4460. let target = kRef(this._target)
  4461. if(target!==null){
  4462. for(const key of Object.keys(this.flist)){
  4463. this.checker(target,key)
  4464. }
  4465. }else{
  4466. console.log('target is null') //disconnected??
  4467. }
  4468. target = null;
  4469. },delay)
  4470. }
  4471. }
  4472.  
  4473. function goYoutubeGeniusLyrics(){
  4474.  
  4475. setTimeout(function $f(){
  4476.  
  4477. if(!document.documentElement.hasAttribute('w-engagement-panel-genius-lyrics')) return setTimeout($f,100)
  4478.  
  4479. document.documentElement.dispatchEvent(new CustomEvent('engagement-panel-genius-lyrics'))
  4480.  
  4481.  
  4482. },100)
  4483.  
  4484.  
  4485.  
  4486. }
  4487.  
  4488.  
  4489. /*
  4490.  
  4491. To be implement: timeline
  4492.  
  4493. timeline.setTimeout
  4494. timeline.clearTimeout
  4495. timeline.setInterval
  4496. timeline.clearInterval
  4497.  
  4498. */
  4499.  
  4500.  
  4501.  
  4502. //Object.keys($0).filter(key=>!(key in $0.constructor.prototype))
  4503.  
  4504. //Object.getOwnPropertyNames(window).filter(k=>k.startsWith('HTML'))
  4505. //Object.getOwnPropertyNames(window).filter(k=>k.startsWith('HTML')).filter(k=>$0 instanceof window[k])
  4506.  
  4507.  
  4508. /* --------------------------- browser's bug in -webkit-box ----------------------------------------- */
  4509.  
  4510. /*
  4511. fix bug for comment section - version 1.8.7
  4512. This issue is the bug in browser's rendering
  4513. I guess, this is due to the lines clamp with display:-webkit-box
  4514. use stupid coding to let it re-render when its content become visible
  4515. /*
  4516.  
  4517. ytd-expander[should-use-number-of-lines][collapsed] > #content.ytd-expander {
  4518. color: var(--yt-spec-text-primary);
  4519. display: -webkit-box;
  4520. overflow: hidden;
  4521. max-height: none;
  4522. -webkit-box-orient: vertical;
  4523. -webkit-line-clamp: var(--ytd-expander-max-lines, 4);
  4524. }
  4525.  
  4526. // v1.8.36 imposed a effective solution for fixing this bug
  4527.  
  4528. */
  4529.  
  4530. /* --------------------------- browser's bug in -webkit-box ----------------------------------------- */
  4531.  
  4532.  
  4533. /**
  4534. *
  4535.  
  4536.  
  4537. f.initChildrenObserver=function(){var a=this;this.observer=new MutationObserver(function(){return a.childrenChanged()});
  4538. this.observer.observe(this.$.content,{subtree:!0,childList:!0,attributes:!0});this.childrenChanged()};
  4539. f.childrenChanged=function(){var a=this;this.alwaysToggleable?this.canToggle=this.alwaysToggleable:this.canToggleJobId||(this.canToggleJobId=window.requestAnimationFrame(function(){$h(function(){a.canToggleJobId=0;a.calculateCanCollapse()})}))};
  4540.  
  4541.  
  4542. f.onIronResize=function(){this.recomputeOnResize&&this.childrenChanged()};
  4543.  
  4544.  
  4545. onButtonClick_:function(){this.fire("yt-close-upsell-dialog")},
  4546. computeHasHeader_:function(a){return!!a.headerBackgroundImage}});var geb;var heb;var ieb;var jeb;var xI=function(){var a=L.apply(this,arguments)||this;a.alignAuto=!1;a.collapsed=!0;a.isToggled=!1;a.alwaysCollapsed=!1;a.canToggle=!0;a.collapsedHeight=80;a.disableToggle=!1;a.alwaysToggleable=!1;a.reversed=!1;a.shouldUseNumberOfLines=!1;a.recomputeOnResize=!1;a.canToggleJobId=0;return a};
  4547. n(xI,L);f=xI.prototype;f.alwaysToggleableChanged=function(){this.alwaysToggleable&&(this.canToggle=!0)};
  4548.  
  4549.  
  4550. f.calculateCanCollapse=function(){this.canToggle=this.shouldUseNumberOfLines?this.alwaysToggleable||this.$.content.offsetHeight<this.$.content.scrollHeight:this.alwaysToggleable||this.$.content.scrollHeight>this.collapsedHeight};
  4551. f.detachObserver=function(){this.observer&&this.observer.disconnect()};
  4552.  
  4553. *
  4554. *
  4555. *
  4556. */
  4557.  
  4558.  
  4559.  
  4560.  
  4561. })();
  4562.  
  4563.  
  4564.  
  4565.  
  4566.  
  4567.  
  4568.  
  4569.  
  4570.  
  4571.  
  4572.  
  4573.  
  4574.  
  4575.  
  4576.  
  4577.  
  4578.  
  4579.  
  4580.  
  4581.  
  4582.  
  4583.  
  4584.  
  4585.  
  4586.  
  4587.  
  4588.  
  4589.  
  4590.  
  4591.  
  4592.  
  4593.  
  4594.  
  4595.  
  4596.  
  4597.  
  4598.  
  4599.  
  4600.  
  4601.  
  4602.  
  4603.  
  4604. // https://github.com/cyfung1031/Tabview-Youtube/raw/main/js/content.js
  4605.  
  4606. }
  4607.  
  4608.  
  4609. ;!(function $$() {
  4610. 'use strict';
  4611.  
  4612. if(document.documentElement==null) return window.requestAnimationFrame($$)
  4613.  
  4614. const cssTxt = GM_getResourceText("contentCSS");
  4615.  
  4616. function addStyle (styleText) {
  4617. const styleNode = document.createElement('style');
  4618. styleNode.type = 'text/css';
  4619. styleNode.textContent = styleText;
  4620. document.documentElement.appendChild(styleNode);
  4621. return styleNode;
  4622. }
  4623.  
  4624. addStyle (cssTxt);
  4625.  
  4626. main(window.$);
  4627.  
  4628.  
  4629. // Your code here...
  4630. })();