// ==UserScript== // @name YouTube + // @name:en YouTube + // @namespace by // @version 2.0.1 // @author diorhc // @description Вкладки для информации, комментариев, видео, плейлиста и скачивание видео и другие функции ↴ // @description:en Tabview YouTube and Download and others features ↴ // @match https://*.youtube.com/* // @match https://music.youtube.com/* // @match *://myactivity.google.com/* // @include *://www.youtube.com/feed/history/* // @include https://www.youtube.com // @include *://*.youtube.com/** // @exclude *://accounts.youtube.com/* // @exclude *://www.youtube.com/live_chat_replay* // @exclude *://www.youtube.com/persist_identity* // @exclude /^https?://\w+\.youtube\.com\/live_chat.*$/ // @exclude /^https?://\S+\.(txt|png|jpg|jpeg|gif|xml|svg|manifest|log|ini)[^\/]*$/ // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com // @license MIT // @grant GM_xmlhttpRequest // @grant unsafeWindow // @connect api.livecounts.io // @connect livecounts.io // @run-at document-start // @homepageURL https://github.com/diorhc/YoutubePlus // @supportURL https://github.com/diorhc/YoutubePlus/issues // @downloadURL none // ==/UserScript== // --- MODULE: utils.js --- // Shared utilities for YouTube+ modules (function () { 'use strict'; /** * Logs an error message with module context * @param {string} module - The module name where the error occurred * @param {string} message - Description of the error * @param {Error|*} error - The error object or value */ const logError = (module, message, error) => { try { console.error(`[YouTube+][${module}] ${message}:`, error); } catch {} }; /** * Creates a debounced function that delays invoking func until after wait milliseconds * @template {Function} T * @param {T} fn - The function to debounce * @param {number} ms - The number of milliseconds to delay * @param {{leading?: boolean}} [options={}] - Options object * @returns {T & {cancel: () => void}} The debounced function with a cancel method */ const debounce = (fn, ms, options = {}) => { let timeout = null; let lastArgs = null; let lastThis = null; /** @this {any} */ const debounced = function (...args) { lastArgs = args; lastThis = this; clearTimeout(timeout); if (options.leading && !timeout) { /** @type {Function} */ (fn).apply(this, args); } timeout = setTimeout(() => { if (!options.leading) /** @type {Function} */ (fn).apply(lastThis, lastArgs); timeout = null; lastArgs = null; lastThis = null; }, ms); }; debounced.cancel = () => { clearTimeout(timeout); timeout = null; lastArgs = null; lastThis = null; }; return /** @type {any} */ (debounced); }; /** * Creates a throttled function that only invokes func at most once per limit milliseconds * @template {Function} T * @param {T} fn - The function to throttle * @param {number} limit - The number of milliseconds to throttle invocations to * @returns {T} The throttled function */ const throttle = (fn, limit) => { let inThrottle = false; let lastResult; /** @this {any} */ const throttled = function (...args) { if (!inThrottle) { lastResult = /** @type {Function} */ (fn).apply(this, args); inThrottle = true; setTimeout(() => (inThrottle = false), limit); } return lastResult; }; return /** @type {any} */ (throttled); }; const StyleManager = (function () { const styles = new Map(); return { add(id, css) { try { let el = document.getElementById(id); if (!el) { el = document.createElement('style'); el.id = id; document.head.appendChild(el); } styles.set(id, css); el.textContent = Array.from(styles.values()).join('\n\n'); } catch (e) { logError('StyleManager', 'add failed', e); } }, remove(id) { try { styles.delete(id); const el = document.getElementById(id); if (el) el.remove(); } catch (e) { logError('StyleManager', 'remove failed', e); } }, clear() { for (const id of Array.from(styles.keys())) this.remove(id); }, }; })(); const cleanupManager = (function () { const observers = new Set(); const listeners = new Map(); const intervals = new Set(); const timeouts = new Set(); const animationFrames = new Set(); return { registerObserver(o) { try { observers.add(o); } catch {} return o; }, registerListener(target, ev, fn, opts) { try { target.addEventListener(ev, fn, opts); const key = Symbol(); listeners.set(key, { target, ev, fn, opts }); return key; } catch (e) { logError('cleanupManager', 'registerListener failed', e); return null; } }, registerInterval(id) { intervals.add(id); return id; }, registerTimeout(id) { timeouts.add(id); return id; }, registerAnimationFrame(id) { animationFrames.add(id); return id; }, cleanup() { try { for (const o of observers) { try { o.disconnect(); } catch {} } observers.clear(); for (const keyEntry of listeners.values()) { try { keyEntry.target.removeEventListener(keyEntry.ev, keyEntry.fn, keyEntry.opts); } catch {} } listeners.clear(); for (const id of intervals) clearInterval(id); intervals.clear(); for (const id of timeouts) clearTimeout(id); timeouts.clear(); for (const id of animationFrames) cancelAnimationFrame(id); animationFrames.clear(); } catch (e) { logError('cleanupManager', 'cleanup failed', e); } }, // expose for debug observers, listeners, intervals, timeouts, animationFrames, }; })(); const createElement = (tag, props = {}, children = []) => { try { const element = document.createElement(tag); Object.entries(props).forEach(([k, v]) => { if (k === 'className') element.className = v; else if (k === 'style' && typeof v === 'object') Object.assign(element.style, v); else if (k === 'dataset' && typeof v === 'object') Object.assign(element.dataset, v); else if (k.startsWith('on') && typeof v === 'function') { element.addEventListener(k.slice(2), v); } else element.setAttribute(k, v); }); children.forEach(c => { if (typeof c === 'string') element.appendChild(document.createTextNode(c)); else if (c instanceof Node) element.appendChild(c); }); return element; } catch (e) { logError('createElement', 'failed', e); return document.createElement('div'); } }; const waitForElement = (selector, timeout = 5000, parent = document.body) => new Promise((resolve, reject) => { if (!selector || typeof selector !== 'string') return reject(new Error('Invalid selector')); try { const el = parent.querySelector(selector); if (el) return resolve(el); } catch (e) { return reject(e); } const obs = new MutationObserver(() => { const el = parent.querySelector(selector); if (el) { try { obs.disconnect(); } catch {} resolve(el); } }); obs.observe(parent, { childList: true, subtree: true }); const id = setTimeout(() => { try { obs.disconnect(); } catch {} reject(new Error('timeout')); }, timeout); cleanupManager.registerTimeout(id); }); // Minimal storage wrapper const storage = { get(key, def = null) { try { const v = localStorage.getItem(key); return v === null ? def : JSON.parse(v); } catch { return def; } }, set(key, val) { try { localStorage.setItem(key, JSON.stringify(val)); return true; } catch { return false; } }, remove(key) { try { localStorage.removeItem(key); } catch {} }, }; // Expose a global YouTubeUtils if not present (non-destructive) if (typeof window !== 'undefined') { /** @type {any} */ (window).YouTubeUtils = /** @type {any} */ (window).YouTubeUtils || {}; const U = /** @type {any} */ (window).YouTubeUtils; U.logError = U.logError || logError; U.debounce = U.debounce || debounce; U.throttle = U.throttle || throttle; U.StyleManager = U.StyleManager || StyleManager; U.cleanupManager = U.cleanupManager || cleanupManager; U.createElement = U.createElement || createElement; U.waitForElement = U.waitForElement || waitForElement; U.storage = U.storage || storage; } })(); // --- MODULE: error-boundary.js --- // Global error boundary for YouTube+ userscript (function () { 'use strict'; /** * Error boundary configuration */ const ErrorBoundaryConfig = { maxErrors: 10, errorWindow: 60000, // 1 minute enableLogging: true, enableRecovery: true, storageKey: 'youtube_plus_errors', }; /** * Error tracking state */ const errorState = { errors: [], errorCount: 0, lastErrorTime: 0, isRecovering: false, }; /** * Error severity levels */ const ErrorSeverity = { LOW: 'low', MEDIUM: 'medium', HIGH: 'high', CRITICAL: 'critical', }; /** * Categorize error severity * @param {Error} error - The error object * @returns {string} Severity level */ const categorizeSeverity = error => { const message = error.message?.toLowerCase() || ''; if ( message.includes('cannot read') || message.includes('undefined') || message.includes('null') ) { return ErrorSeverity.MEDIUM; } if (message.includes('network') || message.includes('fetch') || message.includes('timeout')) { return ErrorSeverity.LOW; } if (message.includes('syntax') || message.includes('reference') || message.includes('type')) { return ErrorSeverity.HIGH; } if (message.includes('security') || message.includes('csp')) { return ErrorSeverity.CRITICAL; } return ErrorSeverity.MEDIUM; }; /** * Log error with context * @param {Error} error - The error object * @param {Object} context - Additional context information */ const logError = (error, context = {}) => { if (!ErrorBoundaryConfig.enableLogging) return; const fallbackMessage = error.message?.trim() || '(no message)'; const errorInfo = { timestamp: new Date().toISOString(), message: fallbackMessage, stack: error.stack, severity: categorizeSeverity(error), context: { url: window.location.href, userAgent: navigator.userAgent, ...context, }, }; console.error(`[YouTube+ Error Boundary] ${errorInfo.message}`, errorInfo); // Store error for analysis errorState.errors.push(errorInfo); if (errorState.errors.length > 50) { errorState.errors.shift(); // Keep only last 50 errors } // Persist to localStorage for debugging try { const stored = JSON.parse(localStorage.getItem(ErrorBoundaryConfig.storageKey) || '[]'); stored.push(errorInfo); if (stored.length > 20) stored.shift(); localStorage.setItem(ErrorBoundaryConfig.storageKey, JSON.stringify(stored)); } catch {} }; /** * Check if error rate is too high * @returns {boolean} True if error rate exceeded */ const isErrorRateExceeded = () => { const now = Date.now(); const windowStart = now - ErrorBoundaryConfig.errorWindow; // Count errors in the time window const recentErrors = errorState.errors.filter( e => new Date(e.timestamp).getTime() > windowStart ); return recentErrors.length >= ErrorBoundaryConfig.maxErrors; }; /** * Attempt to recover from error * @param {Error} error - The error that occurred * @param {Object} context - Error context */ const attemptRecovery = (error, context) => { if (!ErrorBoundaryConfig.enableRecovery || errorState.isRecovering) return; const severity = categorizeSeverity(error); if (severity === ErrorSeverity.CRITICAL) { console.error('[YouTube+] Critical error detected. Script may not function properly.'); return; } errorState.isRecovering = true; try { // Attempt recovery based on error type if (context.module && window.YouTubeUtils?.cleanupManager) { console.log(`[YouTube+] Attempting recovery for module: ${context.module}`); // Could implement module-specific recovery here } setTimeout(() => { errorState.isRecovering = false; }, 5000); } catch (recoveryError) { console.error('[YouTube+] Recovery attempt failed:', recoveryError); errorState.isRecovering = false; } }; /** * Global error handler * @param {ErrorEvent} event - The error event */ const handleError = event => { const error = event.error || new Error(event.message); const message = (error.message || event.message || '').trim(); const source = event.filename || ''; const isCrossOriginSource = source && !source.startsWith(window.location.origin) && !/YouTube\+/.test(source); if (!message && isCrossOriginSource) { // Ignore opaque cross-origin errors we can't introspect return false; } // Track error errorState.errorCount++; errorState.lastErrorTime = Date.now(); // Log error logError(error, { type: 'uncaught', filename: event.filename, lineno: event.lineno, colno: event.colno, }); // Check error rate if (isErrorRateExceeded()) { console.error( '[YouTube+] Error rate exceeded! Too many errors in short period. Some features may be disabled.' ); return; } // Attempt recovery attemptRecovery(error, { type: 'uncaught' }); // Don't prevent default error handling return false; }; /** * Unhandled promise rejection handler * @param {PromiseRejectionEvent} event - The rejection event */ const handleUnhandledRejection = event => { const error = event.reason instanceof Error ? event.reason : new Error(String(event.reason)); logError(error, { type: 'unhandledRejection', promise: event.promise, }); // Check error rate if (isErrorRateExceeded()) { console.error('[YouTube+] Promise rejection rate exceeded!'); return; } // Attempt recovery attemptRecovery(error, { type: 'unhandledRejection' }); }; /** * Safe function wrapper with error boundary * @param {Function} fn - Function to wrap * @param {string} context - Context identifier * @returns {Function} Wrapped function */ const withErrorBoundary = (fn, context = 'unknown') => { /** @this {any} */ return function (...args) { try { const fnAny = /** @type {any} */ (fn); return /** @this {any} */ fnAny.apply(this, args); } catch (error) { logError(error, { module: context, args }); attemptRecovery(error, { module: context }); return null; } }; }; /** * Safe async function wrapper with error boundary * @param {Function} fn - Async function to wrap * @param {string} context - Context identifier * @returns {Function} Wrapped async function */ const withAsyncErrorBoundary = (fn, context = 'unknown') => { /** @this {any} */ return async function (...args) { try { const fnAny = /** @type {any} */ (fn); return /** @this {any} */ await fnAny.apply(this, args); } catch (error) { logError(error, { module: context, args }); attemptRecovery(error, { module: context }); return null; } }; }; /** * Get error statistics * @returns {Object} Error statistics */ const getErrorStats = () => { return { totalErrors: errorState.errorCount, recentErrors: errorState.errors.length, lastErrorTime: errorState.lastErrorTime, isRecovering: errorState.isRecovering, errorsByType: errorState.errors.reduce((acc, e) => { acc[e.severity] = (acc[e.severity] || 0) + 1; return acc; }, {}), }; }; /** * Clear stored errors */ const clearErrors = () => { errorState.errors = []; try { localStorage.removeItem(ErrorBoundaryConfig.storageKey); } catch {} }; // Install global error handlers if (typeof window !== 'undefined') { window.addEventListener('error', handleError, true); window.addEventListener('unhandledrejection', handleUnhandledRejection, true); // Expose error boundary utilities window.YouTubeErrorBoundary = { withErrorBoundary, withAsyncErrorBoundary, getErrorStats, clearErrors, logError, }; console.log('[YouTube+] Error boundary initialized'); } })(); // --- MODULE: performance.js --- // Performance monitoring for YouTube+ userscript (function () { 'use strict'; /** * Performance monitoring configuration */ const PerformanceConfig = { enabled: true, sampleRate: 1.0, // 100% sampling storageKey: 'youtube_plus_performance', metricsRetention: 100, // Keep last 100 metrics enableConsoleOutput: false, }; /** * Performance metrics storage */ const metrics = { timings: new Map(), marks: new Map(), measures: [], resources: [], }; /** * Create a performance mark * @param {string} name - Mark name */ const mark = name => { if (!PerformanceConfig.enabled) return; try { if (typeof performance !== 'undefined' && performance.mark) { performance.mark(name); } metrics.marks.set(name, Date.now()); } catch (e) { console.warn('[YouTube+ Perf] Failed to create mark:', e); } }; /** * Measure time between two marks * @param {string} name - Measure name * @param {string} startMark - Start mark name * @param {string} endMark - End mark name (optional, defaults to now) * @returns {number} Duration in milliseconds */ const measure = (name, startMark, endMark) => { if (!PerformanceConfig.enabled) return 0; try { const startTime = metrics.marks.get(startMark); if (!startTime) { console.warn(`[YouTube+ Perf] Start mark "${startMark}" not found`); return 0; } const endTime = endMark ? metrics.marks.get(endMark) : Date.now(); const duration = endTime - startTime; const measureData = { name, startMark, endMark: endMark || 'now', duration, timestamp: Date.now(), }; metrics.measures.push(measureData); // Keep only recent measures if (metrics.measures.length > PerformanceConfig.metricsRetention) { metrics.measures.shift(); } if (PerformanceConfig.enableConsoleOutput) { console.log(`[YouTube+ Perf] ${name}: ${duration.toFixed(2)}ms`); } // Try native performance API if (typeof performance !== 'undefined' && performance.measure) { try { performance.measure(name, startMark, endMark); } catch {} } return duration; } catch (e) { console.warn('[YouTube+ Perf] Failed to measure:', e); return 0; } }; /** * Time a function execution * @param {string} name - Timer name * @param {Function} fn - Function to time * @returns {Function} Wrapped function */ const timeFunction = (name, fn) => { if (!PerformanceConfig.enabled) return fn; return /** @this {any} */ function (...args) { const startMark = `${name}-start-${Date.now()}`; mark(startMark); try { const fnAny = /** @type {any} */ (fn); const result = fnAny.apply(this, args); // Handle promises if (result && typeof result.then === 'function') { return result.finally(() => { measure(name, startMark, undefined); }); } measure(name, startMark, undefined); return result; } catch (error) { measure(name, startMark, undefined); throw error; } }; }; /** * Time an async function execution * @param {string} name - Timer name * @param {Function} fn - Async function to time * @returns {Function} Wrapped async function */ const timeAsyncFunction = (name, fn) => { if (!PerformanceConfig.enabled) return fn; return /** @this {any} */ async function (...args) { const startMark = `${name}-start-${Date.now()}`; mark(startMark); try { const fnAny = /** @type {any} */ (fn); const result = await fnAny.apply(this, args); measure(name, startMark, undefined); return result; } catch (error) { measure(name, startMark, undefined); throw error; } }; }; /** * Record custom metric * @param {string} name - Metric name * @param {number} value - Metric value * @param {Object} metadata - Additional metadata */ const recordMetric = (name, value, metadata = {}) => { if (!PerformanceConfig.enabled) return; const metric = { name, value, timestamp: Date.now(), ...metadata, }; metrics.timings.set(name, metric); if (PerformanceConfig.enableConsoleOutput) { console.log(`[YouTube+ Perf] ${name}: ${value}`, metadata); } }; /** * Get performance statistics * @param {string} metricName - Optional metric name filter * @returns {Object} Performance statistics */ const getStats = metricName => { if (metricName) { const filtered = metrics.measures.filter(m => m.name === metricName); if (filtered.length === 0) return null; const durations = filtered.map(m => m.duration); return { name: metricName, count: durations.length, min: Math.min(...durations), max: Math.max(...durations), avg: durations.reduce((a, b) => a + b, 0) / durations.length, latest: durations[durations.length - 1], }; } // Get all stats const allMetrics = {}; const metricNames = [...new Set(metrics.measures.map(m => m.name))]; metricNames.forEach(name => { allMetrics[name] = getStats(name); }); return { metrics: allMetrics, totalMeasures: metrics.measures.length, totalMarks: metrics.marks.size, customMetrics: Object.fromEntries(metrics.timings), }; }; /** * Export metrics to JSON * @returns {string} JSON string of metrics */ const exportMetrics = () => { const data = { timestamp: new Date().toISOString(), userAgent: navigator.userAgent, url: window.location.href, stats: getStats(undefined), measures: metrics.measures, customMetrics: Object.fromEntries(metrics.timings), }; return JSON.stringify(data, null, 2); }; /** * Clear all performance metrics */ const clearMetrics = () => { metrics.timings.clear(); metrics.marks.clear(); metrics.measures = []; metrics.resources = []; try { localStorage.removeItem(PerformanceConfig.storageKey); } catch {} if (typeof performance !== 'undefined' && performance.clearMarks) { try { performance.clearMarks(); performance.clearMeasures(); } catch {} } }; /** * Monitor DOM mutations performance * @param {Element} element - Element to monitor * @param {string} name - Monitor name * @returns {MutationObserver} The observer instance */ const monitorMutations = (element, name) => { if (!PerformanceConfig.enabled) return null; let mutationCount = 0; const startTime = Date.now(); const observer = new MutationObserver(mutations => { mutationCount += mutations.length; recordMetric(`${name}-mutations`, mutationCount, { elapsed: Date.now() - startTime, }); }); observer.observe(element, { childList: true, subtree: true, attributes: true, }); return observer; }; /** * Get browser performance entries * @param {string} type - Entry type filter * @returns {Array} Performance entries */ const getPerformanceEntries = type => { if (typeof performance === 'undefined' || !performance.getEntriesByType) { return []; } try { return performance.getEntriesByType(type); } catch { return []; } }; /** * Log page load performance */ const logPageLoadMetrics = () => { if (!PerformanceConfig.enabled) return; try { const navigation = getPerformanceEntries('navigation')[0]; if (navigation) { recordMetric('page-load-time', navigation.loadEventEnd - navigation.fetchStart); recordMetric('dom-content-loaded', navigation.domContentLoadedEventEnd); recordMetric('dom-interactive', navigation.domInteractive); } } catch (e) { console.warn('[YouTube+ Perf] Failed to log page metrics:', e); } }; // Auto-log page load metrics if (typeof window !== 'undefined') { if (document.readyState === 'complete') { logPageLoadMetrics(); } else { window.addEventListener('load', logPageLoadMetrics, { once: true }); } // Expose performance monitoring API window.YouTubePerformance = { mark, measure, timeFunction, timeAsyncFunction, recordMetric, getStats, exportMetrics, clearMetrics, monitorMutations, getPerformanceEntries, config: PerformanceConfig, }; console.log('[YouTube+] Performance monitoring initialized'); } })(); // --- MODULE: main.js --- /** * Identity function that returns the input value unchanged * @param {*} value - The value to return * @returns {*} The same value */ // @ts-nocheck const identityFn = value => value; /** * Ensure TrustedTypes policy exists for secure HTML handling * @returns {{createHTML: Function, error: Error|null}} Policy object with createHTML function and error status */ function ensureTrustedTypesPolicy() { if (typeof trustedTypes === 'undefined') { return { createHTML: identityFn, error: null }; } try { if (trustedTypes.defaultPolicy === null) { trustedTypes.createPolicy('default', { createHTML: identityFn, createScriptURL: identityFn, createScript: identityFn, }); } const policy = trustedTypes.defaultPolicy; const createHTML = policy && typeof policy.createHTML === 'function' ? policy.createHTML.bind(policy) : identityFn; // Validate policy works const testDiv = document.createElement('div'); testDiv.innerHTML = createHTML('1'); return { createHTML, error: null }; } catch (error) { console.error('TrustedTypes policy creation failed:', error); return { createHTML: identityFn, error }; } } /** * Create browser tick scheduler for microtask execution * @param {Function} existing - Existing scheduler to reuse if version compatible * @returns {Function} Scheduler function with version property */ function createNextBrowserTick(existing) { if (existing && typeof existing === 'function' && existing.version >= 2) { return existing; } const SafePromise = (async () => { })().constructor; const queue = typeof queueMicrotask === 'function' ? callback => queueMicrotask(callback) : callback => SafePromise.resolve().then(callback); const scheduler = callback => { if (typeof callback === 'function') { queue(callback); return; } return SafePromise.resolve(); }; scheduler.version = 2; return scheduler; } const { createHTML, error: trustHTMLErr } = ensureTrustedTypesPolicy(); if (trustHTMLErr) { console.error( '[YouTube+] TrustedHTML Error: Script cannot run due to Content Security Policy restrictions', trustHTMLErr ); throw new Error('CSP restriction - cannot initialize TrustedTypes'); } // Export createHTML for use in modules if needed if (typeof window !== 'undefined') { window._ytplusCreateHTML = createHTML; } const nextBrowserTick = createNextBrowserTick( (typeof window !== 'undefined' && window.nextBrowserTick) || undefined ); if ( typeof window !== 'undefined' && (!window.nextBrowserTick || window.nextBrowserTick.version < 2) ) { window.nextBrowserTick = nextBrowserTick; } // ----------------------------------------------------------------------------------------------------------------------------- /** * Main execution script for YouTube tab view * @param {string} _communicationKey - Unique key for cross-context communication (reserved for future use) */ const executionScript = _communicationKey => { /** @const {boolean} Debug flag for attachment/detachment events */ const DEBUG_5084 = false; /** @const {boolean} Debug flag for tab operations */ const DEBUG_5085 = false; /** @const {boolean} Auto-switch to comments tab when available */ const TAB_AUTO_SWITCH_TO_COMMENTS = false; // Configuration validation /** @const {number} Maximum value for attributes before overflow reset */ const MAX_ATTRIBUTE_VALUE = 1e9; /** @const {number} Reset value when attribute exceeds max */ const ATTRIBUTE_RESET_VALUE = 9; // Validate configuration if ( MAX_ATTRIBUTE_VALUE <= 0 || ATTRIBUTE_RESET_VALUE < 0 || ATTRIBUTE_RESET_VALUE >= MAX_ATTRIBUTE_VALUE ) { console.error( '[YouTube+] Invalid configuration: MAX_ATTRIBUTE_VALUE and ATTRIBUTE_RESET_VALUE must be valid positive numbers' ); } // Reuse utility functions from parent scope const identityFn = value => value; const ensureTrustedTypesPolicyLocal = () => { if (typeof trustedTypes === 'undefined') { return { createHTML: identityFn, error: null }; } try { if (trustedTypes.defaultPolicy === null) { trustedTypes.createPolicy('default', { createHTML: identityFn, createScriptURL: identityFn, createScript: identityFn, }); } const policy = trustedTypes.defaultPolicy; const createHTML = policy?.createHTML?.bind?.(policy) ?? identityFn; // Validate policy works const testDiv = document.createElement('div'); testDiv.innerHTML = createHTML('1'); return { createHTML, error: null }; } catch (error) { console.error('[YouTube+] TrustedTypes local policy failed:', error); return { createHTML: identityFn, error }; } }; /** * Create browser tick scheduler for microtask execution * @param {Function} existing - Existing scheduler to reuse if version compatible * @returns {Function} Scheduler function */ const createNextBrowserTickLocal = existing => { if (existing?.version >= 2) { return existing; } const SafePromise = (async () => { })().constructor; const queue = typeof queueMicrotask === 'function' ? callback => queueMicrotask(callback) : callback => SafePromise.resolve().then(callback); const scheduler = callback => { if (typeof callback === 'function') { queue(callback); return; } return SafePromise.resolve(); }; scheduler.version = 2; return scheduler; }; const { createHTML, error: trustHTMLErr } = ensureTrustedTypesPolicyLocal(); if (trustHTMLErr) { console.error( '[YouTube+] TrustedHTML Error: Script cannot run due to CSP restrictions', trustHTMLErr ); return; // Exit execution script gracefully } const nextBrowserTick = createNextBrowserTickLocal( (typeof window !== 'undefined' && window.nextBrowserTick) || undefined ); if ( typeof window !== 'undefined' && (!window.nextBrowserTick || window.nextBrowserTick.version < 2) ) { window.nextBrowserTick = nextBrowserTick; } try { let executionFinished = 0; if (typeof CustomElementRegistry === 'undefined') return; if (CustomElementRegistry.prototype.define000) return; if (typeof CustomElementRegistry.prototype.define !== 'function') return; /** @type {HTMLElement} HTMLElement constructor reference */ const HTMLElement_ = HTMLElement.prototype.constructor; /** * Simple cache for frequently used querySelector results * Helps reduce DOM traversal overhead */ const selectorCache = new Map(); // eslint-disable-next-line no-unused-vars const _CACHE_MAX_SIZE = 50; // Reserved for future cache implementation const CACHE_TTL = 5000; // 5 seconds /** * Clear expired cache entries */ const clearExpiredCache = () => { const now = Date.now(); for (const [key, value] of selectorCache.entries()) { if (now - value.timestamp > CACHE_TTL) { selectorCache.delete(key); } } }; // Periodically clear expired cache setInterval(clearExpiredCache, CACHE_TTL); /** * Query single element from a specific parent * @param {Element} elm - Parent element to query from * @param {string} selector - CSS selector string * @returns {Element | null} Found element or null */ const qsOne = (elm, selector) => { return HTMLElement_.prototype.querySelector.call(elm, selector); }; /** * Query all matching elements from a specific parent * @param {Element} elm - Parent element to query from * @param {string} selector - CSS selector string * @returns {NodeListOf} NodeList of found elements */ // eslint-disable-next-line no-unused-vars const _qsAll = (elm, selector) => { return HTMLElement_.prototype.querySelectorAll.call(elm, selector); }; const pdsBaseDF = Object.getOwnPropertyDescriptors(DocumentFragment.prototype); Object.defineProperties(DocumentFragment.prototype, { replaceChildren000: pdsBaseDF.replaceChildren, }); const pdsBaseNode = Object.getOwnPropertyDescriptors(Node.prototype); Object.defineProperties(Node.prototype, { appendChild000: pdsBaseNode.appendChild, insertBefore000: pdsBaseNode.insertBefore, }); const pdsBaseElement = Object.getOwnPropertyDescriptors(Element.prototype); Object.defineProperties(Element.prototype, { setAttribute000: pdsBaseElement.setAttribute, getAttribute000: pdsBaseElement.getAttribute, hasAttribute000: pdsBaseElement.hasAttribute, removeAttribute000: pdsBaseElement.removeAttribute, querySelector000: pdsBaseElement.querySelector, replaceChildren000: pdsBaseElement.replaceChildren, }); /** * Set attribute only if value has changed (optimization to reduce DOM operations) * @param {string} p - Attribute name * @param {*} v - Attribute value */ Element.prototype.setAttribute111 = function (p, v) { if (!p || typeof p !== 'string') { console.warn('[YouTube+] setAttribute111: invalid attribute name', p); return; } try { v = `${v}`; if (this.getAttribute000(p) === v) return; this.setAttribute000(p, v); } catch (error) { console.warn('[YouTube+] setAttribute111 failed:', error, p, v); } }; /** * Increment attribute value (with overflow protection) * @param {string} p - Attribute name * @returns {number} New attribute value */ Element.prototype.incAttribute111 = function (p) { if (!p || typeof p !== 'string') { console.warn('[YouTube+] incAttribute111: invalid attribute name', p); return 0; } try { let v = +this.getAttribute000(p) || 0; v = v > MAX_ATTRIBUTE_VALUE ? ATTRIBUTE_RESET_VALUE : v + 1; this.setAttribute000(p, `${v}`); return v; } catch (error) { console.warn('[YouTube+] incAttribute111 failed:', error, p); return 0; } }; /** * Assign children elements in specific order while managing DOM efficiently * @param {Array|null} previousSiblings - Nodes to place before target node * @param {Node} node - Target node (required) * @param {Array|null} nextSiblings - Nodes to place after target node */ Element.prototype.assignChildren111 = function (previousSiblings, node, nextSiblings) { if (!node) { console.warn('[YouTube+] assignChildren111: node is required'); return; } try { // Collect all child nodes except the target node let nodeList = []; for (let t = this.firstChild; t instanceof Node; t = t.nextSibling) { if (t === node) continue; nodeList.push(t); } inPageRearrange = true; if (node.parentNode === this) { // Node is already a child, rearrange efficiently let fm = new DocumentFragment(); if (nodeList.length > 0) { fm.replaceChildren000(...nodeList); } if (previousSiblings?.length > 0) { fm.replaceChildren000(...previousSiblings); this.insertBefore000(fm, node); } if (nextSiblings?.length > 0) { fm.replaceChildren000(...nextSiblings); this.appendChild000(fm); } fm.replaceChildren000(); fm = null; } else { // Node is not a child yet, replace all children this.replaceChildren000(...(previousSiblings || []), node, ...(nextSiblings || [])); } inPageRearrange = false; // Cleanup disconnected nodes if (nodeList.length > 0) { for (const t of nodeList) { if (t instanceof Element && t.isConnected === false) { t.remove(); // Trigger removal events } } } nodeList.length = 0; nodeList = null; } catch (error) { inPageRearrange = false; console.error('[YouTube+] assignChildren111 failed:', error); } }; // ============================================================================================================================================================================================================================================================================== const DISABLE_FLAGS_SHADYDOM_FREE = true; /** * * Minified Code from https://greasyfork.org/en/scripts/475632-ytconfighacks/code (ytConfigHacks) * Date: 2024.04.17 * Minifier: https://www.toptal.com/developers/javascript-minifier * */ (() => { const e = 'undefined' != typeof unsafeWindow ? unsafeWindow : this instanceof Window ? this : window; if (!e._ytConfigHacks) { let t = 4; class n extends Set { add(e) { if (t <= 0) return console.warn('yt.config_ is already applied on the page.'); 'function' == typeof e && super.add(e); } } const a = (async () => { })().constructor, i = (e._ytConfigHacks = new n()); let l = () => { const t = e.ytcsi.originalYtcsi; t && ((e.ytcsi = t), (l = null)); }; let c = null; const o = () => { if (t >= 1) { const n = (e.yt || 0).config_ || (e.ytcfg || 0).data_ || 0; if ('string' == typeof n.INNERTUBE_API_KEY && 'object' == typeof n.EXPERIMENT_FLAGS) { for (const a of (--t <= 0 && l && l(), (c = !0), i)) a(n); } } }; let f = 1; const d = t => { if ((t = t || e.ytcsi)) { return ( (e.ytcsi = new Proxy(t, { get: (e, t) => ('originalYtcsi' === t ? e : (o(), c && --f <= 0 && l && l(), e[t])), })), !0 ); } }; d() || Object.defineProperty(e, 'ytcsi', { get() { }, set: t => (t && (delete e.ytcsi, d(t)), !0), enumerable: !1, configurable: !0, }); const { addEventListener: s, removeEventListener: y } = Document.prototype; function r(t) { (o(), t && e.removeEventListener('DOMContentLoaded', r, !1)); } (new a(e => { if ('undefined' != typeof AbortSignal) { (s.call(document, 'yt-page-data-fetched', e, { once: !0 }), s.call(document, 'yt-navigate-finish', e, { once: !0 }), s.call(document, 'spfdone', e, { once: !0 })); } else { const t = () => { (e(), y.call(document, 'yt-page-data-fetched', t, !1), y.call(document, 'yt-navigate-finish', t, !1), y.call(document, 'spfdone', t, !1)); }; (s.call(document, 'yt-page-data-fetched', t, !1), s.call(document, 'yt-navigate-finish', t, !1), s.call(document, 'spfdone', t, !1)); } }).then(o), new a(e => { if ('undefined' != typeof AbortSignal) { s.call(document, 'yt-action', e, { once: !0, capture: !0 }); } else { const t = () => { (e(), y.call(document, 'yt-action', t, !0)); }; s.call(document, 'yt-action', t, !0); } }).then(o), a.resolve().then(() => { 'loading' !== document.readyState ? r() : e.addEventListener('DOMContentLoaded', r, !1); })); } })(); let configOnce = false; window._ytConfigHacks.add(config_ => { if (configOnce) return; configOnce = true; const EXPERIMENT_FLAGS = config_.EXPERIMENT_FLAGS || 0; const EXPERIMENTS_FORCED_FLAGS = config_.EXPERIMENTS_FORCED_FLAGS || 0; for (const flags of [EXPERIMENT_FLAGS, EXPERIMENTS_FORCED_FLAGS]) { if (flags) { // flags.kevlar_watch_metadata_refresh_no_old_secondary_data = false; // flags.live_chat_overflow_hide_chat = false; flags.web_watch_chat_hide_button_killswitch = false; flags.web_watch_theater_chat = false; // for re-openable chat (ytd-watch-flexy's liveChatCollapsed is always undefined) flags.suppress_error_204_logging = true; flags.kevlar_watch_grid = false; // A/B testing for watch grid if (DISABLE_FLAGS_SHADYDOM_FREE) { flags.enable_shadydom_free_scoped_node_methods = false; flags.enable_shadydom_free_scoped_query_methods = false; flags.enable_shadydom_free_scoped_readonly_properties_batch_one = false; flags.enable_shadydom_free_parent_node = false; flags.enable_shadydom_free_children = false; flags.enable_shadydom_free_last_child = false; } } } }); // =================================================================================================================================================================================================================================== /* globals WeakRef:false */ /** @type {(o: Object | null) => WeakRef | null} */ const mWeakRef = typeof WeakRef === 'function' ? o => (o ? new WeakRef(o) : null) : o => o || null; // typeof InvalidVar == 'undefined' /** @type {(wr: Object | null) => Object | null} */ const kRef = wr => (wr && wr.deref ? wr.deref() : wr); /** @type {globalThis.PromiseConstructor} */ /** @type {PromiseConstructor} Safe Promise constructor (YouTube hacks Promise in WaterFox Classic) */ const Promise = (async () => { })().constructor; /** * Create a promise that resolves after a delay * @param {number} delay - Delay in milliseconds * @returns {Promise} Promise that resolves after the delay */ const delayPn = delay => new Promise(fn => setTimeout(fn, delay)); /** * Get polymer controller or instance from element * @param {*} o - Element or object to inspect * @returns {*} Polymer controller, instance, or the object itself */ const insp = o => (o ? o.polymerController || o.inst || o || 0 : o || 0); /** @type {Function} Bound setTimeout to ensure correct context */ const setTimeout_ = setTimeout.bind(window); /** * Error handler for promises - logs errors with context * @param {Error} error - The error that occurred * @param {string} context - Context information about where the error occurred */ const handlePromiseError = (error, context = 'Unknown') => { if (error) { console.error(`[YouTube+] Promise error in ${context}:`, error); } }; /** * Promise class with external resolve/reject methods * Useful for creating deferred promises that can be resolved externally */ const PromiseExternal = ((resolve_, reject_) => { const h = (resolve, reject) => { resolve_ = resolve; reject_ = reject; }; return class PromiseExternal extends Promise { constructor(cb = h) { super(cb); if (cb === h) { /** @type {(value: any) => void} */ this.resolve = resolve_; /** @type {(reason?: any) => void} */ this.reject = reject_; } } }; })(); // ------------------------------------------------------------------------ Event Listener Options ------------------------------------------------------------------------ /** @const {boolean} Check if passive event listeners are supported */ const isPassiveArgSupport = typeof IntersectionObserver === 'function'; /** @const {Object|boolean} Event listener options for bubble phase with passive */ // eslint-disable-next-line no-unused-vars const _bubblePassive = isPassiveArgSupport ? { capture: false, passive: true } : false; /** @const {Object|boolean} Event listener options for capture phase with passive */ const capturePassive = isPassiveArgSupport ? { capture: true, passive: true } : true; /** * Helper class to manage binary flags as string attributes */ class Attributer { /** * @param {string} list - String where each character represents a flag */ constructor(list) { this.list = list; this.flag = 0; } /** * Convert active flags to string representation * @returns {string} String with characters for active flags */ makeString() { let k = 1; let s = ''; let i = 0; while (this.flag >= k) { if (this.flag & k) { s += this.list[i]; } i++; k <<= 1; } return s; } } /** @type {Attributer} Module loaded state tracker */ const mLoaded = new Attributer('icp'); /** @type {WeakMap} WeakMap for self-referencing objects */ const wrSelfMap = new WeakMap(); /** * Elements cache using Proxy with WeakRef for memory efficiency * Automatically manages element references and prevents memory leaks * @type {Object.} */ const elements = new Proxy( { related: null, comments: null, infoExpander: null, }, { get(target, prop) { return kRef(target[prop]); }, set(target, prop, value) { if (value) { let wr = wrSelfMap.get(value); if (!wr) { wr = mWeakRef(value); wrSelfMap.set(value, wr); } target[prop] = wr; } else { target[prop] = null; } return true; }, } ); /** * Get the main info element from the infoExpander * @returns {Element|null} The main info element or null */ const getMainInfo = () => { const infoExpander = elements.infoExpander; if (!infoExpander) return null; const mainInfo = infoExpander.matches('[tyt-main-info]') ? infoExpander : infoExpander.querySelector000('[tyt-main-info]'); return mainInfo || null; }; /** * Wrap async function to execute in next microtask * @param {Function} asyncFn - Async function to wrap * @returns {Function} Wrapped function */ // eslint-disable-next-line no-unused-vars const _asyncWrap = asyncFn => { return () => { Promise.resolve().then(asyncFn); }; }; let pageType = null; let pageLang = 'en'; /** * Localized strings for different languages * @type {Object.>} */ const langWords = { en: { info: 'Info', videos: 'Videos', playlist: 'Playlist', }, jp: { info: '情報', videos: '動画', playlist: '再生リスト', }, tw: { info: '資訊', videos: '影片', playlist: '播放清單', }, cn: { info: '资讯', videos: '视频', playlist: '播放列表', }, du: { info: 'Info', videos: 'Videos', playlist: 'Playlist', }, fr: { info: 'Info', videos: 'Vidéos', playlist: 'Playlist', }, kr: { info: '정보', videos: '동영상', playlist: '재생목록', }, ru: { info: 'Описание', videos: 'Видео', playlist: 'Плейлист', }, }; const svgComments = ``.trim(); const svgVideos = ``.trim(); const svgInfo = ``.trim(); const svgPlayList = ``.trim(); // eslint-disable-next-line no-unused-vars const svgDiag1 = ``; // eslint-disable-next-line no-unused-vars const svgDiag2 = ``; /** * Get GMT offset for the current timezone * @returns {string} GMT offset string (e.g., "+9" or "-5") */ // eslint-disable-next-line no-unused-vars const getGMT = () => { const m = new Date('2023-01-01T00:00:00Z'); return m.getDate() === 1 ? `+${m.getHours()}` : `-${24 - m.getHours()}`; }; /** * Get localized word based on current page language * @param {string} tag - Word identifier * @returns {string} Localized word or empty string */ function getWord(tag) { return langWords[pageLang]?.[tag] || langWords['en']?.[tag] || ''; } /** * Create SVG element string * @param {number} w - Width * @param {number} h - Height * @param {number} vw - ViewBox width * @param {number} vh - ViewBox height * @param {string} p - Path data * @param {string} m - Optional class name * @returns {string} SVG element string */ const svgElm = (w, h, vw, vh, p, m) => `${p}`; const hiddenTabsByUserCSS = 0; /** * Generate HTML for tab buttons * @returns {string} HTML string for tabs */ function getTabsHTML() { const sTabBtnVideos = `${svgElm(16, 16, 90, 90, svgVideos)}${getWord('videos')}`; const sTabBtnInfo = `${svgElm(16, 16, 60, 60, svgInfo)}${getWord('info')}`; const sTabBtnPlayList = `${svgElm(16, 16, 20, 20, svgPlayList)}${getWord('playlist')}`; const str1 = `
`; const str_fbtns = `
`.replace(/[\r\n]+/g, ''); const str_tabs = [ `${sTabBtnInfo}${str1}${str_fbtns}`, `${svgElm(16, 16, 120, 120, svgComments)}${str1}${str_fbtns}`, `${sTabBtnVideos}${str1}${str_fbtns}`, `${sTabBtnPlayList}${str1}${str_fbtns}`, ].join(''); const addHTML = `
${str_tabs}
`; return addHTML; } function getLang() { const htmlLang = ((document || 0).documentElement || 0).lang || ''; // Language mapping with optimized lookup const langMap = { en: 'en', 'en-GB': 'en', de: 'du', 'de-DE': 'du', fr: 'fr', 'fr-CA': 'fr', 'fr-FR': 'fr', 'zh-Hant': 'tw', 'zh-Hant-HK': 'tw', 'zh-Hant-TW': 'tw', 'zh-Hans': 'cn', 'zh-Hans-CN': 'cn', ja: 'jp', 'ja-JP': 'jp', ko: 'kr', 'ko-KR': 'kr', ru: 'ru', 'ru-RU': 'ru', }; return langMap[htmlLang] || 'en'; } function getLangForPage() { const lang = getLang(); pageLang = langWords[lang] ? lang : 'en'; } /** @type {Object.} */ const _locks = {}; const lockGet = new Proxy(_locks, { get(target, prop) { return target[prop] || 0; }, set(_target, _prop, _val) { return true; }, }); const lockSet = new Proxy(_locks, { get(target, prop) { if (target[prop] > MAX_ATTRIBUTE_VALUE) target[prop] = ATTRIBUTE_RESET_VALUE; return (target[prop] = (target[prop] || 0) + 1); }, set(_target, _prop, _val) { return true; }, }); // note: xxxxxxxxxAsyncLock is not expected for calling multiple time in a short period. // it is just to split the process into microTasks. const videosElementProvidedPromise = new PromiseExternal(); const navigateFinishedPromise = new PromiseExternal(); let isRightTabsInserted = false; const rightTabsProvidedPromise = new PromiseExternal(); const infoExpanderElementProvidedPromise = new PromiseExternal(); const cmAttr = document.createComment('1'); const cmAttrStack = []; /** * Add function to attribute change stack * @param {Function} f - Function to execute on attribute change */ // eslint-disable-next-line no-unused-vars const cmAttrStackPush = f => { cmAttrStack.push(f); cmAttr.data = `${(cmAttr.data & 7) + 1}`; }; const cmAttrObs = new MutationObserver(() => { cmAttrStack.forEach(fn => fn()); }); cmAttrObs.observe(cmAttr, { characterData: true }); /** * Function to calculate if element can collapse * @param {*} _s - Parameter (unused but kept for compatibility) */ const funcCanCollapse = function (_s) { const content = this.content || this.$.content; this.canToggle = this.shouldUseNumberOfLines && (this.alwaysCollapsed || this.collapsed || this.isToggled === false) ? this.alwaysToggleable || this.isToggled || (content && content.offsetHeight < content.scrollHeight) : this.alwaysToggleable || this.isToggled || (content && content.scrollHeight > this.collapsedHeight); }; const aoChatAttrChangeFn = async lockId => { if (lockGet['aoChatAttrAsyncLock'] !== lockId) return; const chatElm = elements.chat; const ytdFlexyElm = elements.flexy; if (chatElm && ytdFlexyElm) { const isChatCollapsed = chatElm.hasAttribute000('collapsed'); if (isChatCollapsed) { ytdFlexyElm.setAttribute111('tyt-chat-collapsed', ''); } else { ytdFlexyElm.removeAttribute000('tyt-chat-collapsed'); } ytdFlexyElm.setAttribute111('tyt-chat', isChatCollapsed ? '-' : '+'); } }; const aoPlayListAttrChangeFn = async lockId => { if (lockGet['aoPlayListAttrAsyncLock'] !== lockId) return; const playlistElm = elements.playlist; const ytdFlexyElm = elements.flexy; if (playlistElm && ytdFlexyElm) { if (playlistElm.hasAttribute000('collapsed')) { ytdFlexyElm.removeAttribute000('tyt-playlist-expanded'); } else { ytdFlexyElm.setAttribute111('tyt-playlist-expanded', ''); } } else if (ytdFlexyElm) { ytdFlexyElm.removeAttribute000('tyt-playlist-expanded'); } }; const aoChat = new MutationObserver(() => { Promise.resolve(lockSet['aoChatAttrAsyncLock']) .then(aoChatAttrChangeFn) .catch(err => handlePromiseError(err, 'aoChatAttrChange')); }); const aoPlayList = new MutationObserver(() => { Promise.resolve(lockSet['aoPlayListAttrAsyncLock']) .then(aoPlayListAttrChangeFn) .catch(err => handlePromiseError(err, 'aoPlayListAttrChange')); }); const aoComment = new MutationObserver(async mutations => { const commentsArea = elements.comments; const ytdFlexyElm = elements.flexy; //tyt-comments-video-id //tyt-comments-data-status // hidden if (!commentsArea) return; let bfHidden = false; let bfCommentsVideoId = false; let bfCommentDisabled = false; for (const mutation of mutations) { if (mutation.attributeName === 'hidden' && mutation.target === commentsArea) { bfHidden = true; } else if ( mutation.attributeName === 'tyt-comments-video-id' && mutation.target === commentsArea ) { bfCommentsVideoId = true; } else if ( mutation.attributeName === 'tyt-comments-data-status' && mutation.target === commentsArea ) { bfCommentDisabled = true; } } if (bfHidden) { if (!commentsArea.hasAttribute000('hidden')) { Promise.resolve(commentsArea) .then(eventMap['settingCommentsVideoId']) .catch(err => handlePromiseError(err, 'settingCommentsVideoId')); } Promise.resolve(lockSet['removeKeepCommentsScrollerLock']) .then(removeKeepCommentsScroller) .catch(err => handlePromiseError(err, 'removeKeepCommentsScroller')); } if ((bfHidden || bfCommentsVideoId || bfCommentDisabled) && ytdFlexyElm) { const commentsDataStatus = +commentsArea.getAttribute000('tyt-comments-data-status'); if (commentsDataStatus === 2) { ytdFlexyElm.setAttribute111('tyt-comment-disabled', ''); } else if (commentsDataStatus === 1) { ytdFlexyElm.removeAttribute000('tyt-comment-disabled'); } Promise.resolve(lockSet['checkCommentsShouldBeHiddenLock']) .then(eventMap['checkCommentsShouldBeHidden']) .catch(err => handlePromiseError(err, 'checkCommentsShouldBeHidden')); const lockId = lockSet['rightTabReadyLock01']; await rightTabsProvidedPromise.then(); if (lockGet['rightTabReadyLock01'] !== lockId) return; if (elements.comments !== commentsArea) return; if (commentsArea.isConnected === false) return; // console.log(7932, 'comments'); if (commentsArea.closest('#tab-comments')) { const shouldTabVisible = !commentsArea.closest('[hidden]'); document .querySelector('[tyt-tab-content="#tab-comments"]') .classList.toggle('tab-btn-hidden', !shouldTabVisible); } } }); const ioComment = new IntersectionObserver( entries => { for (const entry of entries) { const target = entry.target; const cnt = insp(target); if ( entry.isIntersecting && target instanceof HTMLElement_ && typeof cnt.calculateCanCollapse === 'function' ) { lockSet['removeKeepCommentsScrollerLock']; cnt.calculateCanCollapse(true); target.setAttribute111('io-intersected', ''); const ytdFlexyElm = elements.flexy; if (ytdFlexyElm && !ytdFlexyElm.hasAttribute000('keep-comments-scroller')) { ytdFlexyElm.setAttribute111('keep-comments-scroller', ''); } } else if (target.hasAttribute000('io-intersected')) { target.removeAttribute000('io-intersected'); } } }, { threshold: [0], rootMargin: '32px', // enlarging viewport for getting intersection earlier } ); let bFixForResizedTabLater = false; let lastRoRightTabsWidth = 0; const roRightTabs = new ResizeObserver(entries => { const entry = entries[entries.length - 1]; const width = Math.round(entry.borderBoxSize.inlineSize); if (lastRoRightTabsWidth !== width) { lastRoRightTabsWidth = width; if ((tabAStatus & 2) === 2) { bFixForResizedTabLater = false; Promise.resolve(1).then(eventMap['fixForTabDisplay']); } else { bFixForResizedTabLater = true; } } }); /** * Switch to specified tab * @param {string|Element} activeLink - Tab link selector or element */ const switchToTab = activeLink => { if (typeof activeLink === 'string') { activeLink = document.querySelector(`a[tyt-tab-content="${activeLink}"]`) || null; } const ytdFlexyElm = elements.flexy; const links = document.querySelectorAll('#material-tabs a[tyt-tab-content]'); for (const link of links) { const content = document.querySelector(link.getAttribute000('tyt-tab-content')); if (!link || !content) continue; const isActive = link === activeLink; link.classList.toggle('active', isActive); content.classList.toggle('tab-content-hidden', !isActive); if (isActive) { content.removeAttribute000('tyt-hidden'); } else if (!content.hasAttribute000('tyt-hidden')) { content.setAttribute111('tyt-hidden', ''); } } const switchingTo = activeLink ? activeLink.getAttribute000('tyt-tab-content') : ''; if (switchingTo) { lastTab = lastPanel = switchingTo; } if (ytdFlexyElm?.getAttribute000('tyt-chat') === '') { ytdFlexyElm.removeAttribute000('tyt-chat'); } ytdFlexyElm?.setAttribute111('tyt-tab', switchingTo); if (switchingTo) { bFixForResizedTabLater = false; Promise.resolve(0).then(eventMap['fixForTabDisplay']); } }; let tabAStatus = 0; /** * Calculate status flags based on element attributes * @param {number} r - Initial result value * @param {number} flag - Flags to check (bitwise) * @returns {number} Calculated status flags */ const calculationFn = (r = 0, flag) => { const ytdFlexyElm = elements.flexy; if (!ytdFlexyElm) return r; if (flag & 1) { r |= 1; if (!ytdFlexyElm.hasAttribute000('theater')) r -= 1; } if (flag & 2) { r |= 2; if (!ytdFlexyElm.getAttribute000('tyt-tab')) r -= 2; } if (flag & 4) { r |= 4; if (ytdFlexyElm.getAttribute000('tyt-chat') !== '-') r -= 4; } if (flag & 8) { r |= 8; if (ytdFlexyElm.getAttribute000('tyt-chat') !== '+') r -= 8; } if (flag & 16) { r |= 16; if (!ytdFlexyElm.hasAttribute000('is-two-columns_')) r -= 16; } if (flag & 32) { r |= 32; if (!ytdFlexyElm.hasAttribute000('tyt-egm-panel_')) r -= 32; } if (flag & 64) { r |= 64; if (!document.fullscreenElement) r -= 64; } if (flag & 128) { r |= 128; if (!ytdFlexyElm.hasAttribute000('tyt-playlist-expanded')) r -= 128; } return r; }; /** * Check if theater mode is active * @returns {boolean} True if theater mode is active */ function isTheater() { return Boolean(elements.flexy?.hasAttribute000('theater')); } /** * Get theater mode toggle button * @returns {HTMLButtonElement|null} Theater button or null */ function getTheaterButton() { return document.querySelector('ytd-watch-flexy #ytd-player button.ytp-size-button'); } /** * Enable theater mode * @internal Reserved for future use */ // eslint-disable-next-line no-unused-vars function ytBtnSetTheater() { if (!isTheater()) { getTheaterButton()?.click(); } } /** * Disable theater mode */ function ytBtnCancelTheater() { if (isTheater()) { getTheaterButton()?.click(); } } /** * Get element with most nested children (best match) * @param {string} selector - CSS selector * @returns {Element|null} Element with most children or null */ function getSuitableElement(selector) { const elements = document.querySelectorAll(selector); let bestIndex = -1; let maxDepth = -1; for (let i = 0; i < elements.length; i++) { const depth = elements[i].getElementsByTagName('*').length; if (depth > maxDepth) { maxDepth = depth; bestIndex = i; } } return bestIndex >= 0 ? elements[bestIndex] : null; } /** * Expand YouTube live chat */ function ytBtnExpandChat() { const dom = getSuitableElement('ytd-live-chat-frame#chat'); const cnt = insp(dom); if (cnt && typeof cnt.collapsed === 'boolean') { if (typeof cnt.setCollapsedState === 'function') { cnt.setCollapsedState({ setLiveChatCollapsedStateAction: { collapsed: false, }, }); if (cnt.collapsed === false) return; } cnt.collapsed = false; if (cnt.collapsed === false) return; if (cnt.isHiddenByUser === true && cnt.collapsed === true) { cnt.isHiddenByUser = false; cnt.collapsed = false; } } let button = document.querySelector( 'ytd-live-chat-frame#chat[collapsed] > .ytd-live-chat-frame#show-hide-button' ); if (button) { button = button.querySelector000('div.yt-spec-touch-feedback-shape') || button.querySelector000('ytd-toggle-button-renderer'); button?.click(); } } /** * Collapse YouTube live chat */ /** * Collapse YouTube live chat panel */ function ytBtnCollapseChat() { const dom = getSuitableElement('ytd-live-chat-frame#chat'); const cnt = insp(dom); if (cnt && typeof cnt.collapsed === 'boolean') { if (typeof cnt.setCollapsedState === 'function') { cnt.setCollapsedState({ setLiveChatCollapsedStateAction: { collapsed: true, }, }); if (cnt.collapsed === true) return; } cnt.collapsed = true; if (cnt.collapsed === true) return; if (cnt.isHiddenByUser === false && cnt.collapsed === false) { cnt.isHiddenByUser = true; cnt.collapsed = true; } } let button = document.querySelector( 'ytd-live-chat-frame#chat:not([collapsed]) > .ytd-live-chat-frame#show-hide-button' ); if (button) { button = button.querySelector000('div.yt-spec-touch-feedback-shape') || button.querySelector000('ytd-toggle-button-renderer'); button?.click(); } } /** * Control YouTube engagement panels (show/hide) * @param {Array|Object} arr - Array of panel actions or single action object */ function ytBtnEgmPanelCore(arr) { if (!arr) return; if (!('length' in arr)) arr = [arr]; const ytdFlexyElm = elements.flexy; if (!ytdFlexyElm) return; const actions = []; for (const entry of arr) { if (!entry) continue; const { panelId, toHide, toShow } = entry; if (toHide === true && !toShow) { actions.push({ changeEngagementPanelVisibilityAction: { targetId: panelId, visibility: 'ENGAGEMENT_PANEL_VISIBILITY_HIDDEN', }, }); } else if (toShow === true && !toHide) { actions.push({ showEngagementPanelEndpoint: { panelIdentifier: panelId, }, }); } } if (actions.length > 0) { const cnt = insp(ytdFlexyElm); cnt.resolveCommand( { signalServiceEndpoint: { signal: 'CLIENT_SIGNAL', actions: actions, }, }, {}, false ); } } /* function ytBtnCloseEngagementPanel( s) { //ePanel.setAttribute('visibility',"ENGAGEMENT_PANEL_VISIBILITY_HIDDEN"); let panelId = s.getAttribute('target-id') scriptletDeferred.debounce(() => { document.dispatchEvent(new CustomEvent('tyt-engagement-panel-visibility-change', { detail: { panelId, toHide: true } })) }) } /** * Close all expanded YouTube engagement panels */ function ytBtnCloseEngagementPanels() { const actions = []; for (const panelElm of document.querySelectorAll( `ytd-watch-flexy[tyt-tab] #panels.ytd-watch-flexy ytd-engagement-panel-section-list-renderer[target-id][visibility]:not([hidden])` )) { if ( panelElm.getAttribute('visibility') === 'ENGAGEMENT_PANEL_VISIBILITY_EXPANDED' && !panelElm.closest('[hidden]') ) { actions.push({ panelId: panelElm.getAttribute000('target-id'), toHide: true, }); } } ytBtnEgmPanelCore(actions); } /** * Open YouTube playlist panel */ function ytBtnOpenPlaylist() { const cnt = insp(elements.playlist); if (cnt && typeof cnt.collapsed === 'boolean') { cnt.collapsed = false; } } /** * Close YouTube playlist panel */ function ytBtnClosePlaylist() { const cnt = insp(elements.playlist); if (cnt && typeof cnt.collapsed === 'boolean') { cnt.collapsed = true; } } const updateChatLocation498 = function () { /* updateChatLocation: function() { if (this.is !== "ytd-watch-grid" && y("web_watch_theater_chat")) { var a = T(this.hostElement).querySelector("#chat-container") , b = this.theater && (!this.fullscreen || y("web_watch_fullscreen_panels")); this.watchWhileWindowSizeSufficient && this.liveChatPresentAndExpanded && b ? y("web_watch_theater_chat_beside_player") ? (b = T(this.hostElement).querySelector("#panels-full-bleed-container"), (a == null ? void 0 : a.parentElement) !== b && b.append(a), this.panelsBesidePlayer = !0) : y("web_watch_theater_fixed_chat") && (b = T(this.hostElement).querySelector("#columns"), (a == null ? void 0 : a.parentElement) !== b && b.append(a), this.fixedPanels = !0) : (y("web_watch_theater_chat_beside_player") ? this.panelsBesidePlayer = !1 : y("web_watch_theater_fixed_chat") && (this.fixedPanels = !1), b = T(this.hostElement).querySelector("#playlist"), a && b ? Fh(a, b) : Gm(new zk("Missing element when updating chat location",{ "chatContainer defined": !!a, "playlist defined": !!b }))); this.updatePageMediaQueries(); this.schedulePlayerSizeUpdate_() } }, */ // console.log('updateChatLocation498') if (this.is !== 'ytd-watch-grid') { this.updatePageMediaQueries(); this.schedulePlayerSizeUpdate_(); } }; const mirrorNodeWS = new WeakMap(); /* const infoFix = () => { const infoExpander = elements.infoExpander; const ytdFlexyElm = elements.flexy; if (!infoExpander || !ytdFlexyElm) return; console.log(386, infoExpander, infoExpander.matches('#tab-info > [class]')) if (!infoExpander.matches('#tab-info > [class]')) return; // const elms = [...document.querySelectorAll('ytd-watch-metadata.ytd-watch-flexy div[slot="extra-content"], ytd-watch-metadata.ytd-watch-flexy ytd-metadata-row-container-renderer')].filter(elm=>{ // if(elm.parentNode.closest('div[slot="extra-content"], ytd-metadata-row-container-renderer')) return false; // return true; // }); const requireElements = [...document.querySelectorAll('ytd-watch-metadata.ytd-watch-flexy div[slot="extra-content"] > *, ytd-watch-metadata.ytd-watch-flexy #extra-content > *')].filter(elm => { return typeof elm.is == 'string' }).map(elm => { const is = elm.is; while (elm instanceof HTMLElement_) { const q = [...elm.querySelectorAll(is)].filter(e => insp(e).data); if (q.length >= 1) return q[0]; elm = elm.parentNode; } }).filter(elm => !!elm && typeof elm.is === 'string'); console.log(requireElements) const source = requireElements.map(entry=>({ data: insp(entry).data, tag: insp(entry).is, elm: entry })) if (!document.querySelector('noscript#aythl')) { const noscript = document.createElement('noscript') noscript.id = 'aythl'; ytdFlexyElm.insertBefore000(noscript, ytdFlexyElm.firstChild); } const noscript = document.querySelector('noscript#aythl'); const clones = new Set(); for (const {data, tag, elm} of source) { // const cloneNode = document.createElement(tag); let cloneNode = elm.cloneNode(true); // noscript.appendChild(cloneNode); // insp(cloneNode).data = null; insp(cloneNode).data = data; source.clone = cloneNode; clones.add(cloneNode); } // const elms = [...document.querySelectorAll('ytd-watch-metadata.ytd-watch-flexy div[slot="extra-content"]')].filter(elm => { // if (elm.parentNode.closest('div[slot="extra-content"], ytd-metadata-row-container-renderer')) return false; // return true; // }); // let arr = []; // for(const elm of elms){ // if(elm.hasAttribute('slot')) arr.push(...elm.childNodes); // else arr.push(elm); // } // arr = arr.filter(e=>e && e.nodeType === 1); // console.log(386,arr) // const clones = arr.map(e=>e.cloneNode(true)); // for(let node = infoExpander.nextSibling; node instanceof Node; node = node.nextSibling) node.remove(); // infoExpander.parentNode.assignChildren111(null, infoExpander, [...clones]); let removal = []; for(let node = infoExpander.nextSibling; node instanceof Node; node = node.nextSibling)removal.push(node); for(const node of removal) node.remove(); for(const node of clones) infoExpander.parentNode.appendChild(node); for (const {data, tag, elm, clone} of source) { insp(clone).data = null; insp(clone).data = data; } // console.log(infoExpander.parentNode.childNodes) } */ const dummyNode = document.createElement('noscript'); // const __j4838__ = Symbol(); const __j4836__ = Symbol(); const __j5744__ = Symbol(); // original element const __j5733__ = Symbol(); // __lastChanged__ const monitorDataChangedByDOMMutation = async function (_mutations) { const nodeWR = this; const node = kRef(nodeWR); if (!node) return; const cnt = insp(node); const __lastChanged__ = cnt[__j5733__]; const val = cnt.data ? cnt.data[__j4836__] || 1 : 0; if (__lastChanged__ !== val) { cnt[__j5733__] = val > 0 ? (cnt.data[__j4836__] = Date.now()) : 0; await Promise.resolve(); // required for making sufficient delay for data rendering attributeInc(node, 'tyt-data-change-counter'); // next macro task } }; const moChangeReflection = function (mutations) { const nodeWR = this; const node = kRef(nodeWR); if (!node) return; const originElement = kRef(node[__j5744__] || null) || null; if (!originElement) return; const cnt = insp(node); const oriCnt = insp(originElement); if (mutations) { let bfDataChangeCounter = false; for (const mutation of mutations) { if ( mutation.attributeName === 'tyt-clone-refresh-count' && mutation.target === originElement ) { bfDataChangeCounter = true; } else if ( mutation.attributeName === 'tyt-data-change-counter' && mutation.target === originElement ) { bfDataChangeCounter = true; } } if (bfDataChangeCounter && oriCnt.data) { node.replaceWith(dummyNode); cnt.data = Object.assign({}, oriCnt.data); dummyNode.replaceWith(node); } } }; /* const moChangeReflection = async function (mutations) { const nodeWR = this; const node = kRef(nodeWR); if (!node) return; const originElement = kRef(node[__j5744__] || null) || null; if (!originElement) return; const cnt = insp(node); const oriCnt = insp(originElement); if(mutations){ let bfDataChangeCounter = false; for (const mutation of mutations) { if (mutation.attributeName === 'tyt-data-change-counter' && mutation.target === originElement) { bfDataChangeCounter = true; } } if(bfDataChangeCounter && oriCnt.data){ node.replaceWith(dummyNode); cnt.data = Object.assign({}, oriCnt.data); dummyNode.replaceWith(node); } } // console.log(8348, originElement) if (cnt.isAttached === false) { // do nothing // don't call infoFix() as it shall be only called in ytd-expander::attached and yt-navigate-finish } else if (oriCnt.isAttached === false && cnt.isAttached === true) { if (node.isConnected && node.parentNode instanceof HTMLElement_) { node.parentNode.removeChild(node); } else { node.remove(); } if (oriCnt.data !== null) { cnt.data = null; } } else if (oriCnt.isAttached === true && cnt.isAttached === true) { if (!oriCnt.data) { if(cnt.data){ cnt.data = null; } } else if (!cnt.data || oriCnt.data[__j4838__] !== cnt.data[__j4838__]) { oriCnt.data[__j4838__] = Date.now(); await Promise.resolve(); // required for making sufficient delay for data rendering attributeInc(originElement, 'tyt-data-change-counter'); // next macro task } } }; */ /** * Increment attribute value with overflow protection * @param {Element} elm - Element to modify * @param {string} prop - Attribute name * @returns {number} New attribute value */ const attributeInc = (elm, prop) => { let v = (+elm.getAttribute000(prop) || 0) + 1; if (v > MAX_ATTRIBUTE_VALUE) v = ATTRIBUTE_RESET_VALUE; elm.setAttribute000(prop, v); return v; }; /** * Validates if a string is a valid YouTube channel ID * Format: UC[-_a-zA-Z0-9+=.]{22} * @see https://support.google.com/youtube/answer/6070344?hl=en * @param {string} x - The string to validate * @returns {boolean} True if valid channel ID */ const isChannelId = x => { return typeof x === 'string' && x.length === 24 && /^UC[-_a-zA-Z0-9+=.]{22}$/.test(x); }; /** * Fix and organize info panel layout * @param {number|null} lockId - Lock identifier for concurrent execution control */ const infoFix = lockId => { if (lockId !== null && lockGet['infoFixLock'] !== lockId) return; const infoExpander = elements.infoExpander; const infoContainer = (infoExpander ? infoExpander.parentNode : null) || document.querySelector('#tab-info'); const ytdFlexyElm = elements.flexy; if (!infoContainer || !ytdFlexyElm) return; if (infoExpander) { const match = infoExpander.matches('#tab-info > [class]') || infoExpander.matches('#tab-info > [tyt-main-info]'); if (!match) return; } const requireElements = [ ...document.querySelectorAll( 'ytd-watch-metadata.ytd-watch-flexy div[slot="extra-content"] > *, ytd-watch-metadata.ytd-watch-flexy #extra-content > *' ), ] .filter(elm => { return typeof elm.is == 'string'; }) .map(elm => { const is = elm.is; while (elm instanceof HTMLElement_) { const q = [...elm.querySelectorAll(is)].filter(e => insp(e).data); if (q.length >= 1) return q[0]; elm = elm.parentNode; } }) .filter(elm => !!elm && typeof elm.is === 'string'); const source = requireElements.map(entry => { const inst = insp(entry); return { data: inst.data, tag: inst.is, elm: entry, }; }); let noscript_ = document.querySelector('noscript#aythl'); if (!noscript_) { noscript_ = document.createElement('noscript'); noscript_.id = 'aythl'; inPageRearrange = true; ytdFlexyElm.insertBefore000(noscript_, ytdFlexyElm.firstChild); inPageRearrange = false; } const noscript = noscript_; let requiredUpdate = false; const mirrorElmSet = new Set(); const targetParent = infoContainer; for (const { data, tag: tag, elm: s } of source) { let mirrorNode = mirrorNodeWS.get(s); mirrorNode = mirrorNode ? kRef(mirrorNode) : mirrorNode; if (!mirrorNode) { const cnt = insp(s); const cProto = cnt.constructor.prototype; const element = document.createElement(tag); noscript.appendChild(element); mirrorNode = element; mirrorNode[__j5744__] = mWeakRef(s); const nodeWR = mWeakRef(mirrorNode); new MutationObserver(moChangeReflection.bind(nodeWR)).observe(s, { attributes: true, attributeFilter: ['tyt-clone-refresh-count', 'tyt-data-change-counter'], }); s.jy8432 = 1; if ( !(cProto instanceof Node) && !cProto._dataChanged496 && typeof cProto._createPropertyObserver === 'function' ) { cProto._dataChanged496 = function () { const cnt = this; const node = cnt.hostElement || cnt; if (node.jy8432) { attributeInc(node, 'tyt-data-change-counter'); } }; cProto._createPropertyObserver('data', '_dataChanged496', undefined); } else if ( !(cProto instanceof Node) && !cProto._dataChanged496 && cProto.useSignals === true && insp(s).signalProxy ) { const dataSignal = cnt?.signalProxy?.signalCache?.data; if ( dataSignal && typeof dataSignal.setWithPath === 'function' && !dataSignal.setWithPath573 && !dataSignal.controller573 ) { dataSignal.controller573 = mWeakRef(cnt); dataSignal.setWithPath573 = dataSignal.setWithPath; dataSignal.setWithPath = function () { const cnt = kRef(this.controller573 || null) || null; cnt && typeof cnt._dataChanged496k === 'function' && Promise.resolve(cnt) .then(cnt._dataChanged496k) .catch(err => handlePromiseError(err, 'setWithPath_dataChanged496k')); return this.setWithPath573(...arguments); }; cProto._dataChanged496 = function () { const cnt = this; const node = cnt.hostElement || cnt; if (node.jy8432) { attributeInc(node, 'tyt-data-change-counter'); } }; cProto._dataChanged496k = cnt => cnt._dataChanged496(); } } if (!cProto._dataChanged496) { new MutationObserver( monitorDataChangedByDOMMutation.bind(mirrorNode[__j5744__]) ).observe(s, { attributes: true, childList: true, subtree: true }); } mirrorNodeWS.set(s, nodeWR); requiredUpdate = true; } else { if (mirrorNode.parentNode !== targetParent) { requiredUpdate = true; } } if (!requiredUpdate) { const cloneNodeCnt = insp(mirrorNode); if (cloneNodeCnt.data !== data) { // if(mirrorNode.parentNode !== noscript){ // noscript.appendChild(mirrorNode); // } // mirrorNode.replaceWith(dummyNode); // cloneNodeCnt.data = data; // dummyNode.replaceWith(mirrorNode); requiredUpdate = true; } } mirrorElmSet.add(mirrorNode); source.mirrored = mirrorNode; } const mirroElmArr = [...mirrorElmSet]; mirrorElmSet.clear(); if (!requiredUpdate) { let e = infoExpander ? -1 : 0; // DOM Tree Check for (let n = targetParent.firstChild; n instanceof Node; n = n.nextSibling) { const target = e < 0 ? infoExpander : mirroElmArr[e]; e++; if (n !== target) { // target can be undefined if index overflow requiredUpdate = true; break; } } if (!requiredUpdate && e !== mirroElmArr.length + 1) requiredUpdate = true; } if (requiredUpdate) { if (infoExpander) { targetParent.assignChildren111(null, infoExpander, mirroElmArr); } else { targetParent.replaceChildren000(...mirroElmArr); } for (const mirrorElm of mirroElmArr) { // trigger data assignment and record refresh count by manual update const j = attributeInc(mirrorElm, 'tyt-clone-refresh-count'); const oriElm = kRef(mirrorElm[__j5744__] || null) || null; if (oriElm) { oriElm.setAttribute111('tyt-clone-refresh-count', j); } } } mirroElmArr.length = 0; source.length = 0; }; /** * Fix and optimize secondary layout structure * @param {number} lockId - Lock identifier for concurrent execution control */ const layoutFix = lockId => { if (lockGet['layoutFixLock'] !== lockId) return; // console.log('((layoutFix))') const secondaryWrapper = document.querySelector( '#secondary-inner.style-scope.ytd-watch-flexy > secondary-wrapper' ); // console.log(3838, !!chatContainer, !!(secondaryWrapper && secondaryInner), secondaryInner?.firstChild, secondaryInner?.lastChild , secondaryWrapper?.parentNode === secondaryInner) if (secondaryWrapper) { const secondaryInner = secondaryWrapper.parentNode; const chatContainer = document.querySelector( '#columns.style-scope.ytd-watch-flexy [tyt-chat-container]' ); if ( secondaryInner.firstChild !== secondaryInner.lastChild || (chatContainer && !chatContainer.closest('secondary-wrapper')) ) { // console.log(38381) const w = []; const w2 = []; for ( let node = secondaryInner.firstChild; node instanceof Node; node = node.nextSibling ) { if (node === chatContainer && chatContainer) { } else if (node === secondaryWrapper) { for ( let node2 = secondaryWrapper.firstChild; node2 instanceof Node; node2 = node2.nextSibling ) { if (node2 === chatContainer && chatContainer) { } else { if (node2.id === 'right-tabs' && chatContainer) { w2.push(chatContainer); } w2.push(node2); } } } else { w.push(node); } } // console.log('qww', w, w2) inPageRearrange = true; secondaryWrapper.replaceChildren000(...w, ...w2); inPageRearrange = false; const chatElm = elements.chat; const chatCnt = insp(chatElm); if ( chatCnt && typeof chatCnt.urlChanged === 'function' && secondaryWrapper.contains(chatElm) ) { // setTimeout(() => chatCnt.urlChanged, 136); if (typeof chatCnt.urlChangedAsync12 === 'function') { DEBUG_5085 && console.log('elements.chat urlChangedAsync12', 61); chatCnt.urlChanged(); } else { DEBUG_5085 && console.log('elements.chat urlChangedAsync12', 62); setTimeout(() => chatCnt.urlChanged(), 136); } } } } }; let lastPanel = ''; let lastTab = ''; // let fixInitialTabState = 0; const aoEgmPanels = new MutationObserver(() => { // console.log(5094,3); Promise.resolve(lockSet['updateEgmPanelsLock']) .then(updateEgmPanels) .catch(err => handlePromiseError(err, 'aoEgmPanels_updateEgmPanels')); }); const removeKeepCommentsScroller = async lockId => { if (lockGet['removeKeepCommentsScrollerLock'] !== lockId) return; await Promise.resolve(); if (lockGet['removeKeepCommentsScrollerLock'] !== lockId) return; const ytdFlexyFlm = elements.flexy; if (ytdFlexyFlm) { ytdFlexyFlm.removeAttribute000('keep-comments-scroller'); } }; const updateEgmPanels = async lockId => { if (lockId !== lockGet['updateEgmPanelsLock']) return; await navigateFinishedPromise.then().catch(console.warn); if (lockId !== lockGet['updateEgmPanelsLock']) return; // console.log('updateEgmPanels::called'); const ytdFlexyElm = elements.flexy; if (!ytdFlexyElm) return; let newVisiblePanels = []; let newHiddenPanels = []; let allVisiblePanels = []; for (const panelElm of document.querySelectorAll('[tyt-egm-panel][target-id][visibility]')) { const visibility = panelElm.getAttribute000('visibility'); if (visibility === 'ENGAGEMENT_PANEL_VISIBILITY_HIDDEN' || panelElm.closest('[hidden]')) { if (panelElm.hasAttribute000('tyt-visible-at')) { panelElm.removeAttribute000('tyt-visible-at'); newHiddenPanels.push(panelElm); } } else if ( visibility === 'ENGAGEMENT_PANEL_VISIBILITY_EXPANDED' && !panelElm.closest('[hidden]') ) { const visibleAt = panelElm.getAttribute000('tyt-visible-at'); if (!visibleAt) { panelElm.setAttribute111('tyt-visible-at', Date.now()); newVisiblePanels.push(panelElm); } allVisiblePanels.push(panelElm); } } if (newVisiblePanels.length >= 1 && allVisiblePanels.length >= 2) { const targetVisible = newVisiblePanels[newVisiblePanels.length - 1]; const actions = []; for (const panelElm of allVisiblePanels) { if (panelElm === targetVisible) continue; actions.push({ panelId: panelElm.getAttribute000('target-id'), toHide: true, }); } if (actions.length >= 1) { ytBtnEgmPanelCore(actions); } } if (allVisiblePanels.length >= 1) { ytdFlexyElm.setAttribute111('tyt-egm-panel_', ''); } else { ytdFlexyElm.removeAttribute000('tyt-egm-panel_'); } newVisiblePanels.length = 0; newVisiblePanels = null; newHiddenPanels.length = 0; newHiddenPanels = null; allVisiblePanels.length = 0; allVisiblePanels = null; }; const checkElementExist = (css, exclude) => { for (const p of document.querySelectorAll(css)) { if (!p.closest(exclude)) return p; } return null; }; let fixInitialTabStateK = 0; const { handleNavigateFactory } = (() => { let isLoadStartListened = false; function findLcComment(lc) { if (arguments.length === 1) { const element = document.querySelector( `#tab-comments ytd-comments ytd-comment-renderer #header-author a[href*="lc=${lc}"]` ); if (element) { const commentRendererElm = closestFromAnchor.call(element, 'ytd-comment-renderer'); if (commentRendererElm && lc) { return { lc, commentRendererElm, }; } } } else if (arguments.length === 0) { const element = document.querySelector( `#tab-comments ytd-comments ytd-comment-renderer > #linked-comment-badge span:not(:empty)` ); if (element) { const commentRendererElm = closestFromAnchor.call(element, 'ytd-comment-renderer'); if (commentRendererElm) { const header = _querySelector.call(commentRendererElm, '#header-author'); if (header) { const anchor = _querySelector.call(header, 'a[href*="lc="]'); if (anchor) { const href = anchor.getAttribute('href') || ''; const m = /[&?]lc=([\w_.-]+)/.exec(href); // dot = sub-comment if (m) { lc = m[1]; } } } } if (commentRendererElm && lc) { return { lc, commentRendererElm, }; } } } return null; } function lcSwapFuncA(targetLcId, currentLcId) { let done = 0; try { // console.log(currentLcId, targetLcId) const r1 = findLcComment(currentLcId).commentRendererElm; const r2 = findLcComment(targetLcId).commentRendererElm; if ( typeof insp(r1).data.linkedCommentBadge === 'object' && typeof insp(r2).data.linkedCommentBadge === 'undefined' ) { const p = Object.assign({}, insp(r1).data.linkedCommentBadge); if (((p || 0).metadataBadgeRenderer || 0).trackingParams) { delete p.metadataBadgeRenderer.trackingParams; } const v1 = findContentsRenderer(r1); const v2 = findContentsRenderer(r2); if ( v1.parent === v2.parent && (v2.parent.nodeName === 'YTD-COMMENTS' || v2.parent.nodeName === 'YTD-ITEM-SECTION-RENDERER') ) { } else { // currently not supported return false; } if (v2.index >= 0) { if (v2.parent.nodeName === 'YTD-COMMENT-REPLIES-RENDERER') { if (lcSwapFuncB(targetLcId, currentLcId, p)) { done = 1; } done = 1; } else { const v2pCnt = insp(v2.parent); const v2Conents = (v2pCnt.data || 0).contents || 0; if (!v2Conents) console.warn('v2Conents is not found'); v2pCnt.data = Object.assign({}, v2pCnt.data, { contents: [].concat( [v2Conents[v2.index]], v2Conents.slice(0, v2.index), v2Conents.slice(v2.index + 1) ), }); if (lcSwapFuncB(targetLcId, currentLcId, p)) { done = 1; } } } } } catch (e) { console.warn(e); } return done === 1; } function lcSwapFuncB(targetLcId, currentLcId, _p) { let done = 0; try { const r1 = findLcComment(currentLcId).commentRendererElm; const r1cnt = insp(r1); const r2 = findLcComment(targetLcId).commentRendererElm; const r2cnt = insp(r2); const r1d = r1cnt.data; const p = Object.assign({}, _p); r1d.linkedCommentBadge = null; delete r1d.linkedCommentBadge; const q = Object.assign({}, r1d); q.linkedCommentBadge = null; delete q.linkedCommentBadge; r1cnt.data = Object.assign({}, q); r2cnt.data = Object.assign({}, r2cnt.data, { linkedCommentBadge: p }); done = 1; } catch (e) { console.warn(e); } return done === 1; } const loadStartFx = async evt => { const media = (evt || 0).target || 0; if (media.nodeName === 'VIDEO' || media.nodeName === 'AUDIO') { } else return; const newMedia = media; const media1 = common.getMediaElement(0); // document.querySelector('#movie_player video[src]'); const media2 = common.getMediaElements(2); // document.querySelectorAll('ytd-browse[role="main"] video[src]'); if (media1 !== null && media2.length > 0) { if (newMedia !== media1 && media1.paused === false) { if (isVideoPlaying(media1)) { Promise.resolve(newMedia) .then(video => video.paused === false && video.pause()) .catch(console.warn); } } else if (newMedia === media1) { for (const s of media2) { if (s.paused === false) { Promise.resolve(s) .then(s => s.paused === false && s.pause()) .catch(console.warn); break; } } } else { Promise.resolve(media1) .then(video1 => video1.paused === false && video1.pause()) .catch(console.warn); } } }; const getBrowsableEndPoint = req => { let valid = false; let endpoint = req ? req.command : null; if ( endpoint && (endpoint.commandMetadata || 0).webCommandMetadata && endpoint.watchEndpoint ) { const videoId = endpoint.watchEndpoint.videoId; const url = endpoint.commandMetadata.webCommandMetadata.url; if (typeof videoId === 'string' && typeof url === 'string' && url.indexOf('lc=') > 0) { const m = /^\/watch\?v=([\w_-]+)&lc=([\w_.-]+)$/.exec(url); // dot = sub-comment if (m && m[1] === videoId) { /* { "style": "BADGE_STYLE_TYPE_SIMPLE", "label": "注目のコメント", "trackingParams": "XXXXXX" } */ const targetLc = findLcComment(m[2]); const currentLc = targetLc ? findLcComment() : null; if (targetLc && currentLc) { const done = targetLc.lc === currentLc.lc ? 1 : lcSwapFuncA(targetLc.lc, currentLc.lc) ? 1 : 0; if (done === 1) { common.xReplaceState(history.state, url); return; } } } } } /* { "type": 0, "command": endpoint, "form": { "tempData": {}, "reload": false } } */ if ( endpoint && (endpoint.commandMetadata || 0).webCommandMetadata && endpoint.browseEndpoint && isChannelId(endpoint.browseEndpoint.browseId) ) { valid = true; } else if ( endpoint && (endpoint.browseEndpoint || endpoint.searchEndpoint) && !endpoint.urlEndpoint && !endpoint.watchEndpoint ) { if (endpoint.browseEndpoint && endpoint.browseEndpoint.browseId === 'FEwhat_to_watch') { // valid = false; const playerMedia = common.getMediaElement(1); if (playerMedia && playerMedia.paused === false) valid = true; // home page } else if (endpoint.commandMetadata && endpoint.commandMetadata.webCommandMetadata) { const meta = endpoint.commandMetadata.webCommandMetadata; if (meta && /*meta.apiUrl &&*/ meta.url && meta.webPageType) { valid = true; } } } if (!valid) endpoint = null; return endpoint; }; const shouldUseMiniPlayer = () => { const isSubTypeExist = document.querySelector( 'ytd-page-manager#page-manager > ytd-browse[page-subtype]' ); if (isSubTypeExist) return true; const movie_player = [...document.querySelectorAll('#movie_player')].filter( e => !e.closest('[hidden]') )[0]; if (movie_player) { const media = qsOne(movie_player, 'video[class], audio[class]'); if ( media && media.currentTime > 3 && media.duration - media.currentTime > 3 && media.paused === false ) { return true; } } return false; // return true; // return !!document.querySelector('ytd-page-manager#page-manager > ytd-browse[page-subtype]'); }; const conditionFulfillment = req => { const endpoint = req ? req.command : null; if (!endpoint) return; if ( endpoint && (endpoint.commandMetadata || 0).webCommandMetadata && endpoint.watchEndpoint ) { } else if ( endpoint && (endpoint.commandMetadata || 0).webCommandMetadata && endpoint.browseEndpoint && isChannelId(endpoint.browseEndpoint.browseId) ) { } else if ( endpoint && (endpoint.browseEndpoint || endpoint.searchEndpoint) && !endpoint.urlEndpoint && !endpoint.watchEndpoint ) { } else { return false; } if (!shouldUseMiniPlayer()) return false; /* // user would like to switch page immediately without playing the video; // attribute appear after playing video for more than 2s if (!document.head.dataset.viTime) return false; else { let currentVideo = common.getMediaElement(0); if (currentVideo && currentVideo.readyState > currentVideo.HAVE_CURRENT_DATA && currentVideo.currentTime > 2.2 && currentVideo.duration - 2.2 < currentVideo.currentTime) { // disable miniview browsing if the media is near to the end return false; } } */ if (pageType !== 'watch') return false; if ( !checkElementExist( 'ytd-watch-flexy #player button.ytp-miniplayer-button.ytp-button', '[hidden]' ) ) { return false; } return true; }; let u38 = 0; const fixChannelAboutPopup = async t38 => { let promise = new PromiseExternal(); const f = () => { promise && promise.resolve(); promise = null; }; document.addEventListener('yt-navigate-finish', f, false); await promise.then(); promise = null; document.removeEventListener('yt-navigate-finish', f, false); if (t38 !== u38) return; setTimeout(() => { const currentAbout = [...document.querySelectorAll('ytd-about-channel-renderer')].filter( e => !e.closest('[hidden]') )[0]; let okay = false; if (!currentAbout) okay = true; else { const popupContainer = currentAbout.closest('ytd-popup-container'); if (popupContainer) { const cnt = insp(popupContainer); let arr = null; try { arr = cnt.handleGetOpenedPopupsAction_(); } catch { } if (arr && arr.length === 0) okay = true; } else { okay = false; } } if (okay) { const descriptionModel = [ ...document.querySelectorAll('yt-description-preview-view-model'), ].filter(e => !e.closest('[hidden]'))[0]; if (descriptionModel) { const button = [...descriptionModel.querySelectorAll('button')].filter( e => !e.closest('[hidden]') && `${e.textContent}`.trim().length > 0 )[0]; if (button) { button.click(); } } } }, 80); }; const handleNavigateFactory = handleNavigate => { return function (req) { if (u38 > MAX_ATTRIBUTE_VALUE) u38 = ATTRIBUTE_RESET_VALUE; const t38 = ++u38; const $this = this; const $arguments = arguments; let endpoint = null; if (conditionFulfillment(req)) { endpoint = getBrowsableEndPoint(req); } if (!endpoint || !shouldUseMiniPlayer()) return handleNavigate.apply($this, $arguments); // console.log('tabview-script-handleNavigate') const ytdAppElm = document.querySelector('ytd-app'); const ytdAppCnt = insp(ytdAppElm); let object = null; try { object = ytdAppCnt.data.response.currentVideoEndpoint.watchEndpoint || null; } catch { object = null; } if (typeof object !== 'object') object = null; const once = { once: true }; // browsers supporting async function can also use once option. if (object !== null && !('playlistId' in object)) { let wObject = mWeakRef(object); const N = 3; let count = 0; /* rcb(b) => a = playlistId = undefinded var scb = function(a, b, c, d) { a.isInitialized() && (B("kevlar_miniplayer_navigate_to_shorts_killswitch") ? c || d ? ("watch" !== Xu(b) && "shorts" !== Xu(b) && os(a.miniplayerEl, "yt-cache-miniplayer-page-action", [b]), qs(a.miniplayerEl, "yt-deactivate-miniplayer-action")) : "watch" === Xu(b) && rcb(b) && (qt.getInstance().playlistWatchPageActivation = !0, a.activateMiniplayer(b)) : c ? ("watch" !== Xu(b) && os(a.miniplayerEl, "yt-cache-miniplayer-page-action", [b]), qs(a.miniplayerEl, "yt-deactivate-miniplayer-action")) : d ? qs(a.miniplayerEl, "yt-pause-miniplayer-action") : "watch" === Xu(b) && rcb(b) && (qt.getInstance().playlistWatchPageActivation = !0, a.activateMiniplayer(b))) }; */ Object.defineProperty(kRef(wObject) || {}, 'playlistId', { get() { count++; if (count === N) { delete this.playlistId; } return '*'; }, set(value) { delete this.playlistId; // remove property definition this.playlistId = value; // assign as normal property }, enumerable: false, configurable: true, }); let playlistClearout = null; let timeoutid = 0; Promise.race([ new Promise(r => { timeoutid = setTimeout(r, 4000); }), new Promise(r => { playlistClearout = () => { if (timeoutid > 0) { clearTimeout(timeoutid); timeoutid = 0; } r(); }; document.addEventListener('yt-page-type-changed', playlistClearout, once); }), ]) .then(() => { if (timeoutid !== 0) { playlistClearout && document.removeEventListener('yt-page-type-changed', playlistClearout, once); timeoutid = 0; } playlistClearout = null; count = N - 1; const object = kRef(wObject); wObject = null; return object ? object.playlistId : null; }) .catch(console.warn); } if (!isLoadStartListened) { isLoadStartListened = true; document.addEventListener('loadstart', loadStartFx, true); } const endpointURL = `${endpoint?.commandMetadata?.webCommandMetadata?.url || ''}`; if ( endpointURL && endpointURL.endsWith('/about') && /\/channel\/UC[-_a-zA-Z0-9+=.]{22}\/about/.test(endpointURL) ) { fixChannelAboutPopup(t38); } handleNavigate.apply($this, $arguments); }; }; return { handleNavigateFactory }; })(); const common = (() => { let mediaModeLock = 0; const _getMediaElement = i => { if (mediaModeLock === 0) { const e = document.querySelector('.video-stream.html5-main-video') || document.querySelector('#movie_player video, #movie_player audio') || document.querySelector('body video[src], body audio[src]'); if (e) { if (e.nodeName === 'VIDEO') mediaModeLock = 1; else if (e.nodeName === 'AUDIO') mediaModeLock = 2; } } if (!mediaModeLock) return null; if (mediaModeLock === 1) { switch (i) { case 1: return 'ytd-player#ytd-player video[src]'; case 2: return 'ytd-browse[role="main"] video[src]'; case 0: default: return '#movie_player video[src]'; } } else if (mediaModeLock === 2) { switch (i) { case 1: return 'ytd-player#ytd-player audio.video-stream.html5-main-video[src]'; case 2: return 'ytd-browse[role="main"] audio.video-stream.html5-main-video[src]'; case 0: default: return '#movie_player audio.video-stream.html5-main-video[src]'; } } return null; }; return { xReplaceState(s, u) { try { history.replaceState(s, '', u); } catch { // in case error occurs if replaceState is replaced by any external script / extension } if (s.endpoint) { try { const ytdAppElm = document.querySelector('ytd-app'); const ytdAppCnt = insp(ytdAppElm); ytdAppCnt.replaceState(s.endpoint, '', u); } catch { } } }, getMediaElement(i) { const s = _getMediaElement(i) || ''; if (s) return document.querySelector(s); return null; }, getMediaElements(i) { const s = _getMediaElement(i) || ''; if (s) return document.querySelectorAll(s); return []; }, }; })(); let inPageRearrange = false; let tmpLastVideoId = ''; // const nsMap = new Map(); const getCurrentVideoId = () => { const ytdFlexyElm = elements.flexy; const ytdFlexyCnt = insp(ytdFlexyElm); if (ytdFlexyCnt && typeof ytdFlexyCnt.videoId === 'string') return ytdFlexyCnt.videoId; if (ytdFlexyElm && typeof ytdFlexyElm.videoId === 'string') return ytdFlexyElm.videoId; console.log('video id not found'); return ''; }; // eslint-disable-next-line no-unused-vars const holdInlineExpanderAlwaysExpanded = inlineExpanderCnt => { console.log('holdInlineExpanderAlwaysExpanded'); if (inlineExpanderCnt.alwaysShowExpandButton === true) { inlineExpanderCnt.alwaysShowExpandButton = false; } if (typeof (inlineExpanderCnt.collapseLabel || 0) === 'string') { inlineExpanderCnt.collapseLabel = ''; } if (typeof (inlineExpanderCnt.expandLabel || 0) === 'string') { inlineExpanderCnt.expandLabel = ''; } if (inlineExpanderCnt.showCollapseButton === true) { inlineExpanderCnt.showCollapseButton = false; } if (inlineExpanderCnt.showExpandButton === true) inlineExpanderCnt.showExpandButton = false; if (inlineExpanderCnt.expandButton instanceof HTMLElement_) { inlineExpanderCnt.expandButton = null; inlineExpanderCnt.expandButton.remove(); } }; const fixInlineExpanderDisplay = inlineExpanderCnt => { try { inlineExpanderCnt.updateIsAttributedExpanded(); } catch (e) { console.warn('[YouTube+] updateIsAttributedExpanded failed:', e); } try { inlineExpanderCnt.updateIsFormattedExpanded(); } catch (e) { console.warn('[YouTube+] updateIsFormattedExpanded failed:', e); } try { inlineExpanderCnt.updateTextOnSnippetTypeChange(); } catch (e) { console.warn('[YouTube+] updateTextOnSnippetTypeChange failed:', e); } try { inlineExpanderCnt.updateStyles(); } catch (e) { console.warn('[YouTube+] updateStyles failed:', e); } }; const fixInlineExpanderMethods = inlineExpanderCnt => { if (inlineExpanderCnt && !inlineExpanderCnt.__$$idncjk8487$$__) { inlineExpanderCnt.__$$idncjk8487$$__ = true; inlineExpanderCnt.updateTextOnSnippetTypeChange = function () { true || (this.isResetMutation && this.mutationCallback()); }; // inlineExpanderCnt.hasAttributedStringText = true; inlineExpanderCnt.isResetMutation = true; fixInlineExpanderDisplay(inlineExpanderCnt); // do the initial fix } }; const fixInlineExpanderContent = () => { // console.log(21886,1) const mainInfo = getMainInfo(); if (!mainInfo) return; // console.log(21886,2) const inlineExpanderElm = mainInfo.querySelector('ytd-text-inline-expander'); const inlineExpanderCnt = insp(inlineExpanderElm); fixInlineExpanderMethods(inlineExpanderCnt); // console.log(21886, 3) // if (inlineExpanderCnt && inlineExpanderCnt.isExpanded === true && plugin.autoExpandInfoDesc.activated) { // // inlineExpanderCnt.isExpandedChanged(); // // holdInlineExpanderAlwaysExpanded(inlineExpanderCnt); // } // if(inlineExpanderCnt){ // // console.log(21886,4, inlineExpanderCnt.isExpanded, inlineExpanderCnt.isTruncated) // if (inlineExpanderCnt.isExpanded === false && inlineExpanderCnt.isTruncated === true) { // // console.log(21881) // inlineExpanderCnt.isTruncated = false; // } // } }; const plugin = { minibrowser: { activated: false, toUse: true, // depends on shouldUseMiniPlayer() activate() { if (this.activated) return; // Use global isPassiveArgSupport constant // https://caniuse.com/?search=observer // https://caniuse.com/?search=addEventListener%20passive if (!isPassiveArgSupport) return; this.activated = true; const ytdAppElm = document.querySelector('ytd-app'); const ytdAppCnt = insp(ytdAppElm); if (!ytdAppCnt) return; const cProto = ytdAppCnt.constructor.prototype; if (!cProto.handleNavigate) return; if (cProto.handleNavigate.__ma355__) return; cProto.handleNavigate = handleNavigateFactory(cProto.handleNavigate); cProto.handleNavigate.__ma355__ = 1; }, }, autoExpandInfoDesc: { activated: false, toUse: false, // false by default; once the expand is clicked, maintain the feature until the browser is closed. /** @type { MutationObserver | null } */ mo: null, promiseReady: new PromiseExternal(), moFn(lockId) { if (lockGet['autoExpandInfoDescAttrAsyncLock'] !== lockId) return; const mainInfo = getMainInfo(); if (!mainInfo) return; switch (((mainInfo || 0).nodeName || '').toLowerCase()) { case 'ytd-expander': if (mainInfo.hasAttribute000('collapsed')) { let success = false; try { insp(mainInfo).handleMoreTap(new Event('tap')); success = true; } catch { } if (success) mainInfo.setAttribute111('tyt-no-less-btn', ''); } break; case 'ytd-expandable-video-description-body-renderer': const inlineExpanderElm = mainInfo.querySelector('ytd-text-inline-expander'); const inlineExpanderCnt = insp(inlineExpanderElm); if (inlineExpanderCnt && inlineExpanderCnt.isExpanded === false) { inlineExpanderCnt.isExpanded = true; inlineExpanderCnt.isExpandedChanged(); // holdInlineExpanderAlwaysExpanded(inlineExpanderCnt); } break; } }, activate() { if (this.activated) return; this.moFn = this.moFn.bind(this); this.mo = new MutationObserver(() => { Promise.resolve(lockSet['autoExpandInfoDescAttrAsyncLock']) .then(this.moFn) .catch(console.warn); }); this.activated = true; this.promiseReady.resolve(); }, async onMainInfoSet(mainInfo) { await this.promiseReady.then(); if (mainInfo.nodeName.toLowerCase() === 'ytd-expander') { this.mo.observe(mainInfo, { attributes: true, attributeFilter: ['collapsed', 'attr-8ifv7'], }); } else { this.mo.observe(mainInfo, { attributes: true, attributeFilter: ['attr-8ifv7'] }); } mainInfo.incAttribute111('attr-8ifv7'); }, }, fullChannelNameOnHover: { activated: false, toUse: true, /** @type { MutationObserver | null } */ mo: null, /** @type { ResizeObserver | null} */ ro: null, promiseReady: new PromiseExternal(), checkResize: 0, mouseEnterFn(evt) { const target = evt ? evt.target : null; if (!(target instanceof HTMLElement_)) return; const metaDataElm = target.closest('ytd-watch-metadata'); metaDataElm.classList.remove('tyt-metadata-hover-resized'); this.checkResize = Date.now() + 300; metaDataElm.classList.add('tyt-metadata-hover'); // console.log('mouseEnter') }, mouseLeaveFn(evt) { const target = evt ? evt.target : null; if (!(target instanceof HTMLElement_)) return; const metaDataElm = target.closest('ytd-watch-metadata'); metaDataElm.classList.remove('tyt-metadata-hover-resized'); metaDataElm.classList.remove('tyt-metadata-hover'); // console.log('mouseLeaveFn') }, moFn(lockId) { if (lockGet['fullChannelNameOnHoverAttrAsyncLock'] !== lockId) return; const uploadInfo = document.querySelector( '#primary.ytd-watch-flexy ytd-watch-metadata #upload-info' ); if (!uploadInfo) return; const evtOpt = { passive: true, capture: false }; uploadInfo.removeEventListener('pointerenter', this.mouseEnterFn, evtOpt); uploadInfo.removeEventListener('pointerleave', this.mouseLeaveFn, evtOpt); uploadInfo.addEventListener('pointerenter', this.mouseEnterFn, evtOpt); uploadInfo.addEventListener('pointerleave', this.mouseLeaveFn, evtOpt); }, async onNavigateFinish() { await this.promiseReady.then(); const uploadInfo = document.querySelector( '#primary.ytd-watch-flexy ytd-watch-metadata #upload-info' ); if (!uploadInfo) return; this.mo.observe(uploadInfo, { attributes: true, attributeFilter: ['hidden', 'attr-3wb0k'], }); uploadInfo.incAttribute111('attr-3wb0k'); this.ro.observe(uploadInfo); }, activate() { if (this.activated) return; // Use global isPassiveArgSupport constant // https://caniuse.com/?search=observer // https://caniuse.com/?search=addEventListener%20passive if (!isPassiveArgSupport) return; this.activated = true; this.mouseEnterFn = this.mouseEnterFn.bind(this); this.mouseLeaveFn = this.mouseLeaveFn.bind(this); this.moFn = this.moFn.bind(this); this.mo = new MutationObserver(() => { Promise.resolve(lockSet['fullChannelNameOnHoverAttrAsyncLock']) .then(this.moFn) .catch(console.warn); }); this.ro = new ResizeObserver(mutations => { if (Date.now() > this.checkResize) return; for (const mutation of mutations) { const uploadInfo = mutation.target; if (uploadInfo && mutation.contentRect.width > 0 && mutation.contentRect.height > 0) { const metaDataElm = uploadInfo.closest('ytd-watch-metadata'); if (metaDataElm.classList.contains('tyt-metadata-hover')) { metaDataElm.classList.add('tyt-metadata-hover-resized'); } break; } } }); this.promiseReady.resolve(); }, }, }; if (sessionStorage.__$$tmp_UseAutoExpandInfoDesc$$__) plugin.autoExpandInfoDesc.toUse = true; // let shouldFixInfo = false; const __attachedSymbol__ = Symbol(); const makeInitAttached = tag => { const inPageRearrange_ = inPageRearrange; inPageRearrange = false; for (const elm of document.querySelectorAll(`${tag}`)) { const cnt = insp(elm) || 0; if (typeof cnt.attached498 === 'function' && !elm[__attachedSymbol__]) { Promise.resolve(elm).then(eventMap[`${tag}::attached`]).catch(console.warn); } } inPageRearrange = inPageRearrange_; }; const getGeneralChatElement = async () => { for (let i = 2; i-- > 0;) { const t = document.querySelector( '#columns.style-scope.ytd-watch-flexy ytd-live-chat-frame#chat' ); if (t instanceof Element) return t; if (i > 0) { // try later console.log('ytd-live-chat-frame::attached - delayPn(200)'); await delayPn(200); } } return null; }; const nsTemplateObtain = () => { let nsTemplate = document.querySelector('ytd-watch-flexy noscript[ns-template]'); if (!nsTemplate) { nsTemplate = document.createElement('noscript'); nsTemplate.setAttribute('ns-template', ''); document.querySelector('ytd-watch-flexy').appendChild(nsTemplate); } return nsTemplate; }; const isPageDOM = (elm, selector) => { if (!elm || !(elm instanceof Element) || !elm.nodeName) return false; if (!elm.closest(selector)) return false; if (elm.isConnected !== true) return false; return true; }; const invalidFlexyParent = hostElement => { if (hostElement instanceof HTMLElement) { const hasFlexyParent = HTMLElement.prototype.closest.call(hostElement, 'ytd-watch-flexy'); // eg short if (!hasFlexyParent) return true; const currentFlexy = elements.flexy; if (currentFlexy && currentFlexy !== hasFlexyParent) return true; } return false; }; // const mutationComment = document.createComment('1'); // let mutationPromise = new PromiseExternal(); // const mutationPromiseObs = new MutationObserver(()=>{ // mutationPromise.resolve(); // mutationPromise = new PromiseExternal(); // }); // mutationPromiseObs.observe(mutationComment, {characterData: true}); let headerMutationObserver = null; let headerMutationTmpNode = null; const eventMap = { ceHack: () => { mLoaded.flag |= 2; document.documentElement.setAttribute111('tabview-loaded', mLoaded.makeString()); retrieveCE('ytd-watch-flexy') .then(eventMap['ytd-watch-flexy::defined']) .catch(console.warn); retrieveCE('ytd-expander').then(eventMap['ytd-expander::defined']).catch(console.warn); retrieveCE('ytd-watch-next-secondary-results-renderer') .then(eventMap['ytd-watch-next-secondary-results-renderer::defined']) .catch(err => console.warn( '[YouTube+] retrieveCE ytd-watch-next-secondary-results-renderer failed:', err ) ); retrieveCE('ytd-comments-header-renderer') .then(eventMap['ytd-comments-header-renderer::defined']) .catch(err => console.warn('[YouTube+] retrieveCE ytd-comments-header-renderer failed:', err) ); retrieveCE('ytd-live-chat-frame') .then(eventMap['ytd-live-chat-frame::defined']) .catch(err => console.warn('[YouTube+] retrieveCE ytd-live-chat-frame failed:', err)); retrieveCE('ytd-comments') .then(eventMap['ytd-comments::defined']) .catch(err => console.warn('[YouTube+] retrieveCE ytd-comments failed:', err)); retrieveCE('ytd-engagement-panel-section-list-renderer') .then(eventMap['ytd-engagement-panel-section-list-renderer::defined']) .catch(err => console.warn( '[YouTube+] retrieveCE ytd-engagement-panel-section-list-renderer failed:', err ) ); retrieveCE('ytd-watch-metadata') .then(eventMap['ytd-watch-metadata::defined']) .catch(err => console.warn('[YouTube+] retrieveCE ytd-watch-metadata failed:', err)); retrieveCE('ytd-playlist-panel-renderer') .then(eventMap['ytd-playlist-panel-renderer::defined']) .catch(err => console.warn('[YouTube+] retrieveCE ytd-playlist-panel-renderer failed:', err) ); retrieveCE('ytd-expandable-video-description-body-renderer') .then(eventMap['ytd-expandable-video-description-body-renderer::defined']) .catch(err => console.warn( '[YouTube+] retrieveCE ytd-expandable-video-description-body-renderer failed:', err ) ); }, fixForTabDisplay: isResize => { // isResize is true if the layout is resized (not due to tab switching) // youtube components shall handle the resize issue. can skip some checkings. bFixForResizedTabLater = false; for (const element of document.querySelectorAll('[io-intersected]')) { const cnt = insp(element); if (element instanceof HTMLElement_ && typeof cnt.calculateCanCollapse === 'function') { try { cnt.calculateCanCollapse(true); } catch (e) { console.warn('[YouTube+] calculateCanCollapse failed:', e); } } } if (!isResize && lastTab === '#tab-info') { // #tab-info is now shown. // to fix the sizing issue (description info cards in tab info) for (const element of document.querySelectorAll( '#tab-info ytd-video-description-infocards-section-renderer, #tab-info yt-chip-cloud-renderer, #tab-info ytd-horizontal-card-list-renderer, #tab-info yt-horizontal-list-renderer' )) { const cnt = insp(element); if (element instanceof HTMLElement_ && typeof cnt.notifyResize === 'function') { try { cnt.notifyResize(); } catch (e) { console.warn('[YouTube+] notifyResize failed for tab-info:', e); } } } // to fix expand/collapse sizing issue (inline-expander in tab info) // for example, expand button is required but not shown as it was rendered in the hidden state for (const element of document.querySelectorAll('#tab-info ytd-text-inline-expander')) { const cnt = insp(element); if (element instanceof HTMLElement_ && typeof cnt.resize === 'function') { cnt.resize(false); // reflow due to offsetWidth calling } fixInlineExpanderDisplay(cnt); // just in case } } if (!isResize && typeof lastTab === 'string' && lastTab.startsWith('#tab-')) { const tabContent = document.querySelector('.tab-content-cld:not(.tab-content-hidden)'); if (tabContent) { const renderers = tabContent.querySelectorAll('yt-chip-cloud-renderer'); for (const renderer of renderers) { const cnt = insp(renderer); if (typeof cnt.notifyResize === 'function') { try { cnt.notifyResize(); } catch (e) { console.warn('[YouTube+] notifyResize failed for renderer:', e); } } } } } }, 'ytd-watch-flexy::defined': cProto => { if ( !cProto.updateChatLocation498 && typeof cProto.updateChatLocation === 'function' && cProto.updateChatLocation.length === 0 ) { cProto.updateChatLocation498 = cProto.updateChatLocation; cProto.updateChatLocation = updateChatLocation498; } }, 'ytd-watch-next-secondary-results-renderer::defined': cProto => { if (!cProto.attached498 && typeof cProto.attached === 'function') { cProto.attached498 = cProto.attached; cProto.attached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-watch-next-secondary-results-renderer::attached']) .catch(console.warn); } return this.attached498(); }; } if (!cProto.detached498 && typeof cProto.detached === 'function') { cProto.detached498 = cProto.detached; cProto.detached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-watch-next-secondary-results-renderer::detached']) .catch(console.warn); } return this.detached498(); }; } makeInitAttached('ytd-watch-next-secondary-results-renderer'); }, 'ytd-watch-next-secondary-results-renderer::attached': hostElement => { if (invalidFlexyParent(hostElement)) return; // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-watch-next-secondary-results-renderer::attached'); if (hostElement instanceof Element) hostElement[__attachedSymbol__] = true; if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } if (hostElement.isConnected !== true) return; // if (hostElement.__connectedFlg__ !== 4) return; // hostElement.__connectedFlg__ = 5; if ( hostElement instanceof HTMLElement_ && hostElement.matches('#columns #related ytd-watch-next-secondary-results-renderer') && !hostElement.matches( '#right-tabs ytd-watch-next-secondary-results-renderer, [hidden] ytd-watch-next-secondary-results-renderer' ) ) { elements.related = hostElement.closest('#related'); hostElement.setAttribute111('tyt-videos-list', ''); } // console.log('ytd-watch-next-secondary-results-renderer::attached', hostElement); }, 'ytd-watch-next-secondary-results-renderer::detached': hostElement => { // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-watch-next-secondary-results-renderer::detached'); if (!(hostElement instanceof HTMLElement_) || hostElement.closest('noscript')) return; if (hostElement.isConnected !== false) return; // if (hostElement.__connectedFlg__ !== 8) return; // hostElement.__connectedFlg__ = 9; if (hostElement.hasAttribute000('tyt-videos-list')) { elements.related = null; hostElement.removeAttribute000('tyt-videos-list'); } console.log('ytd-watch-next-secondary-results-renderer::detached', hostElement); }, settingCommentsVideoId: hostElement => { if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } const cnt = insp(hostElement); const commentsArea = elements.comments; if ( commentsArea !== hostElement || hostElement.isConnected !== true || cnt.isAttached !== true || !cnt.data || cnt.hidden !== false ) { return; } const ytdFlexyElm = elements.flexy; const ytdFlexyCnt = ytdFlexyElm ? insp(ytdFlexyElm) : null; if (ytdFlexyCnt && ytdFlexyCnt.videoId) { hostElement.setAttribute111('tyt-comments-video-id', ytdFlexyCnt.videoId); } else { hostElement.removeAttribute000('tyt-comments-video-id'); } }, checkCommentsShouldBeHidden: lockId => { if (lockGet['checkCommentsShouldBeHiddenLock'] !== lockId) return; // commentsArea's attribute: tyt-comments-video-id // ytdFlexyElm's attribute: video-id const commentsArea = elements.comments; const ytdFlexyElm = elements.flexy; if (commentsArea && ytdFlexyElm && !commentsArea.hasAttribute000('hidden')) { const ytdFlexyCnt = insp(ytdFlexyElm); if (typeof ytdFlexyCnt.videoId === 'string') { const commentsVideoId = commentsArea.getAttribute('tyt-comments-video-id'); if (commentsVideoId && commentsVideoId !== ytdFlexyCnt.videoId) { commentsArea.setAttribute111('hidden', ''); // removeKeepCommentsScroller(); } } } }, 'ytd-comments::defined': cProto => { if (!cProto.attached498 && typeof cProto.attached === 'function') { cProto.attached498 = cProto.attached; cProto.attached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-comments::attached']) .catch(console.warn); } // Promise.resolve(this.hostElement).then(eventMap['ytd-comments::dataChanged_']).catch(console.warn); return this.attached498(); }; } if (!cProto.detached498 && typeof cProto.detached === 'function') { cProto.detached498 = cProto.detached; cProto.detached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-comments::detached']) .catch(console.warn); } // Promise.resolve(this.hostElement).then(eventMap['ytd-comments::dataChanged_']).catch(console.warn); return this.detached498(); }; } cProto._createPropertyObserver('data', '_dataChanged498', undefined); cProto._dataChanged498 = function () { // console.log('_dataChanged498', this.hostElement) Promise.resolve(this.hostElement) .then(eventMap['ytd-comments::_dataChanged498']) .catch(console.warn); }; // if (!cProto.dataChanged498_ && typeof cProto.dataChanged_ === 'function') { // cProto.dataChanged498_ = cProto.dataChanged_; // cProto.dataChanged_ = function () { // Promise.resolve(this.hostElement).then(eventMap['ytd-comments::dataChanged_']).catch(console.warn); // return this.dataChanged498_(); // } // } makeInitAttached('ytd-comments'); }, 'ytd-comments::_dataChanged498': hostElement => { // console.log(18984, hostElement.hasAttribute('tyt-comments-area')) if (!hostElement.hasAttribute000('tyt-comments-area')) return; let commentsDataStatus = 0; const cnt = insp(hostElement); const data = cnt ? cnt.data : null; const contents = data ? data.contents : null; if (data) { if (contents && contents.length === 1 && contents[0].messageRenderer) { commentsDataStatus = 2; } if (contents && contents.length > 1 && contents[0].commentThreadRenderer) { commentsDataStatus = 1; } } if (commentsDataStatus) { hostElement.setAttribute111('tyt-comments-data-status', commentsDataStatus); // ytdFlexyElm.setAttribute111('tyt-comment-disabled', '') } else { // ytdFlexyElm.removeAttribute000('tyt-comment-disabled') hostElement.removeAttribute000('tyt-comments-data-status'); } Promise.resolve(hostElement).then(eventMap['settingCommentsVideoId']).catch(console.warn); }, 'ytd-comments::attached': async hostElement => { if (invalidFlexyParent(hostElement)) return; // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-comments::attached'); if (hostElement instanceof Element) hostElement[__attachedSymbol__] = true; if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } if (hostElement.isConnected !== true) return; // if (hostElement.__connectedFlg__ !== 4) return; // hostElement.__connectedFlg__ = 5; if (!hostElement || hostElement.id !== 'comments') return; // if (!hostElement || hostElement.closest('[hidden]')) return; elements.comments = hostElement; console.log('ytd-comments::attached'); Promise.resolve(hostElement).then(eventMap['settingCommentsVideoId']).catch(console.warn); aoComment.observe(hostElement, { attributes: true }); hostElement.setAttribute111('tyt-comments-area', ''); const lockId = lockSet['rightTabReadyLock02']; await rightTabsProvidedPromise.then(); if (lockGet['rightTabReadyLock02'] !== lockId) return; if (elements.comments !== hostElement) return; if (hostElement.isConnected === false) return; DEBUG_5085 && console.log(7932, 'comments'); // if(!elements.comments || elements.comments.isConnected === false) return; if (hostElement && !hostElement.closest('#right-tabs')) { document.querySelector('#tab-comments').assignChildren111(null, hostElement, null); } else { const shouldTabVisible = elements.comments && elements.comments.closest('#tab-comments') && !elements.comments.closest('[hidden]'); document .querySelector('[tyt-tab-content="#tab-comments"]') .classList.toggle('tab-btn-hidden', !shouldTabVisible); // document.querySelector('#tab-comments').classList.remove('tab-content-hidden') // document.querySelector('[tyt-tab-content="#tab-comments"]').classList.remove('tab-btn-hidden') Promise.resolve(lockSet['removeKeepCommentsScrollerLock']) .then(removeKeepCommentsScroller) .catch(console.warn); } TAB_AUTO_SWITCH_TO_COMMENTS && switchToTab('#tab-comments'); }, 'ytd-comments::detached': hostElement => { // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-comments::detached'); // console.log(858, hostElement) if (!(hostElement instanceof HTMLElement_) || hostElement.closest('noscript')) return; if (hostElement.isConnected !== false) return; // if (hostElement.__connectedFlg__ !== 8) return; // hostElement.__connectedFlg__ = 9; if (hostElement.hasAttribute000('tyt-comments-area')) { // foComments.disconnect(); // foComments.takeRecords(); hostElement.removeAttribute000('tyt-comments-area'); // document.querySelector('#tab-comments').classList.add('tab-content-hidden') // document.querySelector('[tyt-tab-content="#tab-comments"]').classList.add('tab-btn-hidden') aoComment.disconnect(); aoComment.takeRecords(); elements.comments = null; document .querySelector('[tyt-tab-content="#tab-comments"]') .classList.add('tab-btn-hidden'); Promise.resolve(lockSet['removeKeepCommentsScrollerLock']) .then(removeKeepCommentsScroller) .catch(console.warn); } }, 'ytd-comments-header-renderer::defined': cProto => { if (!cProto.attached498 && typeof cProto.attached === 'function') { cProto.attached498 = cProto.attached; cProto.attached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-comments-header-renderer::attached']) .catch(console.warn); } Promise.resolve(this.hostElement) .then(eventMap['ytd-comments-header-renderer::dataChanged']) .catch(console.warn); // force dataChanged on attached return this.attached498(); }; } if (!cProto.detached498 && typeof cProto.detached === 'function') { cProto.detached498 = cProto.detached; cProto.detached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-comments-header-renderer::detached']) .catch(console.warn); } return this.detached498(); }; } if (!cProto.dataChanged498 && typeof cProto.dataChanged === 'function') { cProto.dataChanged498 = cProto.dataChanged; cProto.dataChanged = function () { Promise.resolve(this.hostElement) .then(eventMap['ytd-comments-header-renderer::dataChanged']) .catch(console.warn); return this.dataChanged498(); }; } makeInitAttached('ytd-comments-header-renderer'); }, 'ytd-comments-header-renderer::attached': hostElement => { if (invalidFlexyParent(hostElement)) return; // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-comments-header-renderer::attached'); if (hostElement instanceof Element) hostElement[__attachedSymbol__] = true; if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } if (hostElement.isConnected !== true) return; // if (hostElement.__connectedFlg__ !== 4) return; // hostElement.__connectedFlg__ = 5; if (!hostElement || !hostElement.classList.contains('ytd-item-section-renderer')) return; // console.log(12991, 'ytd-comments-header-renderer::attached') const targetElement = document.querySelector( '[tyt-comments-area] ytd-comments-header-renderer' ); if (hostElement === targetElement) { hostElement.setAttribute111('tyt-comments-header-field', ''); } else { const parentNode = hostElement.parentNode; if ( parentNode instanceof HTMLElement_ && parentNode.querySelector('[tyt-comments-header-field]') ) { hostElement.setAttribute111('tyt-comments-header-field', ''); } } }, 'ytd-comments-header-renderer::detached': hostElement => { // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-comments-header-renderer::detached'); if (!(hostElement instanceof HTMLElement_) || hostElement.closest('noscript')) return; if (hostElement.isConnected !== false) return; // if (hostElement.__connectedFlg__ !== 8) return; // hostElement.__connectedFlg__ = 9; // console.log(12992, 'ytd-comments-header-renderer::detached') if (hostElement.hasAttribute000('field-of-cm-count')) { hostElement.removeAttribute000('field-of-cm-count'); const cmCount = document.querySelector('#tyt-cm-count'); if ( cmCount && !document.querySelector('#tab-comments ytd-comments-header-renderer[field-of-cm-count]') ) { cmCount.textContent = ''; } } if (hostElement.hasAttribute000('tyt-comments-header-field')) { hostElement.removeAttribute000('tyt-comments-header-field'); } }, 'ytd-comments-header-renderer::dataChanged': hostElement => { if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } const ytdFlexyElm = elements.flexy; let b = false; const cnt = insp(hostElement); if ( cnt && hostElement.closest('#tab-comments') && document.querySelector('#tab-comments ytd-comments-header-renderer') === hostElement ) { b = true; } else if ( hostElement instanceof HTMLElement_ && hostElement.parentNode instanceof HTMLElement_ && hostElement.parentNode.querySelector('[tyt-comments-header-field]') ) { b = true; } if (b) { hostElement.setAttribute111('tyt-comments-header-field', ''); ytdFlexyElm && ytdFlexyElm.removeAttribute000('tyt-comment-disabled'); } if ( hostElement.hasAttribute000('tyt-comments-header-field') && hostElement.isConnected === true ) { if (!headerMutationObserver) { headerMutationObserver = new MutationObserver( eventMap['ytd-comments-header-renderer::deferredCounterUpdate'] ); } headerMutationObserver.observe(hostElement.parentNode, { subtree: false, childList: true, }); if (!headerMutationTmpNode) { headerMutationTmpNode = document.createElementNS('http://www.w3.org/2000/svg', 'defs'); } const tmpNode = headerMutationTmpNode; hostElement.insertAdjacentElement('afterend', tmpNode); tmpNode.remove(); } }, 'ytd-comments-header-renderer::deferredCounterUpdate': () => { const nodes = document.querySelectorAll( '#tab-comments ytd-comments-header-renderer[class]' ); if (nodes.length === 1) { const hostElement = nodes[0]; const cnt = insp(hostElement); const data = cnt.data; if (!data) return; let ez = ''; if ( data.commentsCount && data.commentsCount.runs && data.commentsCount.runs.length >= 1 ) { let max = -1; const z = data.commentsCount.runs .map(e => { const c = e.text.replace(/\D+/g, '').length; if (c > max) max = c; return [e.text, c]; }) .filter(a => a[1] === max); if (z.length >= 1) { ez = z[0][0]; } } else if (data.countText && data.countText.runs && data.countText.runs.length >= 1) { let max = -1; const z = data.countText.runs .map(e => { const c = e.text.replace(/\D+/g, '').length; if (c > max) max = c; return [e.text, c]; }) .filter(a => a[1] === max); if (z.length >= 1) { ez = z[0][0]; } } const cmCount = document.querySelector('#tyt-cm-count'); if (ez) { hostElement.setAttribute111('field-of-cm-count', ''); cmCount && (cmCount.textContent = ez.trim()); } else { hostElement.removeAttribute000('field-of-cm-count'); cmCount && (cmCount.textContent = ''); console.warn('no text for #tyt-cm-count'); } } }, 'ytd-expander::defined': cProto => { if (!cProto.attached498 && typeof cProto.attached === 'function') { cProto.attached498 = cProto.attached; cProto.attached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-expander::attached']) .catch(console.warn); } return this.attached498(); }; } if (!cProto.detached498 && typeof cProto.detached === 'function') { cProto.detached498 = cProto.detached; cProto.detached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-expander::detached']) .catch(console.warn); } return this.detached498(); }; } if (!cProto.calculateCanCollapse498 && typeof cProto.calculateCanCollapse === 'function') { cProto.calculateCanCollapse498 = cProto.calculateCanCollapse; cProto.calculateCanCollapse = funcCanCollapse; } if (!cProto.childrenChanged498 && typeof cProto.childrenChanged === 'function') { cProto.childrenChanged498 = cProto.childrenChanged; cProto.childrenChanged = function () { Promise.resolve(this.hostElement) .then(eventMap['ytd-expander::childrenChanged']) .catch(console.warn); return this.childrenChanged498(); }; } /* console.log('ytd-expander::defined 01'); CustomElementRegistry.prototype.get.call(customElements, 'ytd-expander').prototype.connectedCallback = connectedCallbackY(CustomElementRegistry.prototype.get.call(customElements, 'ytd-expander').prototype.connectedCallback) CustomElementRegistry.prototype.get.call(customElements, 'ytd-expander').prototype.disconnectedCallback = disconnectedCallbackY(CustomElementRegistry.prototype.get.call(customElements, 'ytd-expander').prototype.disconnectedCallback) console.log('ytd-expander::defined 02'); */ makeInitAttached('ytd-expander'); }, 'ytd-expander::childrenChanged': hostElement => { if ( hostElement instanceof Node && hostElement.hasAttribute000('hidden') && hostElement.hasAttribute000('tyt-main-info') && hostElement.firstElementChild ) { hostElement.removeAttribute('hidden'); } }, 'ytd-expandable-video-description-body-renderer::defined': cProto => { if (!cProto.attached498 && typeof cProto.attached === 'function') { cProto.attached498 = cProto.attached; cProto.attached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-expandable-video-description-body-renderer::attached']) .catch(console.warn); } return this.attached498(); }; } if (!cProto.detached498 && typeof cProto.detached === 'function') { cProto.detached498 = cProto.detached; cProto.detached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-expandable-video-description-body-renderer::detached']) .catch(console.warn); } return this.detached498(); }; } makeInitAttached('ytd-expandable-video-description-body-renderer'); }, 'ytd-expandable-video-description-body-renderer::attached': async hostElement => { if ( hostElement instanceof HTMLElement_ && isPageDOM(hostElement, '[tyt-info-renderer]') && !hostElement.matches('[tyt-main-info]') ) { elements.infoExpander = hostElement; console.log(128384, elements.infoExpander); // console.log(1299, hostElement.parentNode, isRightTabsInserted) infoExpanderElementProvidedPromise.resolve(); hostElement.setAttribute111('tyt-main-info', ''); if (plugin.autoExpandInfoDesc.toUse) { plugin.autoExpandInfoDesc.onMainInfoSet(hostElement); } const lockId = lockSet['rightTabReadyLock03']; await rightTabsProvidedPromise.then(); if (lockGet['rightTabReadyLock03'] !== lockId) return; if (elements.infoExpander !== hostElement) return; if (hostElement.isConnected === false) return; console.log(7932, 'infoExpander'); elements.infoExpander.classList.add('tyt-main-info'); // add a classname for it const infoExpander = elements.infoExpander; // const infoExpanderBack = elements.infoExpanderBack; // console.log(5438,infoExpander, qt); // const dummy = document.createElement('noscript'); // dummy.setAttribute000('id', 'info-expander-vid'); // dummy.setAttribute000('video-id', getCurrentVideoId()); // infoExpander.insertBefore000(dummy, infoExpander.firstChild); // aoInfo.observe(infoExpander, { attributes: true, attributeFilter: ['tyt-display-for', 'tyt-video-id'] }); // zoInfo.observe(infoExpanderBack, { attributes: true, attributeFilter: ['hidden', 'attr-w20ts'], childList: true, subtree: true}); // new MutationObserver(()=>{ // console.log(591499) // }).observe(infoExpanderBack, {childList: true, subtree: true}) const inlineExpanderElm = infoExpander.querySelector('ytd-text-inline-expander'); if (inlineExpanderElm) { const mo = new MutationObserver(() => { const p = document.querySelector('#tab-info ytd-text-inline-expander'); sessionStorage.__$$tmp_UseAutoExpandInfoDesc$$__ = p && p.hasAttribute('is-expanded') ? '1' : ''; if (p) fixInlineExpanderContent(); }); mo.observe(inlineExpanderElm, { attributes: ['is-expanded', 'attr-6v8qu', 'hidden'], subtree: true, }); // hidden + subtree to trigger the fn by delayedUpdate inlineExpanderElm.incAttribute111('attr-6v8qu'); const cnt = insp(inlineExpanderElm); if (cnt) fixInlineExpanderDisplay(cnt); } if (infoExpander && !infoExpander.closest('#right-tabs')) { document.querySelector('#tab-info').assignChildren111(null, infoExpander, null); } else { if (document.querySelector('[tyt-tab-content="#tab-info"]')) { const shouldTabVisible = elements.infoExpander && elements.infoExpander.closest('#tab-info'); document .querySelector('[tyt-tab-content="#tab-info"]') .classList.toggle('tab-btn-hidden', !shouldTabVisible); } } Promise.resolve(lockSet['infoFixLock']).then(infoFix).catch(console.warn); // required when the page is switched from channel to watch // if (infoExpander && infoExpander.closest('#right-tabs')) Promise.resolve(lockSet['infoFixLock']).then(infoFix).catch(console.warn); // infoExpanderBack.incAttribute111('attr-w20ts'); // return; } DEBUG_5084 && console.log(5084, 'ytd-expandable-video-description-body-renderer::attached'); if (hostElement instanceof Element) hostElement[__attachedSymbol__] = true; if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } if (hostElement.isConnected !== true) return; if (isPageDOM(hostElement, '#tab-info [tyt-main-info]')) { // const cnt = insp(hostElement); // if(cnt.data){ // cnt.data = Object.assign({}, cnt.data); // } } else if (!hostElement.closest('#tab-info')) { const bodyRenderer = hostElement; let bodyRendererNew = document.querySelector( 'ytd-expandable-video-description-body-renderer[tyt-info-renderer]' ); if (!bodyRendererNew) { bodyRendererNew = document.createElement( 'ytd-expandable-video-description-body-renderer' ); bodyRendererNew.setAttribute('tyt-info-renderer', ''); nsTemplateObtain().appendChild(bodyRendererNew); } // document.querySelector('#tab-info').assignChildren111(null, bodyRendererNew, null); const cnt = insp(bodyRendererNew); cnt.data = Object.assign({}, insp(bodyRenderer).data); const inlineExpanderElm = bodyRendererNew.querySelector('ytd-text-inline-expander'); const inlineExpanderCnt = insp(inlineExpanderElm); fixInlineExpanderMethods(inlineExpanderCnt); // insp(bodyRendererNew).data = insp(bodyRenderer).data; // if((bodyRendererNew.hasAttribute('hidden')?1:0)^(bodyRenderer.hasAttribute('hidden')?1:0)){ // if(bodyRenderer.hasAttribute('hidden')) bodyRendererNew.setAttribute('hidden', ''); // else bodyRendererNew.removeAttribute('hidden'); // } elements.infoExpanderRendererBack = bodyRenderer; elements.infoExpanderRendererFront = bodyRendererNew; bodyRenderer.setAttribute('tyt-info-renderer-back', ''); bodyRendererNew.setAttribute('tyt-info-renderer-front', ''); // elements.infoExpanderBack = {{ytd-expander}}; } }, 'ytd-expandable-video-description-body-renderer::detached': async hostElement => { if (!(hostElement instanceof HTMLElement_) || hostElement.closest('noscript')) return; if (hostElement.isConnected !== false) return; // if (hostElement.__connectedFlg__ !== 8) return; // hostElement.__connectedFlg__ = 9; // console.log(5992, hostElement) if (hostElement.hasAttribute000('tyt-main-info')) { DEBUG_5084 && console.log(5084, 'ytd-expandable-video-description-body-renderer::detached'); elements.infoExpander = null; hostElement.removeAttribute000('tyt-main-info'); } }, 'ytd-expander::attached': async hostElement => { if (invalidFlexyParent(hostElement)) return; // if (inPageRearrange) return; if (hostElement instanceof Element) hostElement[__attachedSymbol__] = true; if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } if (hostElement.isConnected !== true) return; // if (hostElement.__connectedFlg__ !== 4) return; // hostElement.__connectedFlg__ = 5; // console.log(4959, hostElement) if ( hostElement instanceof HTMLElement_ && hostElement.matches('[tyt-comments-area] #contents ytd-expander#expander') && !hostElement.matches('[hidden] ytd-expander#expander') ) { hostElement.setAttribute111('tyt-content-comment-entry', ''); ioComment.observe(hostElement); } // -------------- // else if (hostElement instanceof HTMLElement_ && hostElement.matches('ytd-expander#expander.style-scope.ytd-expandable-video-description-body-renderer')) { // // && !hostElement.matches('#right-tabs ytd-expander#expander, [hidden] ytd-expander#expander') // console.log(5084, 'ytd-expander::attached'); // const bodyRenderer = hostElement.closest('ytd-expandable-video-description-body-renderer'); // let bodyRendererNew = document.querySelector('ytd-expandable-video-description-body-renderer[tyt-info-renderer]'); // if (!bodyRendererNew) { // bodyRendererNew = document.createElement('ytd-expandable-video-description-body-renderer'); // bodyRendererNew.setAttribute('tyt-info-renderer', ''); // nsTemplateObtain().appendChild(bodyRendererNew); // } // // document.querySelector('#tab-info').assignChildren111(null, bodyRendererNew, null); // insp(bodyRendererNew).data = insp(bodyRenderer).data; // // if((bodyRendererNew.hasAttribute('hidden')?1:0)^(bodyRenderer.hasAttribute('hidden')?1:0)){ // // if(bodyRenderer.hasAttribute('hidden')) bodyRendererNew.setAttribute('hidden', ''); // // else bodyRendererNew.removeAttribute('hidden'); // // } // elements.infoExpanderRendererBack = bodyRenderer; // elements.infoExpanderRendererFront = bodyRendererNew; // bodyRenderer.setAttribute('tyt-info-renderer-back','') // bodyRendererNew.setAttribute('tyt-info-renderer-front','') // elements.infoExpanderBack = hostElement; // } // -------------- // console.log('ytd-expander::attached', hostElement); }, 'ytd-expander::detached': hostElement => { // if (inPageRearrange) return; if (!(hostElement instanceof HTMLElement_) || hostElement.closest('noscript')) return; if (hostElement.isConnected !== false) return; // if (hostElement.__connectedFlg__ !== 8) return; // hostElement.__connectedFlg__ = 9; // console.log(5992, hostElement) if (hostElement.hasAttribute000('tyt-content-comment-entry')) { ioComment.unobserve(hostElement); hostElement.removeAttribute000('tyt-content-comment-entry'); } else if (hostElement.hasAttribute000('tyt-main-info')) { DEBUG_5084 && console.log(5084, 'ytd-expander::detached'); elements.infoExpander = null; hostElement.removeAttribute000('tyt-main-info'); } // console.log('ytd-expander::detached', hostElement); }, 'ytd-live-chat-frame::defined': cProto => { // eslint-disable-next-line no-unused-vars let lastDomAction = 0; if (!cProto.attached498 && typeof cProto.attached === 'function') { cProto.attached498 = cProto.attached; cProto.attached = function () { lastDomAction = Date.now(); // console.log('chat868-attached', Date.now()); if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-live-chat-frame::attached']) .catch(console.warn); } return this.attached498(); }; } if (!cProto.detached498 && typeof cProto.detached === 'function') { cProto.detached498 = cProto.detached; cProto.detached = function () { lastDomAction = Date.now(); // console.log('chat868-detached', Date.now()); if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-live-chat-frame::detached']) .catch(console.warn); } return this.detached498(); }; } if ( typeof cProto.urlChanged === 'function' && !cProto.urlChanged66 && !cProto.urlChangedAsync12 && cProto.urlChanged.length === 0 ) { cProto.urlChanged66 = cProto.urlChanged; let ath = 0; cProto.urlChangedAsync12 = async function () { await this.__urlChangedAsyncT689__; const t = (ath = (ath & 1073741823) + 1); const chatframe = this.chatframe || (this.$ || 0).chatframe || 0; if (chatframe instanceof HTMLIFrameElement) { if (chatframe.contentDocument === null) { await Promise.resolve('#').catch(console.warn); if (t !== ath) return; } await new Promise(resolve => setTimeout_(resolve, 1)).catch(console.warn); // neccessary for Brave if (t !== ath) return; const isBlankPage = !this.data || this.collapsed; const p1 = new Promise(resolve => setTimeout_(resolve, 706)).catch(console.warn); const p2 = new Promise(resolve => { new IntersectionObserver((entries, observer) => { for (const entry of entries) { const rect = entry.boundingClientRect || 0; if (isBlankPage || (rect.width > 0 && rect.height > 0)) { observer.disconnect(); resolve('#'); break; } } }).observe(chatframe); }).catch(console.warn); await Promise.race([p1, p2]); if (t !== ath) return; } this.urlChanged66(); }; cProto.urlChanged = function () { const t = (this.__urlChangedAsyncT688__ = (this.__urlChangedAsyncT688__ & 1073741823) + 1); nextBrowserTick(() => { if (t !== this.__urlChangedAsyncT688__) return; this.urlChangedAsync12(); }); }; } makeInitAttached('ytd-live-chat-frame'); }, 'ytd-live-chat-frame::attached': async hostElement => { if (invalidFlexyParent(hostElement)) return; // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-live-chat-frame::attached'); if (hostElement instanceof Element) hostElement[__attachedSymbol__] = true; if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } if (hostElement.isConnected !== true) return; // if (hostElement.__connectedFlg__ !== 4) return; // hostElement.__connectedFlg__ = 5; if (!hostElement || hostElement.id !== 'chat') return; console.log('ytd-live-chat-frame::attached'); const lockId = lockSet['ytdLiveAttachedLock']; const chatElem = await getGeneralChatElement(); if (lockGet['ytdLiveAttachedLock'] !== lockId) return; if (chatElem === hostElement) { elements.chat = chatElem; aoChat.observe(chatElem, { attributes: true }); const isFlexyReady = elements.flexy instanceof Element; chatElem.setAttribute111('tyt-active-chat-frame', isFlexyReady ? 'CF' : 'C'); const chatContainer = chatElem ? chatElem.closest('#chat-container') || chatElem : null; if (chatContainer && !chatContainer.hasAttribute000('tyt-chat-container')) { for (const p of document.querySelectorAll('[tyt-chat-container]')) { p.removeAttribute000('[tyt-chat-container]'); } chatContainer.setAttribute111('tyt-chat-container', ''); } const cnt = insp(hostElement); const q = cnt.__urlChangedAsyncT688__; const p = (cnt.__urlChangedAsyncT689__ = new PromiseExternal()); setTimeout_(() => { if (p !== cnt.__urlChangedAsyncT689__) return; if (cnt.isAttached === true && hostElement.isConnected === true) { p.resolve(); if (q === cnt.__urlChangedAsyncT688__) { cnt.urlChanged(); } } }, 320); Promise.resolve(lockSet['layoutFixLock']).then(layoutFix); } else { console.log('Issue found in ytd-live-chat-frame::attached', chatElem, hostElement); } }, 'ytd-live-chat-frame::detached': hostElement => { // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-live-chat-frame::detached'); if (!(hostElement instanceof HTMLElement_) || hostElement.closest('noscript')) return; if (hostElement.isConnected !== false) return; // if (hostElement.__connectedFlg__ !== 8) return; // hostElement.__connectedFlg__ = 9; console.log('ytd-live-chat-frame::detached'); if (hostElement.hasAttribute000('tyt-active-chat-frame')) { aoChat.disconnect(); aoChat.takeRecords(); hostElement.removeAttribute000('tyt-active-chat-frame'); elements.chat = null; const ytdFlexyElm = elements.flexy; if (ytdFlexyElm) { ytdFlexyElm.removeAttribute000('tyt-chat-collapsed'); ytdFlexyElm.setAttribute111('tyt-chat', ''); } } }, 'ytd-engagement-panel-section-list-renderer::defined': cProto => { if (!cProto.attached498 && typeof cProto.attached === 'function') { cProto.attached498 = cProto.attached; cProto.attached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-engagement-panel-section-list-renderer::attached']) .catch(console.warn); } return this.attached498(); }; } if (!cProto.detached498 && typeof cProto.detached === 'function') { cProto.detached498 = cProto.detached; cProto.detached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-engagement-panel-section-list-renderer::detached']) .catch(console.warn); } return this.detached498(); }; } makeInitAttached('ytd-engagement-panel-section-list-renderer'); }, 'ytd-engagement-panel-section-list-renderer::bindTarget': hostElement => { if ( hostElement.matches( '#panels.ytd-watch-flexy > ytd-engagement-panel-section-list-renderer[target-id][visibility]' ) ) { hostElement.setAttribute111('tyt-egm-panel', ''); Promise.resolve(lockSet['updateEgmPanelsLock']).then(updateEgmPanels).catch(console.warn); aoEgmPanels.observe(hostElement, { attributes: true, attributeFilter: ['visibility', 'hidden'], }); // console.log(5094, 2, 'ytd-engagement-panel-section-list-renderer::attached', hostElement); } }, 'ytd-engagement-panel-section-list-renderer::attached': hostElement => { if (invalidFlexyParent(hostElement)) return; // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-engagement-panel-section-list-renderer::attached'); if (hostElement instanceof Element) hostElement[__attachedSymbol__] = true; if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } if (hostElement.isConnected !== true) return; // if (hostElement.__connectedFlg__ !== 4) return; // hostElement.__connectedFlg__ = 5; // console.log('ytd-engagement-panel-section-list-renderer::attached', hostElement) // console.log(5094, 1, 'ytd-engagement-panel-section-list-renderer::attached', hostElement); if ( !hostElement.matches( '#panels.ytd-watch-flexy > ytd-engagement-panel-section-list-renderer' ) ) { return; } if (hostElement.hasAttribute000('target-id') && hostElement.hasAttribute000('visibility')) { Promise.resolve(hostElement) .then(eventMap['ytd-engagement-panel-section-list-renderer::bindTarget']) .catch(console.warn); } else { hostElement.setAttribute000('tyt-egm-panel-jclmd', ''); moEgmPanelReady.observe(hostElement, { attributes: true, attributeFilter: ['visibility', 'target-id'], }); } }, 'ytd-engagement-panel-section-list-renderer::detached': hostElement => { // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-engagement-panel-section-list-renderer::detached'); if (!(hostElement instanceof HTMLElement_) || hostElement.closest('noscript')) return; if (hostElement.isConnected !== false) return; // if (hostElement.__connectedFlg__ !== 8) return; // hostElement.__connectedFlg__ = 9; if (hostElement.hasAttribute000('tyt-egm-panel')) { hostElement.removeAttribute000('tyt-egm-panel'); Promise.resolve(lockSet['updateEgmPanelsLock']).then(updateEgmPanels).catch(console.warn); } else if (hostElement.hasAttribute000('tyt-egm-panel-jclmd')) { hostElement.removeAttribute000('tyt-egm-panel-jclmd'); moEgmPanelReadyClearFn(); } }, 'ytd-watch-metadata::defined': cProto => { if (!cProto.attached498 && typeof cProto.attached === 'function') { cProto.attached498 = cProto.attached; cProto.attached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-watch-metadata::attached']) .catch(console.warn); } return this.attached498(); }; } if (!cProto.detached498 && typeof cProto.detached === 'function') { cProto.detached498 = cProto.detached; cProto.detached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-watch-metadata::detached']) .catch(console.warn); } return this.detached498(); }; } makeInitAttached('ytd-watch-metadata'); }, 'ytd-watch-metadata::attached': hostElement => { if (invalidFlexyParent(hostElement)) return; // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-watch-metadata::attached'); if (hostElement instanceof Element) hostElement[__attachedSymbol__] = true; if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } if (hostElement.isConnected !== true) return; // if (hostElement.__connectedFlg__ !== 4) return; // hostElement.__connectedFlg__ = 5; if (plugin.fullChannelNameOnHover.activated) { plugin.fullChannelNameOnHover.onNavigateFinish(); } }, 'ytd-watch-metadata::detached': hostElement => { // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-watch-metadata::detached'); if (!(hostElement instanceof HTMLElement_) || hostElement.closest('noscript')) return; if (hostElement.isConnected !== false) return; // if (hostElement.__connectedFlg__ !== 8) return; // hostElement.__connectedFlg__ = 9; }, 'ytd-playlist-panel-renderer::defined': cProto => { if (!cProto.attached498 && typeof cProto.attached === 'function') { cProto.attached498 = cProto.attached; cProto.attached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-playlist-panel-renderer::attached']) .catch(console.warn); } return this.attached498(); }; } if (!cProto.detached498 && typeof cProto.detached === 'function') { cProto.detached498 = cProto.detached; cProto.detached = function () { if (!inPageRearrange) { Promise.resolve(this.hostElement) .then(eventMap['ytd-playlist-panel-renderer::detached']) .catch(console.warn); } return this.detached498(); }; } makeInitAttached('ytd-playlist-panel-renderer'); }, 'ytd-playlist-panel-renderer::attached': hostElement => { if (invalidFlexyParent(hostElement)) return; // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-playlist-panel-renderer::attached'); if (hostElement instanceof Element) hostElement[__attachedSymbol__] = true; if ( !(hostElement instanceof HTMLElement_) || !(hostElement.classList.length > 0) || hostElement.closest('noscript') ) { return; } if (hostElement.isConnected !== true) return; // if (hostElement.__connectedFlg__ !== 4) return; // hostElement.__connectedFlg__ = 5; elements.playlist = hostElement; aoPlayList.observe(hostElement, { attributes: true, attributeFilter: ['hidden', 'collapsed', 'attr-1y6nu'], }); hostElement.incAttribute111('attr-1y6nu'); }, 'ytd-playlist-panel-renderer::detached': hostElement => { // if (inPageRearrange) return; DEBUG_5084 && console.log(5084, 'ytd-playlist-panel-renderer::detached'); if (!(hostElement instanceof HTMLElement_) || hostElement.closest('noscript')) return; if (hostElement.isConnected !== false) return; // if (hostElement.__connectedFlg__ !== 8) return; // hostElement.__connectedFlg__ = 9; }, _yt_playerProvided: () => { mLoaded.flag |= 4; document.documentElement.setAttribute111('tabview-loaded', mLoaded.makeString()); }, relatedElementProvided: target => { if (target.closest('[hidden]')) return; elements.related = target; console.log('relatedElementProvided'); videosElementProvidedPromise.resolve(); }, onceInfoExpanderElementProvidedPromised: () => { console.log('hide-default-text-inline-expander'); const ytdFlexyElm = elements.flexy; if (ytdFlexyElm) { ytdFlexyElm.setAttribute111('hide-default-text-inline-expander', ''); } }, refreshSecondaryInner: lockId => { if (lockGet['refreshSecondaryInnerLock'] !== lockId) return; /* ytd-watch-flexy:not([panels-beside-player]):not([fixed-panels]) #panels-full-bleed-container.ytd-watch-flexy{ display: none;} #player-full-bleed-container.ytd-watch-flexy{ position: relative; flex: 1;} */ const ytdFlexyElm = elements.flexy; // if(ytdFlexyElm && ytdFlexyElm.matches('ytd-watch-flexy[fixed-panels][theater]')){ // // ytdFlexyElm.fixedPanels = true; // ytdFlexyElm.removeAttribute000('fixed-panels'); // } if ( ytdFlexyElm && ytdFlexyElm.matches( 'ytd-watch-flexy[theater][full-bleed-player]:not([full-bleed-no-max-width-columns])' ) ) { // ytdFlexyElm.fullBleedNoMaxWidthColumns = true; ytdFlexyElm.setAttribute111('full-bleed-no-max-width-columns', ''); } const related = elements.related; if (related && related.isConnected && !related.closest('#right-tabs #tab-videos')) { document.querySelector('#tab-videos').assignChildren111(null, related, null); } const infoExpander = elements.infoExpander; if ( infoExpander && infoExpander.isConnected && !infoExpander.closest('#right-tabs #tab-info') ) { document.querySelector('#tab-info').assignChildren111(null, infoExpander, null); } else { // if (infoExpander && ytdFlexyElm && shouldFixInfo) { // shouldFixInfo = false; // Promise.resolve(lockSet['infoFixLock']).then(infoFix).catch(console.warn); // } } const commentsArea = elements.comments; if (commentsArea) { const isConnected = commentsArea.isConnected; if (isConnected && !commentsArea.closest('#right-tabs #tab-comments')) { const tab = document.querySelector('#tab-comments'); tab.assignChildren111(null, commentsArea, null); } else { // if (!isConnected || tab.classList.contains('tab-content-hidden')) removeKeepCommentsScroller(); } } }, 'yt-navigate-finish': _evt => { const ytdAppElm = document.querySelector( 'ytd-page-manager#page-manager.style-scope.ytd-app' ); const ytdAppCnt = insp(ytdAppElm); pageType = ytdAppCnt ? (ytdAppCnt.data || 0).page : null; if (!document.querySelector('ytd-watch-flexy #player')) return; // shouldFixInfo = true; // console.log('yt-navigate-finish') const flexyArr = [...document.querySelectorAll('ytd-watch-flexy')].filter( e => !e.closest('[hidden]') && e.querySelector('#player') ); if (flexyArr.length === 1) { // const lockId = lockSet['yt-navigate-finish-videos']; elements.flexy = flexyArr[0]; if (isRightTabsInserted) { Promise.resolve(lockSet['refreshSecondaryInnerLock']) .then(eventMap['refreshSecondaryInner']) .catch(console.warn); Promise.resolve(lockSet['removeKeepCommentsScrollerLock']) .then(removeKeepCommentsScroller) .catch(console.warn); } else { navigateFinishedPromise.resolve(); if (plugin.minibrowser.toUse) plugin.minibrowser.activate(); if (plugin.autoExpandInfoDesc.toUse) plugin.autoExpandInfoDesc.activate(); if (plugin.fullChannelNameOnHover.toUse) plugin.fullChannelNameOnHover.activate(); } const chat = elements.chat; if (chat instanceof Element) { chat.setAttribute111('tyt-active-chat-frame', 'CF'); // chat and flexy ready } const infoExpander = elements.infoExpander; if (infoExpander && infoExpander.closest('#right-tabs')) { Promise.resolve(lockSet['infoFixLock']).then(infoFix).catch(console.warn); } Promise.resolve(lockSet['layoutFixLock']).then(layoutFix); if (plugin.fullChannelNameOnHover.activated) { plugin.fullChannelNameOnHover.onNavigateFinish(); } } }, onceInsertRightTabs: () => { // if(lockId !== lockGet['yt-navigate-finish-videos']) return; const related = elements.related; let rightTabs = document.querySelector('#right-tabs'); if (!document.querySelector('#right-tabs') && related) { getLangForPage(); const docTmp = document.createElement('template'); docTmp.innerHTML = createHTML(getTabsHTML()); const newElm = docTmp.content.firstElementChild; if (newElm !== null) { inPageRearrange = true; related.parentNode.insertBefore000(newElm, related); inPageRearrange = false; } rightTabs = newElm; rightTabs .querySelector('[tyt-tab-content="#tab-comments"]') .classList.add('tab-btn-hidden'); const secondaryWrapper = document.createElement('secondary-wrapper'); const secondaryInner = document.querySelector( '#secondary-inner.style-scope.ytd-watch-flexy' ); inPageRearrange = true; secondaryWrapper.replaceChildren000(...secondaryInner.childNodes); secondaryInner.insertBefore000(secondaryWrapper, secondaryInner.firstChild); inPageRearrange = false; rightTabs .querySelector('#material-tabs') .addEventListener('click', eventMap['tabs-btn-click'], true); inPageRearrange = true; if (!rightTabs.closest('secondary-wrapper')) secondaryWrapper.appendChild000(rightTabs); inPageRearrange = false; } if (rightTabs) { isRightTabsInserted = true; const ioTabBtns = new IntersectionObserver( entries => { for (const entry of entries) { const rect = entry.boundingClientRect; entry.target.classList.toggle('tab-btn-visible', rect.width && rect.height); } }, { rootMargin: '0px' } ); for (const btn of document.querySelectorAll('.tab-btn[tyt-tab-content]')) { ioTabBtns.observe(btn); } if (!related.closest('#right-tabs')) { document.querySelector('#tab-videos').assignChildren111(null, related, null); } const infoExpander = elements.infoExpander; if (infoExpander && !infoExpander.closest('#right-tabs')) { document.querySelector('#tab-info').assignChildren111(null, infoExpander, null); } const commentsArea = elements.comments; if (commentsArea && !commentsArea.closest('#right-tabs')) { document.querySelector('#tab-comments').assignChildren111(null, commentsArea, null); } rightTabsProvidedPromise.resolve(); roRightTabs.disconnect(); roRightTabs.observe(rightTabs); const ytdFlexyElm = elements.flexy; const aoFlexy = new MutationObserver(eventMap['aoFlexyFn']); aoFlexy.observe(ytdFlexyElm, { attributes: true }); // Promise.resolve(lockSet['tabsStatusCorrectionLock']).then(eventMap['tabsStatusCorrection']).catch(console.warn); Promise.resolve(lockSet['fixInitialTabStateLock']) .then(eventMap['fixInitialTabStateFn']) .catch(console.warn); ytdFlexyElm.incAttribute111('attr-7qlsy'); // tabsStatusCorrectionLock and video-id } }, aoFlexyFn: () => { Promise.resolve(lockSet['checkCommentsShouldBeHiddenLock']) .then(eventMap['checkCommentsShouldBeHidden']) .catch(console.warn); Promise.resolve(lockSet['refreshSecondaryInnerLock']) .then(eventMap['refreshSecondaryInner']) .catch(console.warn); Promise.resolve(lockSet['tabsStatusCorrectionLock']) .then(eventMap['tabsStatusCorrection']) .catch(console.warn); const videoId = getCurrentVideoId(); if (videoId !== tmpLastVideoId) { tmpLastVideoId = videoId; Promise.resolve(lockSet['updateOnVideoIdChangedLock']) .then(eventMap['updateOnVideoIdChanged']) .catch(console.warn); } }, twoColumnChanged10: lockId => { if (lockId !== lockGet['twoColumnChanged10Lock']) return; for (const continuation of document.querySelectorAll( '#tab-videos ytd-watch-next-secondary-results-renderer ytd-continuation-item-renderer' )) { if (continuation.closest('[hidden]')) continue; const cnt = insp(continuation); if (typeof cnt.showButton === 'boolean') { if (cnt.showButton === false) continue; cnt.showButton = false; const behavior = cnt.ytRendererBehavior || cnt; if (typeof behavior.invalidate === 'function') { behavior.invalidate(!1); } } } }, tabsStatusCorrection: lockId => { if (lockId !== lockGet['tabsStatusCorrectionLock']) return; const ytdFlexyElm = elements.flexy; if (!ytdFlexyElm) return; const p = tabAStatus; const q = calculationFn(p, 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128); let resetForPanelDisappeared = false; if (p !== q) { console.log(388, p, q); let actioned = false; if ((p & 128) === 0 && (q & 128) === 128) { lastPanel = 'playlist'; } else if ((p & 8) === 0 && (q & 8) === 8) { lastPanel = 'chat'; } else if ( (((p & 4) === 4 && (q & (4 | 8)) === (0 | 0)) || ((p & 8) === 8 && (q & (4 | 8)) === (0 | 0))) && lastPanel === 'chat' ) { // 24 -> 16 = -8; 'd' lastPanel = lastTab || ''; resetForPanelDisappeared = true; } else if ((p & (4 | 8)) === 8 && (q & (4 | 8)) === 4 && lastPanel === 'chat') { // click close lastPanel = lastTab || ''; resetForPanelDisappeared = true; } else if ((p & 128) === 128 && (q & 128) === 0 && lastPanel === 'playlist') { lastPanel = lastTab || ''; resetForPanelDisappeared = true; } tabAStatus = q; let bFixForResizedTab = false; if ((q ^ 2) === 2 && bFixForResizedTabLater) { bFixForResizedTab = true; } if (((p & 16) === 16) & ((q & 16) === 0)) { Promise.resolve(lockSet['twoColumnChanged10Lock']) .then(eventMap['twoColumnChanged10']) .catch(console.warn); } if (((p & 2) === 2) ^ ((q & 2) === 2) && (q & 2) === 2) { bFixForResizedTab = true; } // p->q +2 if ((p & 2) === 0 && (q & 2) === 2 && (p & 128) === 128 && (q & 128) === 128) { lastPanel = lastTab || ''; ytBtnClosePlaylist(); actioned = true; } // p->q +8 if ( (p & (8 | 128)) === (0 | 128) && (q & (8 | 128)) === (8 | 128) && lastPanel === 'chat' ) { lastPanel = lastTab || ''; ytBtnClosePlaylist(); actioned = true; } // p->q +128 if ( (p & (2 | 128)) === (2 | 0) && (q & (2 | 128)) === (2 | 128) && lastPanel === 'playlist' ) { switchToTab(null); actioned = true; } // p->q +128 if ( (p & (8 | 128)) === (8 | 0) && (q & (8 | 128)) === (8 | 128) && lastPanel === 'playlist' ) { lastPanel = lastTab || ''; ytBtnCollapseChat(); actioned = true; } // p->q +128 if ((p & (1 | 16 | 128)) === (1 | 16) && (q & (1 | 16 | 128)) === (1 | 16 | 128)) { ytBtnCancelTheater(); actioned = true; } // p->q +1 if ((p & (1 | 16 | 128)) === (16 | 128) && (q & (1 | 16 | 128)) === (1 | 16 | 128)) { lastPanel = lastTab || ''; ytBtnClosePlaylist(); actioned = true; } if ((q & 64) === 64) { actioned = false; } else if ((p & 64) === 64 && (q & 64) === 0) { // p->q -64 if ((q & 32) === 32) { ytBtnCloseEngagementPanels(); } if ((q & (2 | 8)) === (2 | 8)) { if (lastPanel === 'chat') { switchToTab(null); actioned = true; } else if (lastPanel) { ytBtnCollapseChat(); actioned = true; } } } else if ( (p & (1 | 2 | 8 | 16 | 32)) === (1 | 0 | 0 | 16 | 0) && (q & (1 | 2 | 8 | 16 | 32)) === (1 | 0 | 8 | 16 | 0) ) { // p->q +8 ytBtnCancelTheater(); actioned = true; } else if ( (p & (1 | 16 | 32)) === (0 | 16 | 0) && (q & (1 | 16 | 32)) === (0 | 16 | 32) && (q & (2 | 8)) > 0 ) { // p->q +32 if (q & 2) { switchToTab(null); actioned = true; } if (q & 8) { ytBtnCollapseChat(); actioned = true; } } else if ( (p & (1 | 16 | 8 | 2)) === (16 | 8) && (q & (1 | 16 | 8 | 2)) === 16 && (q & 128) === 0 ) { // p->q -8 if (lastTab) { switchToTab(lastTab); actioned = true; } } else if ((p & 1) === 0 && (q & 1) === 1) { // p->q +1 if ((q & 32) === 32) { ytBtnCloseEngagementPanels(); } if ((p & 9) === 8 && (q & 9) === 9) { ytBtnCollapseChat(); } switchToTab(null); actioned = true; } else if ((p & 3) === 1 && (q & 3) === 3) { // p->q +2 ytBtnCancelTheater(); actioned = true; } else if ((p & 10) === 2 && (q & 10) === 10) { // p->q +8 switchToTab(null); actioned = true; } else if ((p & (8 | 32)) === (0 | 32) && (q & (8 | 32)) === (8 | 32)) { // p->q +8 ytBtnCloseEngagementPanels(); actioned = true; } else if ((p & (2 | 32)) === (0 | 32) && (q & (2 | 32)) === (2 | 32)) { // p->q +2 ytBtnCloseEngagementPanels(); actioned = true; } else if ((p & (2 | 8)) === (0 | 8) && (q & (2 | 8)) === (2 | 8)) { // p->q +2 ytBtnCollapseChat(); actioned = true; // if( lastPanel && (p & (1|16) === 16) && (q & (1 | 16 | 8 | 2)) === (16) ){ // switchToTab(lastTab) // actioned = true; // } } else if ((p & 1) === 1 && (q & (1 | 32)) === (0 | 0)) { // p->q -1 if (lastPanel === 'chat') { ytBtnExpandChat(); actioned = true; } else if (lastPanel === lastTab && lastTab) { switchToTab(lastTab); actioned = true; } } // 24 20 // 8 16 4 16 if (!actioned && (q & 128) === 128) { lastPanel = 'playlist'; if ((q & 2) === 2) { switchToTab(null); actioned = true; } } if ((p & 2) === 2 && (q & (2 | 128)) === (0 | 128)) { // p->q -2 } else if ((p & 8) === 8 && (q & (8 | 128)) === (0 | 128)) { // p->q -8 } else if ( !actioned && (p & (1 | 16)) === 16 && (q & (1 | 16 | 8 | 2 | 32 | 64)) === (16 | 0 | 0) ) { console.log(388, 'd'); if (lastPanel === 'chat') { console.log(388, 'd1c'); ytBtnExpandChat(); actioned = true; } else if (lastPanel === 'playlist') { console.log(388, 'd1p'); ytBtnOpenPlaylist(); actioned = true; } else if (lastTab) { console.log(388, 'd2t'); switchToTab(lastTab); actioned = true; } else if (resetForPanelDisappeared) { // if lastTab is undefined console.log(388, 'd2d'); Promise.resolve(lockSet['fixInitialTabStateLock']) .then(eventMap['fixInitialTabStateFn']) .catch(console.warn); actioned = true; } } if (bFixForResizedTab) { bFixForResizedTabLater = false; Promise.resolve(0).then(eventMap['fixForTabDisplay']).catch(console.warn); } if (((p & 16) === 16) ^ ((q & 16) === 16)) { Promise.resolve(lockSet['infoFixLock']).then(infoFix).catch(console.warn); Promise.resolve(lockSet['removeKeepCommentsScrollerLock']) .then(removeKeepCommentsScroller) .catch(console.warn); Promise.resolve(lockSet['layoutFixLock']).then(layoutFix).catch(console.warn); } } }, updateOnVideoIdChanged: lockId => { if (lockId !== lockGet['updateOnVideoIdChangedLock']) return; const videoId = tmpLastVideoId; if (!videoId) return; const bodyRenderer = elements.infoExpanderRendererBack; const bodyRendererNew = elements.infoExpanderRendererFront; if (bodyRendererNew && bodyRenderer) { insp(bodyRendererNew).data = insp(bodyRenderer).data; // if ((bodyRendererNew.hasAttribute('hidden') ? 1 : 0) ^ (bodyRenderer.hasAttribute('hidden') ? 1 : 0)) { // if (bodyRenderer.hasAttribute('hidden')) bodyRendererNew.setAttribute('hidden', ''); // else bodyRendererNew.removeAttribute('hidden'); // } } Promise.resolve(lockSet['infoFixLock']).then(infoFix).catch(console.warn); }, fixInitialTabStateFn: async lockId => { // console.log('fixInitialTabStateFn 0a'); if (lockGet['fixInitialTabStateLock'] !== lockId) return; // console.log('fixInitialTabStateFn 0b'); const delayTime = fixInitialTabStateK > 0 ? 200 : 1; await delayPn(delayTime); if (lockGet['fixInitialTabStateLock'] !== lockId) return; // console.log('fixInitialTabStateFn 0c'); const kTab = document.querySelector('[tyt-tab]'); const qTab = !kTab || kTab.getAttribute('tyt-tab') === '' ? checkElementExist('ytd-watch-flexy[is-two-columns_]', '[hidden]') : null; if (checkElementExist('ytd-playlist-panel-renderer#playlist', '[hidden], [collapsed]')) { DEBUG_5085 && console.log('fixInitialTabStateFn 1p'); switchToTab(null); } else if (checkElementExist('ytd-live-chat-frame#chat', '[hidden], [collapsed]')) { DEBUG_5085 && console.log('fixInitialTabStateFn 1a'); switchToTab(null); if (checkElementExist('ytd-watch-flexy[theater]', '[hidden]')) { ytBtnCollapseChat(); } } else if (qTab) { const hasTheater = qTab.hasAttribute('theater'); if (!hasTheater) { DEBUG_5085 && console.log('fixInitialTabStateFn 1b'); const btn0 = document.querySelector('.tab-btn-visible'); // or default button if (btn0) { switchToTab(btn0); } else { switchToTab(null); } } else { DEBUG_5085 && console.log('fixInitialTabStateFn 1c'); switchToTab(null); } } else { DEBUG_5085 && console.log('fixInitialTabStateFn 1z'); } // console.log('fixInitialTabStateFn 0d'); fixInitialTabStateK++; }, 'tabs-btn-click': evt => { const target = evt.target; if ( target instanceof HTMLElement_ && target.classList.contains('tab-btn') && target.hasAttribute000('tyt-tab-content') ) { evt.preventDefault(); evt.stopPropagation(); evt.stopImmediatePropagation(); const activeLink = target; switchToTab(activeLink); } }, }; Promise.all([videosElementProvidedPromise, navigateFinishedPromise]) .then(eventMap['onceInsertRightTabs']) .catch(console.warn); Promise.all([navigateFinishedPromise, infoExpanderElementProvidedPromise]) .then(eventMap['onceInfoExpanderElementProvidedPromised']) .catch(console.warn); const isCustomElementsProvided = typeof customElements !== 'undefined' && typeof (customElements || 0).whenDefined === 'function'; const promiseForCustomYtElementsReady = isCustomElementsProvided ? Promise.resolve(0) : new Promise(callback => { const EVENT_KEY_ON_REGISTRY_READY = 'ytI-ce-registry-created'; if (typeof customElements === 'undefined') { if (!('__CE_registry' in document)) { // https://github.com/webcomponents/polyfills/ Object.defineProperty(document, '__CE_registry', { get() { // return undefined }, set(nv) { if (typeof nv == 'object') { delete this.__CE_registry; this.__CE_registry = nv; this.dispatchEvent(new CustomEvent(EVENT_KEY_ON_REGISTRY_READY)); } return true; }, enumerable: false, configurable: true, }); } let eventHandler = _evt => { document.removeEventListener(EVENT_KEY_ON_REGISTRY_READY, eventHandler, false); const f = callback; callback = null; eventHandler = null; f(); }; document.addEventListener(EVENT_KEY_ON_REGISTRY_READY, eventHandler, false); } else { callback(); } }); // eslint-disable-next-line no-unused-vars const _retrieveCE = async nodeName => { try { isCustomElementsProvided || (await promiseForCustomYtElementsReady); await customElements.whenDefined(nodeName); } catch (e) { console.warn(e); } }; const retrieveCE = async nodeName => { try { isCustomElementsProvided || (await promiseForCustomYtElementsReady); await customElements.whenDefined(nodeName); const dummy = document.querySelector(nodeName) || document.createElement(nodeName); const cProto = insp(dummy).constructor.prototype; return cProto; } catch (e) { console.warn(e); } }; const moOverallRes = { _yt_playerProvided: () => (window || 0)._yt_player || 0 || 0, }; let promiseWaitNext = null; const moOverall = new MutationObserver(() => { if (promiseWaitNext) { promiseWaitNext.resolve(); promiseWaitNext = null; } if (typeof moOverallRes._yt_playerProvided === 'function') { const r = moOverallRes._yt_playerProvided(); if (r) { moOverallRes._yt_playerProvided = r; eventMap._yt_playerProvided(); } } }); moOverall.observe(document, { subtree: true, childList: true }); const moEgmPanelReady = new MutationObserver(mutations => { for (const mutation of mutations) { const target = mutation.target; if (!target.hasAttribute000('tyt-egm-panel-jclmd')) continue; if (target.hasAttribute000('target-id') && target.hasAttribute000('visibility')) { target.removeAttribute000('tyt-egm-panel-jclmd'); moEgmPanelReadyClearFn(); Promise.resolve(target) .then(eventMap['ytd-engagement-panel-section-list-renderer::bindTarget']) .catch(console.warn); } } }); const moEgmPanelReadyClearFn = () => { if (document.querySelector('[tyt-egm-panel-jclmd]') === null) { moEgmPanelReady.takeRecords(); moEgmPanelReady.disconnect(); } }; document.addEventListener('yt-navigate-finish', eventMap['yt-navigate-finish'], false); document.addEventListener( 'animationstart', evt => { const f = eventMap[evt.animationName]; if (typeof f === 'function') f(evt.target); }, capturePassive ); // console.log('hi122') mLoaded.flag |= 1; document.documentElement.setAttribute111('tabview-loaded', mLoaded.makeString()); promiseForCustomYtElementsReady.then(eventMap['ceHack']).catch(console.warn); // eslint-disable-next-line no-unused-vars executionFinished = 1; } catch (e) { console.log('error 0xF491'); console.error(e); } }; const styles = { main: ` @keyframes relatedElementProvided{0%{background-position-x:3px;}100%{background-position-x:4px;}} html[tabview-loaded="icp"] #related.ytd-watch-flexy{animation:relatedElementProvided 1ms linear 0s 1 normal forwards;} html[tabview-loaded="icp"] #right-tabs #related.ytd-watch-flexy,html[tabview-loaded="icp"] [hidden] #related.ytd-watch-flexy,html[tabview-loaded="icp"] #right-tabs ytd-expander#expander,html[tabview-loaded="icp"] [hidden] ytd-expander#expander,html[tabview-loaded="icp"] ytd-comments ytd-expander#expander{animation:initial;} #secondary.ytd-watch-flexy{position:relative;} #secondary-inner.style-scope.ytd-watch-flexy{height:100%;} #secondary-inner secondary-wrapper{display:flex;flex-direction:column;flex-wrap:nowrap;box-sizing:border-box;padding:0;margin:0;border:0;height:100%;max-height:calc(100vh - var(--ytd-toolbar-height,56px));position:absolute;top:0;right:0;left:0;contain:strict;padding:var(--ytd-margin-6x) var(--ytd-margin-6x) var(--ytd-margin-6x) 0;} #right-tabs{position:relative;display:flex;padding:0;margin:0;flex-grow:1;flex-direction:column;} [tyt-tab=""] #right-tabs{flex-grow:0;} [tyt-tab=""] #right-tabs .tab-content{border:0;} #right-tabs .tab-content{flex-grow:1;} ytd-watch-flexy[hide-default-text-inline-expander] #primary.style-scope.ytd-watch-flexy ytd-text-inline-expander{display:none;} ytd-watch-flexy:not([keep-comments-scroller]) #tab-comments.tab-content-hidden{--comment-pre-load-sizing:90px;visibility:collapse;z-index:-1;position:fixed!important;left:2px;top:2px;width:var(--comment-pre-load-sizing)!important;height:var(--comment-pre-load-sizing)!important;display:block!important;pointer-events:none!important;overflow:hidden;contain:strict;border:0;margin:0;padding:0;} ytd-watch-flexy:not([keep-comments-scroller]) #tab-comments.tab-content-hidden ytd-comments#comments>ytd-item-section-renderer#sections{display:block!important;overflow:hidden;height:var(--comment-pre-load-sizing);width:var(--comment-pre-load-sizing);contain:strict;border:0;margin:0;padding:0;} ytd-watch-flexy:not([keep-comments-scroller]) #tab-comments.tab-content-hidden ytd-comments#comments>ytd-item-section-renderer#sections>#contents{display:flex!important;flex-direction:row;gap:60px;overflow:hidden;height:var(--comment-pre-load-sizing);width:var(--comment-pre-load-sizing);contain:strict;border:0;margin:0;padding:0;} ytd-watch-flexy:not([keep-comments-scroller]) #tab-comments.tab-content-hidden ytd-comments#comments #contents{--comment-pre-load-display:none;} ytd-watch-flexy:not([keep-comments-scroller]) #tab-comments.tab-content-hidden ytd-comments#comments #contents>*:only-of-type,ytd-watch-flexy:not([keep-comments-scroller]) #tab-comments.tab-content-hidden ytd-comments#comments #contents>*:last-child{--comment-pre-load-display:block;} ytd-watch-flexy:not([keep-comments-scroller]) #tab-comments.tab-content-hidden ytd-comments#comments #contents>*{display:var(--comment-pre-load-display)!important;} #right-tabs #material-tabs{position:relative;display:flex;padding:0;border:1px solid var(--ytd-searchbox-legacy-border-color);overflow:hidden;} [tyt-tab] #right-tabs #material-tabs{border-radius:12px;} [tyt-tab^="#"] #right-tabs #material-tabs{border-radius:12px 12px 0 0;} ytd-watch-flexy:not([is-two-columns_]) #right-tabs #material-tabs{outline:0;} #right-tabs #material-tabs a.tab-btn[tyt-tab-content]>*{pointer-events:none;} #right-tabs #material-tabs a.tab-btn[tyt-tab-content]>.font-size-right{pointer-events:initial;display:none;} ytd-watch-flexy #right-tabs .tab-content{padding:0;box-sizing:border-box;display:block;border:1px solid var(--ytd-searchbox-legacy-border-color);border-top:0;position:relative;top:0;display:flex;flex-direction:row;overflow:hidden;border-radius:0 0 12px 12px;} ytd-watch-flexy:not([is-two-columns_]) #right-tabs .tab-content{height:100%;} ytd-watch-flexy #right-tabs .tab-content-cld{box-sizing:border-box;position:relative;display:block;width:100%;overflow:auto;--tab-content-padding:var(--ytd-margin-4x);padding:var(--tab-content-padding);contain:layout paint;} .tab-content-cld,#right-tabs,.tab-content{transition:none;animation:none;} #right-tabs #emojis.ytd-commentbox{inset:auto 0 auto 0;width:auto;} ytd-watch-flexy[is-two-columns_] #right-tabs .tab-content-cld{height:100%;width:100%;contain:size layout paint style;position:absolute;} ytd-watch-flexy #right-tabs .tab-content-cld.tab-content-hidden{display:none;width:100%;contain:size layout paint style;} @supports (color:var(--tabview-tab-btn-define)){ ytd-watch-flexy #right-tabs .tab-btn{background:var(--yt-spec-general-background-a);} html{--tyt-tab-btn-flex-grow:1;--tyt-tab-btn-flex-basis:0%;--tyt-tab-bar-color-1-def:#ff4533;--tyt-tab-bar-color-2-def:var(--yt-brand-light-red);--tyt-tab-bar-color-1:var(--main-color,var(--tyt-tab-bar-color-1-def));--tyt-tab-bar-color-2:var(--main-color,var(--tyt-tab-bar-color-2-def));} ytd-watch-flexy #right-tabs .tab-btn[tyt-tab-content]{flex:var(--tyt-tab-btn-flex-grow) 1 var(--tyt-tab-btn-flex-basis);position:relative;display:inline-block;text-decoration:none;text-transform:uppercase;--tyt-tab-btn-color:var(--yt-spec-text-secondary);color:var(--tyt-tab-btn-color);text-align:center;padding:14px 8px 10px;border:0;border-bottom:4px solid transparent;font-weight:500;font-size:12px;line-height:18px;cursor:pointer;transition:border 200ms linear 100ms;background-color:var(--ytd-searchbox-legacy-button-color);text-transform:var(--yt-button-text-transform,inherit);user-select:none!important;overflow:hidden;white-space:nowrap;text-overflow:clip;} ytd-watch-flexy #right-tabs .tab-btn[tyt-tab-content]>svg{height:18px;padding-right:0;vertical-align:bottom;opacity:.5;margin-right:0;color:var(--yt-button-color,inherit);fill:var(--iron-icon-fill-color,currentcolor);stroke:var(--iron-icon-stroke-color,none);pointer-events:none;} ytd-watch-flexy #right-tabs .tab-btn{--tabview-btn-txt-ml:8px;} ytd-watch-flexy[tyt-comment-disabled] #right-tabs .tab-btn[tyt-tab-content="#tab-comments"]{--tabview-btn-txt-ml:0;} ytd-watch-flexy #right-tabs .tab-btn[tyt-tab-content]>svg+span{margin-left:var(--tabview-btn-txt-ml);} ytd-watch-flexy #right-tabs .tab-btn[tyt-tab-content].active{font-weight:500;outline:0;--tyt-tab-btn-color:var(--yt-spec-text-primary);background-color:var(--ytd-searchbox-legacy-button-focus-color);border-bottom:2px var(--tyt-tab-bar-color-2) solid;} ytd-watch-flexy #right-tabs .tab-btn[tyt-tab-content].active svg{opacity:.9;} ytd-watch-flexy #right-tabs .tab-btn[tyt-tab-content]:not(.active):hover{background-color:var(--ytd-searchbox-legacy-button-hover-color);--tyt-tab-btn-color:var(--yt-spec-text-primary);} ytd-watch-flexy #right-tabs .tab-btn[tyt-tab-content]:not(.active):hover svg{opacity:.9;} ytd-watch-flexy #right-tabs .tab-btn[tyt-tab-content].tab-btn-hidden{display:none;} ytd-watch-flexy[tyt-comment-disabled] #right-tabs .tab-btn[tyt-tab-content="#tab-comments"],ytd-watch-flexy[tyt-comment-disabled] #right-tabs .tab-btn[tyt-tab-content="#tab-comments"]:hover{--tyt-tab-btn-color:var(--yt-spec-icon-disabled);} ytd-watch-flexy[tyt-comment-disabled] #right-tabs .tab-btn[tyt-tab-content="#tab-comments"] span#tyt-cm-count:empty{display:none;} ytd-watch-flexy #right-tabs .tab-btn span#tyt-cm-count:empty::after{display:inline-block;width:4em;text-align:left;font-size:inherit;color:currentColor;transform:scaleX(.8);} } @supports (color:var(--tyt-cm-count-define)){ ytd-watch-flexy{--tyt-x-loading-content-letter-spacing:2px;} html{--tabview-text-loading:"Loading";--tabview-text-fetching:"Fetching";--tabview-panel-loading:var(--tabview-text-loading);} html:lang(ja){--tabview-text-loading:"読み込み中";--tabview-text-fetching:"フェッチ..";} html:lang(ko){--tabview-text-loading:"로딩..";--tabview-text-fetching:"가져오기..";} html:lang(zh-Hant){--tabview-text-loading:"載入中";--tabview-text-fetching:"擷取中";} html:lang(zh-Hans){--tabview-text-loading:"加载中";--tabview-text-fetching:"抓取中";} html:lang(ru){--tabview-text-loading:"Загрузка";--tabview-text-fetching:"Получение";} ytd-watch-flexy #right-tabs .tab-btn span#tyt-cm-count:empty::after{content:var(--tabview-text-loading);letter-spacing:var(--tyt-x-loading-content-letter-spacing);} } @supports (color:var(--tabview-font-size-btn-define)){ .font-size-right{display:inline-flex;flex-direction:column;position:absolute;right:0;top:0;bottom:0;width:16px;padding:4px 0;justify-content:space-evenly;align-content:space-evenly;pointer-events:none;} html body ytd-watch-flexy.style-scope .font-size-btn{user-select:none!important;} .font-size-btn{--tyt-font-size-btn-display:none;display:var(--tyt-font-size-btn-display,none);width:12px;height:12px;color:var(--yt-spec-text-secondary);background-color:var(--yt-spec-badge-chip-background);box-sizing:border-box;cursor:pointer;transform-origin:left top;margin:0;padding:0;position:relative;font-family:'Menlo','Lucida Console','Monaco','Consolas',monospace;line-height:100%;font-weight:900;transition:background-color 90ms linear,color 90ms linear;pointer-events:all;} .font-size-btn:hover{background-color:var(--yt-spec-text-primary);color:var(--yt-spec-general-background-a);} @supports (zoom:.5){ .tab-btn .font-size-btn{--tyt-font-size-btn-display:none;} .tab-btn.active:hover .font-size-btn{--tyt-font-size-btn-display:inline-block;} body ytd-watch-flexy:not([is-two-columns_]) #columns.ytd-watch-flexy{flex-direction:column;} body ytd-watch-flexy:not([is-two-columns_]) #secondary.ytd-watch-flexy{display:block;width:100%;box-sizing:border-box;} body ytd-watch-flexy:not([is-two-columns_]) #secondary.ytd-watch-flexy secondary-wrapper{padding-left:var(--ytd-margin-6x);contain:content;height:initial;} body ytd-watch-flexy:not([is-two-columns_]) #secondary.ytd-watch-flexy secondary-wrapper #right-tabs{overflow:auto;} [tyt-chat="+"] secondary-wrapper>[tyt-chat-container]{flex-grow:1;flex-shrink:0;display:flex;flex-direction:column;} [tyt-chat="+"] secondary-wrapper>[tyt-chat-container]>#chat{flex-grow:1;} ytd-watch-flexy[is-two-columns_]:not([theater]) #columns.style-scope.ytd-watch-flexy{min-height:calc(100vh - var(--ytd-toolbar-height,56px));} ytd-watch-flexy[is-two-columns_] ytd-live-chat-frame#chat{min-height:initial!important;height:initial!important;} ytd-watch-flexy[tyt-tab^="#"]:not([is-two-columns_]):not([tyt-chat="+"]) #right-tabs{min-height:var(--ytd-watch-flexy-chat-max-height);} body ytd-watch-flexy:not([is-two-columns_]) #chat.ytd-watch-flexy{margin-top:0;} body ytd-watch-flexy:not([is-two-columns_]) ytd-watch-metadata.ytd-watch-flexy{margin-bottom:0;} ytd-watch-metadata.ytd-watch-flexy ytd-metadata-row-container-renderer{display:none;} #tab-info [show-expand-button] #expand-sizer.ytd-text-inline-expander{visibility:initial;} #tab-info #social-links.style-scope.ytd-video-description-infocards-section-renderer>#left-arrow-container.ytd-video-description-infocards-section-renderer>#left-arrow,#tab-info #social-links.style-scope.ytd-video-description-infocards-section-renderer>#right-arrow-container.ytd-video-description-infocards-section-renderer>#right-arrow{border:6px solid transparent;opacity:.65;} #tab-info #social-links.style-scope.ytd-video-description-infocards-section-renderer>#left-arrow-container.ytd-video-description-infocards-section-renderer>#left-arrow:hover,#tab-info #social-links.style-scope.ytd-video-description-infocards-section-renderer>#right-arrow-container.ytd-video-description-infocards-section-renderer>#right-arrow:hover{opacity:1;} #tab-info #social-links.style-scope.ytd-video-description-infocards-section-renderer>div#left-arrow-container::before{content:'';background:transparent;width:40px;display:block;height:40px;position:absolute;left:-20px;top:0;z-index:-1;} #tab-info #social-links.style-scope.ytd-video-description-infocards-section-renderer>div#right-arrow-container::before{content:'';background:transparent;width:40px;display:block;height:40px;position:absolute;right:-20px;top:0;z-index:-1;} body ytd-watch-flexy[is-two-columns_][tyt-egm-panel_] #columns.style-scope.ytd-watch-flexy #panels.style-scope.ytd-watch-flexy{flex-grow:1;flex-shrink:0;display:flex;flex-direction:column;} body ytd-watch-flexy[is-two-columns_][tyt-egm-panel_] #columns.style-scope.ytd-watch-flexy #panels.style-scope.ytd-watch-flexy ytd-engagement-panel-section-list-renderer[target-id][visibility="ENGAGEMENT_PANEL_VISIBILITY_EXPANDED"]{height:initial;max-height:initial;min-height:initial;flex-grow:1;flex-shrink:0;display:flex;flex-direction:column;} secondary-wrapper [visibility="ENGAGEMENT_PANEL_VISIBILITY_EXPANDED"] ytd-transcript-renderer:not(:empty),secondary-wrapper [visibility="ENGAGEMENT_PANEL_VISIBILITY_EXPANDED"] #body.ytd-transcript-renderer:not(:empty),secondary-wrapper [visibility="ENGAGEMENT_PANEL_VISIBILITY_EXPANDED"] #content.ytd-transcript-renderer:not(:empty){flex-grow:1;height:initial;max-height:initial;min-height:initial;} secondary-wrapper #content.ytd-engagement-panel-section-list-renderer{position:relative;} secondary-wrapper #content.ytd-engagement-panel-section-list-renderer>[panel-target-id]:only-child{contain:style size;} secondary-wrapper #content.ytd-engagement-panel-section-list-renderer ytd-transcript-segment-list-renderer.ytd-transcript-search-panel-renderer{flex-grow:1;contain:strict;} secondary-wrapper #content.ytd-engagement-panel-section-list-renderer ytd-transcript-segment-renderer.style-scope.ytd-transcript-segment-list-renderer{contain:layout paint style;} secondary-wrapper #content.ytd-engagement-panel-section-list-renderer ytd-transcript-segment-renderer.style-scope.ytd-transcript-segment-list-renderer>.segment{contain:layout paint style;} body ytd-watch-flexy[theater] #secondary.ytd-watch-flexy{margin-top:var(--ytd-margin-3x);padding-top:0;} body ytd-watch-flexy[theater] secondary-wrapper{margin-top:0;padding-top:0;} body ytd-watch-flexy[theater] #chat.ytd-watch-flexy{margin-bottom:var(--ytd-margin-2x);} ytd-watch-flexy[theater] #right-tabs .tab-btn[tyt-tab-content]{padding:8px 4px 6px;border-bottom:0 solid transparent;} ytd-watch-flexy[theater] #playlist.ytd-watch-flexy{margin-bottom:var(--ytd-margin-2x);} ytd-watch-flexy[theater] ytd-playlist-panel-renderer[collapsible][collapsed] .header.ytd-playlist-panel-renderer{padding:6px 8px;} #tab-comments ytd-comments#comments [field-of-cm-count]{margin-top:0;} #tab-info>ytd-expandable-video-description-body-renderer{margin-bottom:var(--ytd-margin-3x);} #tab-info [class]:last-child{margin-bottom:0;padding-bottom:0;} #tab-info ytd-rich-metadata-row-renderer ytd-rich-metadata-renderer{max-width:initial;} ytd-watch-flexy[is-two-columns_] secondary-wrapper #chat.ytd-watch-flexy{margin-bottom:var(--ytd-margin-3x);} ytd-watch-flexy[tyt-tab] tp-yt-paper-tooltip{white-space:nowrap;contain:content;} ytd-watch-info-text tp-yt-paper-tooltip.style-scope.ytd-watch-info-text{margin-bottom:-300px;margin-top:-96px;} [hide-default-text-inline-expander] #bottom-row #description.ytd-watch-metadata{font-size:1.2rem;line-height:1.8rem;} [hide-default-text-inline-expander] #bottom-row #description.ytd-watch-metadata yt-animated-rolling-number{font-size:inherit;} [hide-default-text-inline-expander] #bottom-row #description.ytd-watch-metadata #info-container.style-scope.ytd-watch-info-text{align-items:center;} ytd-watch-flexy[hide-default-text-inline-expander]{--tyt-bottom-watch-metadata-margin:6px;} [hide-default-text-inline-expander] #bottom-row #description.ytd-watch-metadata>#description-inner.ytd-watch-metadata{margin:6px 12px;} [hide-default-text-inline-expander] ytd-watch-metadata[title-headline-xs] h1.ytd-watch-metadata{font-size:1.8rem;} ytd-watch-flexy[is-two-columns_][hide-default-text-inline-expander] #below.style-scope.ytd-watch-flexy ytd-merch-shelf-renderer{padding:0;border:0;margin:0;} ytd-watch-flexy[is-two-columns_][hide-default-text-inline-expander] #below.style-scope.ytd-watch-flexy ytd-watch-metadata.ytd-watch-flexy{margin-bottom:6px;} #tab-info yt-video-attribute-view-model .yt-video-attribute-view-model--horizontal .yt-video-attribute-view-model__link-container .yt-video-attribute-view-model__hero-section{flex-shrink:0;} #tab-info yt-video-attribute-view-model .yt-video-attribute-view-model__overflow-menu{background:var(--yt-emoji-picker-category-background-color);border-radius:99px;} #tab-info yt-video-attribute-view-model .yt-video-attribute-view-model--image-square.yt-video-attribute-view-model--image-large .yt-video-attribute-view-model__hero-section{max-height:128px;} #tab-info yt-video-attribute-view-model .yt-video-attribute-view-model--image-large .yt-video-attribute-view-model__hero-section{max-width:128px;} #tab-info ytd-reel-shelf-renderer #items.yt-horizontal-list-renderer ytd-reel-item-renderer.yt-horizontal-list-renderer{max-width:142px;} ytd-watch-info-text#ytd-watch-info-text.style-scope.ytd-watch-metadata #view-count.style-scope.ytd-watch-info-text,ytd-watch-info-text#ytd-watch-info-text.style-scope.ytd-watch-metadata #date-text.style-scope.ytd-watch-info-text{align-items:center;} ytd-watch-info-text:not([detailed]) #info.ytd-watch-info-text a.yt-simple-endpoint.yt-formatted-string{pointer-events:none;} body ytd-app>ytd-popup-container>tp-yt-iron-dropdown>#contentWrapper>[slot="dropdown-content"]{backdrop-filter:none;} #tab-info [tyt-clone-refresh-count]{overflow:visible!important;} #tab-info #items.ytd-horizontal-card-list-renderer yt-video-attribute-view-model.ytd-horizontal-card-list-renderer{contain:layout;} #tab-info #thumbnail-container.ytd-structured-description-channel-lockup-renderer,#tab-info ytd-media-lockup-renderer[is-compact] #thumbnail-container.ytd-media-lockup-renderer{flex-shrink:0;} secondary-wrapper ytd-donation-unavailable-renderer{--ytd-margin-6x:var(--ytd-margin-2x);--ytd-margin-5x:var(--ytd-margin-2x);--ytd-margin-4x:var(--ytd-margin-2x);--ytd-margin-3x:var(--ytd-margin-2x);} [tyt-no-less-btn] #less{display:none;} .tyt-metadata-hover-resized #purchase-button,.tyt-metadata-hover-resized #sponsor-button,.tyt-metadata-hover-resized #analytics-button,.tyt-metadata-hover-resized #subscribe-button{display:none!important;} .tyt-metadata-hover #upload-info{max-width:max-content;min-width:max-content;flex-basis:100vw;flex-shrink:0;} .tyt-info-invisible{display:none;} [tyt-playlist-expanded] secondary-wrapper>ytd-playlist-panel-renderer#playlist{overflow:auto;flex-shrink:1;flex-grow:1;max-height:unset!important;} [tyt-playlist-expanded] secondary-wrapper>ytd-playlist-panel-renderer#playlist>#container{max-height:unset!important;} secondary-wrapper ytd-playlist-panel-renderer{--ytd-margin-6x:var(--ytd-margin-3x);} #tab-info ytd-structured-description-playlist-lockup-renderer[collections] #playlist-thumbnail.style-scope.ytd-structured-description-playlist-lockup-renderer{max-width:100%;} #tab-info ytd-structured-description-playlist-lockup-renderer[collections] #lockup-container.ytd-structured-description-playlist-lockup-renderer{padding:1px;} #tab-info ytd-structured-description-playlist-lockup-renderer[collections] #thumbnail.ytd-structured-description-playlist-lockup-renderer{outline:1px solid rgba(127,127,127,.5);} ytd-live-chat-frame#chat[collapsed] ytd-message-renderer~#show-hide-button.ytd-live-chat-frame>ytd-toggle-button-renderer.ytd-live-chat-frame{padding:0;} ytd-watch-flexy{--tyt-bottom-watch-metadata-margin:12px;} ytd-watch-flexy[rounded-info-panel],ytd-watch-flexy[rounded-player-large]{--tyt-rounded-a1:12px;} #bottom-row.style-scope.ytd-watch-metadata .item.ytd-watch-metadata{margin-right:var(--tyt-bottom-watch-metadata-margin,12px);margin-top:var(--tyt-bottom-watch-metadata-margin,12px);} #cinematics{contain:layout style size;} ytd-watch-flexy[is-two-columns_]{contain:layout style;} .yt-spec-touch-feedback-shape--touch-response .yt-spec-touch-feedback-shape__fill{background-color:transparent;} `, }; (async () => { const communicationKey = `ck-${Date.now()}-${Math.floor(Math.random() * 314159265359 + 314159265359).toString(36)}`; /** @type {globalThis.PromiseConstructor} */ const Promise = (async () => { })().constructor; // YouTube hacks Promise in WaterFox Classic and "Promise.resolve(0)" nevers resolve. if (!document.documentElement) { await Promise.resolve(0); while (!document.documentElement) { await new Promise(resolve => nextBrowserTick(resolve)).then().catch(console.warn); } } const sourceURL = 'debug://tabview-youtube/tabview.execution.js'; const textContent = `(${executionScript})("${communicationKey}");${'\n\n'}//# sourceURL=${sourceURL}${'\n'}`; // Inject script using a script element with the page's nonce (if available) to comply with CSP let script = document.createElement('script'); // Try to get the nonce from an existing script on the page const existingScript = document.querySelector('script[nonce]'); if (existingScript && existingScript.nonce) { script.nonce = existingScript.nonce; } // Use TrustedTypes if available if (typeof trustedTypes !== 'undefined' && trustedTypes.defaultPolicy) { script.textContent = trustedTypes.defaultPolicy.createScript(textContent); } else { script.textContent = textContent; } (document.head || document.documentElement).appendChild(script); script.remove(); script = null; const style = document.createElement('style'); const sourceURLMainCSS = 'debug://tabview-youtube/tabview.main.css'; style.textContent = `${styles['main'].trim()}${'\n\n'}/*# sourceURL=${sourceURLMainCSS} */${'\n'}`; document.documentElement.appendChild(style); })(); // --- MODULE: basic.js --- const YouTubeUtils = (() => { 'use strict'; // Internationalization (i18n) system const i18n = { en: { // Settings modal settingsTitle: 'Settings', basicTab: 'Basic', advancedTab: 'Advanced', experimentalTab: 'Experimental', aboutTab: 'About', closeButton: 'Close', saveChanges: 'Save Changes', settingsSaved: 'Settings saved', // Basic settings speedControl: 'Speed Control', speedControlDesc: 'Add speed control buttons to video player', screenshotButton: 'Screenshot Button', screenshotButtonDesc: 'Add screenshot capture button to video player', downloadButton: 'Download Button', downloadButtonDesc: 'Add download button with multiple site options to video player', // Download customization customDownloader: 'Use custom downloader', alwaysEnabled: 'Always enabled - GitHub repository', siteName: 'Site name', urlTemplate: 'URL template (use {videoId} or {videoUrl})', saveButton: 'Save', resetButton: 'Reset', // Notifications y2mateSettingsSaved: 'Y2Mate settings saved', y2mateReset: 'Y2Mate reset to defaults', xbuddySettingsSaved: '9xBuddy settings saved', xbuddyReset: '9xBuddy reset to defaults', // Tooltips youtubeSettings: 'YouTube + Settings', takeScreenshot: 'Take screenshot', downloadOptions: 'Download options', // Download sites byYTDL: 'by YTDL', copiedToClipboard: 'Video URL copied to clipboard!' }, ru: { // Settings modal settingsTitle: 'Настройки', basicTab: 'Основные', advancedTab: 'Расширенные', experimentalTab: 'Экспериментальные', aboutTab: 'О программе', closeButton: 'Закрыть', saveChanges: 'Сохранить изменения', settingsSaved: 'Настройки сохранены', // Basic settings speedControl: 'Управление скоростью', speedControlDesc: 'Добавить кнопки управления скоростью в видеоплеер', screenshotButton: 'Кнопка скриншота', screenshotButtonDesc: 'Добавить кнопку создания скриншота в видеоплеер', downloadButton: 'Кнопка загрузки', downloadButtonDesc: 'Добавить кнопку загрузки с несколькими вариантами сайтов в видеоплеер', // Download customization customDownloader: 'Использовать свой загрузчик', alwaysEnabled: 'Всегда включено - репозиторий GitHub', siteName: 'Название сайта', urlTemplate: 'Шаблон URL (используйте {videoId} или {videoUrl})', saveButton: 'Сохранить', resetButton: 'Сбросить', // Notifications y2mateSettingsSaved: 'Настройки Y2Mate сохранены', y2mateReset: 'Y2Mate сброшен к значениям по умолчанию', xbuddySettingsSaved: 'Настройки 9xBuddy сохранены', xbuddyReset: '9xBuddy сброшен к значениям по умолчанию', // Tooltips youtubeSettings: 'Настройки YouTube +', takeScreenshot: 'Сделать скриншот', downloadOptions: 'Варианты загрузки', // Download sites byYTDL: 'by YTDL', copiedToClipboard: 'URL видео скопирован в буфер обмена!' } }; // Get browser language function getLanguage() { const lang = document.documentElement.lang || navigator.language || 'en'; return lang.startsWith('ru') ? 'ru' : 'en'; } // Translation function function t(key) { const lang = getLanguage(); return i18n[lang][key] || i18n.en[key] || key; } /** * Error logging with module context * @param {string} module - Module name * @param {string} message - Error message * @param {Error} error - Error object */ const logError = (module, message, error) => { console.error(`[YouTube+][${module}] ${message}:`, error); }; /** * Safe function wrapper with error handling * @param {Function} fn - Function to wrap * @param {string} context - Context for error logging * @returns {Function} Wrapped function */ const safeExecute = (fn, context = 'Unknown') => { /** @this {any} */ return function (...args) { try { return fn.apply(this, args); } catch (error) { logError(context, 'Execution failed', error); return null; } }; }; /** * Safe async function wrapper with error handling * @param {Function} fn - Async function to wrap * @param {string} context - Context for error logging * @returns {Function} Wrapped async function */ const safeExecuteAsync = (fn, context = 'Unknown') => { /** @this {any} */ return async function (...args) { try { return await fn.apply(this, args); } catch (error) { logError(context, 'Async execution failed', error); return null; } }; }; /** * Sanitize HTML string to prevent XSS * @param {string} html - HTML string to sanitize * @returns {string} Sanitized HTML */ const sanitizeHTML = html => { if (typeof html !== 'string') return ''; const map = { '<': '<', '>': '>', '&': '&', '"': '"', "'": ''', '/': '/', }; return html.replace(/[<>&"'\/]/g, char => map[char]); }; /** * Validate URL to prevent injection attacks * @param {string} url - URL to validate * @returns {boolean} Whether URL is safe */ const isValidURL = url => { if (typeof url !== 'string') return false; try { const parsed = new URL(url); // Only allow http and https protocols return ['http:', 'https:'].includes(parsed.protocol); } catch { return false; } }; /** * Safe localStorage wrapper */ const storage = { /** * Get item from localStorage with JSON parsing * @param {string} key - Storage key * @param {*} defaultValue - Default value if key doesn't exist * @returns {*} Parsed value or default */ get: (key, defaultValue = null) => { try { if (typeof key !== 'string' || !key) { logError('Storage', 'Invalid storage key', new Error('Key must be a non-empty string')); return defaultValue; } const value = localStorage.getItem(key); return value !== null ? JSON.parse(value) : defaultValue; } catch (e) { logError('Storage', `Failed to get item: ${key}`, e); return defaultValue; } }, /** * Set item to localStorage with JSON serialization * @param {string} key - Storage key * @param {*} value - Value to store * @returns {boolean} Success status */ set: (key, value) => { try { if (typeof key !== 'string' || !key) { logError('Storage', 'Invalid storage key', new Error('Key must be a non-empty string')); return false; } localStorage.setItem(key, JSON.stringify(value)); return true; } catch (e) { logError('Storage', `Failed to set item: ${key}`, e); return false; } }, /** * Remove item from localStorage * @param {string} key - Storage key */ remove: key => { try { if (typeof key !== 'string' || !key) { logError('Storage', 'Invalid storage key', new Error('Key must be a non-empty string')); return; } localStorage.removeItem(key); } catch (e) { logError('Storage', `Failed to remove item: ${key}`, e); } }, }; // Use shared debounce and throttle from YouTubeUtils (defined in utils.js) const debounce = /** @type {any} */ (window).YouTubeUtils?.debounce || ((func, wait, options = {}) => { let timeout; let lastArgs; let lastThis; /** @this {any} */ const debounced = function (...args) { lastArgs = args; lastThis = this; clearTimeout(timeout); if (options.leading && !timeout) { /** @type {Function} */ (func).apply(this, args); } timeout = setTimeout(() => { if (!options.leading) { /** @type {Function} */ (func).apply(lastThis, lastArgs); } timeout = null; lastArgs = null; lastThis = null; }, wait); }; debounced.cancel = () => { clearTimeout(timeout); timeout = null; lastArgs = null; lastThis = null; }; return debounced; }); const throttle = /** @type {any} */ (window).YouTubeUtils?.throttle || ((func, limit) => { let inThrottle; let lastResult; /** @this {any} */ return function (...args) { if (!inThrottle) { lastResult = /** @type {Function} */ (func).apply(this, args); inThrottle = true; setTimeout(() => (inThrottle = false), limit); } return lastResult; }; }); /** * Safe DOM element creation with props and children * @param {string} tag - HTML tag name * @param {Object} props - Element properties * @param {Array} children - Child elements or text * @returns {HTMLElement} Created element */ const createElement = (tag, props = {}, children = []) => { // Validate tag name to prevent XSS const validTags = /^[a-z][a-z0-9-]*$/i; if (!validTags.test(tag)) { logError('createElement', 'Invalid tag name', new Error(`Tag "${tag}" is not allowed`)); return document.createElement('div'); } const element = document.createElement(tag); Object.entries(props).forEach(([key, value]) => { if (key === 'className') { element.className = value; } else if (key === 'style' && typeof value === 'object') { Object.assign(element.style, value); } else if (key.startsWith('on') && typeof value === 'function') { element.addEventListener(key.substring(2).toLowerCase(), value); } else if (key === 'dataset' && typeof value === 'object') { Object.assign(element.dataset, value); } else if (key === 'innerHTML' || key === 'outerHTML') { // Prevent direct HTML injection logError( 'createElement', 'Direct HTML injection prevented', new Error('Use children array instead') ); } else { try { element.setAttribute(key, value); } catch (e) { logError('createElement', `Failed to set attribute ${key}`, e); } } }); children.forEach(child => { if (typeof child === 'string') { element.appendChild(document.createTextNode(child)); } else if (child instanceof Node) { element.appendChild(child); } }); return element; }; /** * DOM Selector Cache with automatic cleanup */ const selectorCache = new Map(); const CACHE_MAX_SIZE = 50; const CACHE_MAX_AGE = 5000; // 5 seconds /** * Cached querySelector with LRU-like eviction * @param {string} selector - CSS selector * @param {boolean} nocache - Skip cache * @returns {HTMLElement|null} Found element */ const querySelector = (selector, nocache = false) => { if (nocache) return document.querySelector(selector); const now = Date.now(); const cached = selectorCache.get(selector); // Check if cached element is still valid if (cached?.element?.isConnected && now - cached.timestamp < CACHE_MAX_AGE) { return cached.element; } // Remove stale entry if (cached) { selectorCache.delete(selector); } const element = document.querySelector(selector); if (element) { // LRU eviction: remove oldest entries if cache is full if (selectorCache.size >= CACHE_MAX_SIZE) { const firstKey = selectorCache.keys().next().value; selectorCache.delete(firstKey); } selectorCache.set(selector, { element, timestamp: now }); } return element; }; /** * Wait for element with timeout and AbortController * @param {string} selector - CSS selector * @param {number} timeout - Timeout in ms * @param {HTMLElement} parent - Parent element to search in * @returns {Promise} Promise resolving to element */ const waitForElement = (selector, timeout = 5000, parent = document.body) => { return new Promise((resolve, reject) => { // Validate inputs if (!selector || typeof selector !== 'string') { reject(new Error('Selector must be a non-empty string')); return; } if (!parent || !(parent instanceof Element)) { reject(new Error('Parent must be a valid DOM element')); return; } // Check if element already exists try { const element = parent.querySelector(selector); if (element) { resolve(/** @type {HTMLElement} */(/** @type {unknown} */ (element))); return; } } catch { reject(new Error(`Invalid selector: ${selector}`)); return; } const controller = new AbortController(); let observer = null; const timeoutId = setTimeout(() => { controller.abort(); if (observer) { try { observer.disconnect(); } catch (e) { logError('waitForElement', 'Observer disconnect failed', e); } } reject(new Error(`Element ${selector} not found within ${timeout}ms`)); }, timeout); observer = new MutationObserver(() => { try { const element = parent.querySelector(selector); if (element) { clearTimeout(timeoutId); observer.disconnect(); resolve(/** @type {HTMLElement} */(/** @type {unknown} */ (element))); } } catch (e) { logError('waitForElement', 'Observer callback error', e); } }); try { // Ensure parent supports observe/querySelector if (!(parent instanceof Element) && parent !== document) { throw new Error('Parent does not support observation'); } observer.observe(parent, { childList: true, subtree: true }); } catch { // Fallback for browsers without signal support try { observer.observe(parent, { childList: true, subtree: true }); } catch { clearTimeout(timeoutId); reject(new Error('Failed to observe DOM')); } } }); }; /** * Resource Cleanup Manager * Manages observers, listeners, and intervals */ const cleanupManager = { observers: new Set(), listeners: new Map(), intervals: new Set(), timeouts: new Set(), animationFrames: new Set(), /** * Register MutationObserver for cleanup * @param {MutationObserver} observer - Observer to register * @returns {MutationObserver} Registered observer */ registerObserver: observer => { cleanupManager.observers.add(observer); return observer; }, /** * Unregister and disconnect specific observer * @param {MutationObserver} observer - Observer to unregister */ unregisterObserver: observer => { if (observer) { try { observer.disconnect(); } catch (e) { logError('Cleanup', 'Observer disconnect failed', e); } cleanupManager.observers.delete(observer); } }, /** * Register event listener for cleanup * @param {EventTarget|Document|Window} element - Target element * @param {string} event - Event name * @param {EventListener|EventListenerObject} handler - Event handler * @param {Object} options - Event listener options * @returns {Symbol} Listener key for later removal */ registerListener: (element, event, handler, options) => { const key = Symbol('listener'); cleanupManager.listeners.set(key, { element, event, handler, options }); try { element.addEventListener(event, /** @type {EventListener} */(handler), options); } catch { // best-effort: if addEventListener fails, still register the listener record } return key; }, /** * Unregister specific listener * @param {Symbol} key - Listener key */ unregisterListener: key => { const listener = cleanupManager.listeners.get(key); if (listener) { const { element, event, handler, options } = listener; try { element.removeEventListener(event, handler, options); } catch (e) { logError('Cleanup', 'Listener removal failed', e); } cleanupManager.listeners.delete(key); } }, /** * Register interval for cleanup * @param {TimerId} id - Interval ID * @returns {TimerId} Interval ID */ registerInterval: id => { cleanupManager.intervals.add(id); return id; }, /** * Unregister specific interval * @param {number} id - Interval ID */ unregisterInterval: id => { clearInterval(id); cleanupManager.intervals.delete(id); }, /** * Register timeout for cleanup * @param {TimerId} id - Timeout ID * @returns {TimerId} Timeout ID */ registerTimeout: id => { cleanupManager.timeouts.add(id); return id; }, /** * Unregister specific timeout * @param {number} id - Timeout ID */ unregisterTimeout: id => { clearTimeout(id); cleanupManager.timeouts.delete(id); }, /** * Register animation frame for cleanup * @param {number} id - Animation frame ID * @returns {number} Animation frame ID */ registerAnimationFrame: id => { cleanupManager.animationFrames.add(id); return id; }, /** * Unregister specific animation frame * @param {number} id - Animation frame ID */ unregisterAnimationFrame: id => { cancelAnimationFrame(id); cleanupManager.animationFrames.delete(id); }, /** * Cleanup all registered resources */ cleanup: () => { // Disconnect all observers cleanupManager.observers.forEach(obs => { try { obs.disconnect(); } catch (e) { logError('Cleanup', 'Observer disconnect failed', e); } }); cleanupManager.observers.clear(); // Remove all listeners cleanupManager.listeners.forEach(({ element, event, handler, options }) => { try { element.removeEventListener(event, handler, options); } catch (e) { logError('Cleanup', 'Listener removal failed', e); } }); cleanupManager.listeners.clear(); // Clear all intervals cleanupManager.intervals.forEach(id => clearInterval(id)); cleanupManager.intervals.clear(); // Clear all timeouts cleanupManager.timeouts.forEach(id => clearTimeout(id)); cleanupManager.timeouts.clear(); // Cancel all animation frames cleanupManager.animationFrames.forEach(id => cancelAnimationFrame(id)); cleanupManager.animationFrames.clear(); }, }; /** * Settings Manager * Centralized settings storage and retrieval */ const SettingsManager = { storageKey: 'youtube_plus_all_settings_v2', defaults: { speedControl: { enabled: true, currentSpeed: 1 }, screenshot: { enabled: true }, download: { enabled: true }, updateChecker: { enabled: true }, adBlocker: { enabled: true }, pip: { enabled: true }, timecodes: { enabled: true }, // Add other modules... }, /** * Load all settings * @returns {Object} Settings object */ load() { const saved = storage.get(this.storageKey); return saved ? { ...this.defaults, ...saved } : { ...this.defaults }; }, /** * Save all settings * @param {Object} settings - Settings to save */ save(settings) { storage.set(this.storageKey, settings); // Dispatch event for modules to react window.dispatchEvent( new CustomEvent('youtube-plus-settings-changed', { detail: settings, }) ); }, /** * Get setting by path * @param {string} path - Dot-separated path (e.g., 'speedControl.enabled') * @returns {*} Setting value */ get(path) { const settings = this.load(); return path.split('.').reduce((obj, key) => obj?.[key], settings); }, /** * Set setting by path * @param {string} path - Dot-separated path * @param {*} value - Value to set */ set(path, value) { const settings = this.load(); const keys = path.split('.'); const last = keys.pop(); const target = keys.reduce((obj, key) => { obj[key] = obj[key] || {}; return obj[key]; }, settings); target[last] = value; this.save(settings); }, }; /** * Style Manager * Centralized CSS injection and management */ const StyleManager = { styles: new Map(), element: null, /** * Add CSS rules * @param {string} id - Unique identifier * @param {string} css - CSS rules */ add(id, css) { if (typeof id !== 'string' || !id) { logError('StyleManager', 'Invalid style ID', new Error('ID must be a non-empty string')); return; } if (typeof css !== 'string') { logError('StyleManager', 'Invalid CSS', new Error('CSS must be a string')); return; } this.styles.set(id, css); this.update(); }, /** * Remove CSS rules * @param {string} id - Identifier */ remove(id) { this.styles.delete(id); this.update(); }, /** * Update style element */ update() { try { if (!this.element) { this.element = document.createElement('style'); this.element.id = 'youtube-plus-styles'; this.element.type = 'text/css'; (document.head || document.documentElement).appendChild(this.element); } this.element.textContent = Array.from(this.styles.values()).join('\n'); } catch (error) { logError('StyleManager', 'Failed to update styles', error); } }, /** * Clear all styles */ clear() { this.styles.clear(); if (this.element) { try { this.element.remove(); } catch (e) { logError('StyleManager', 'Failed to remove style element', e); } this.element = null; } }, }; /** * Centralized Notification System * Manages all notifications with queue and deduplication */ const NotificationManager = { queue: [], activeNotifications: new Set(), maxVisible: 3, defaultDuration: 3000, /** * Show notification * @param {string} message - Notification message * @param {Object} options - Notification options * @returns {HTMLElement} Notification element */ show(message, options = {}) { // Validate message if (!message || typeof message !== 'string') { logError( 'NotificationManager', 'Invalid message', new Error('Message must be a non-empty string') ); return null; } const { duration = this.defaultDuration, position = null, action = null, // { text: string, callback: function } } = options; // Remove duplicate messages this.activeNotifications.forEach(notif => { if (notif.dataset.message === message) { this.remove(notif); } }); const positions = { 'top-right': { top: '20px', right: '20px' }, 'top-left': { top: '20px', left: '20px' }, 'bottom-right': { bottom: '20px', right: '20px' }, 'bottom-left': { bottom: '20px', left: '20px' }, }; try { // Use shared enhancer notification class for consistent appearance const notification = createElement('div', { className: 'youtube-enhancer-notification', dataset: { message }, // Store message for deduplication // Keep minimal inline styles; main visuals come from the shared CSS class style: Object.assign( { zIndex: '10001', width: 'auto', display: 'flex', alignItems: 'center', gap: '10px', }, position && positions[position] ? positions[position] : {} ), }); // Add message (with accessibility attributes) notification.setAttribute('role', 'status'); notification.setAttribute('aria-live', 'polite'); notification.setAttribute('aria-atomic', 'true'); const messageSpan = createElement( 'span', { style: { flex: '1' }, }, [message] ); notification.appendChild(messageSpan); // Add action button if provided if (action && action.text && typeof action.callback === 'function') { const actionBtn = createElement( 'button', { style: { background: 'rgba(255,255,255,0.2)', border: '1px solid rgba(255,255,255,0.3)', color: 'white', padding: '4px 12px', borderRadius: '4px', cursor: 'pointer', fontSize: '12px', fontWeight: '600', transition: 'background 0.2s', }, onClick: () => { action.callback(); this.remove(notification); }, }, [action.text] ); notification.appendChild(actionBtn); } document.body.appendChild(notification); this.activeNotifications.add(notification); // Apply entry animation from bottom try { notification.style.animation = 'slideInFromBottom 0.38s ease-out forwards'; } catch { } // Auto-dismiss if (duration > 0) { const timeoutId = setTimeout(() => this.remove(notification), duration); cleanupManager.registerTimeout(timeoutId); } // Limit visible notifications if (this.activeNotifications.size > this.maxVisible) { const oldest = Array.from(this.activeNotifications)[0]; this.remove(oldest); } return notification; } catch (error) { logError('NotificationManager', 'Failed to show notification', error); return null; } }, /** * Remove notification * @param {HTMLElement} notification - Notification element */ remove(notification) { if (!notification || !notification.isConnected) return; try { try { notification.style.animation = 'slideOutToBottom 0.32s ease-in forwards'; const timeoutId = setTimeout(() => { try { notification.remove(); this.activeNotifications.delete(notification); } catch (e) { logError('NotificationManager', 'Failed to remove notification', e); } }, 340); cleanupManager.registerTimeout(timeoutId); } catch { // Fallback: immediate removal try { notification.remove(); this.activeNotifications.delete(notification); } catch (e) { logError('NotificationManager', 'Failed to remove notification (fallback)', e); } } } catch (error) { logError('NotificationManager', 'Failed to animate notification removal', error); // Force remove notification.remove(); this.activeNotifications.delete(notification); } }, /** * Clear all notifications */ clearAll() { this.activeNotifications.forEach(notif => { try { notif.remove(); } catch (e) { logError('NotificationManager', 'Failed to clear notification', e); } }); this.activeNotifications.clear(); }, }; // Add notification animation styles StyleManager.add( 'notification-animations', ` @keyframes slideInFromBottom { from { transform: translateY(100%); opacity: 0; } to { transform: translateY(0); opacity: 1; } } @keyframes slideOutToBottom { from { transform: translateY(0); opacity: 1; } to { transform: translateY(100%); opacity: 0; } } ` ); // Global cleanup on page unload window.addEventListener('beforeunload', () => { cleanupManager.cleanup(); selectorCache.clear(); StyleManager.clear(); NotificationManager.clearAll(); }); // Periodic cache cleanup to prevent memory leaks const cacheCleanupInterval = setInterval(() => { const now = Date.now(); for (const [key, value] of selectorCache.entries()) { if (!value.element?.isConnected || now - value.timestamp > CACHE_MAX_AGE) { selectorCache.delete(key); } } }, 30000); // Clean every 30 seconds cleanupManager.registerInterval(cacheCleanupInterval); // Global error handler for uncaught promise rejections window.addEventListener('unhandledrejection', event => { logError('Global', 'Unhandled promise rejection', event.reason); event.preventDefault(); // Prevent console spam }); // Global error handler for uncaught errors window.addEventListener('error', event => { // Only log errors from our script if (event.filename && event.filename.includes('youtube')) { logError( 'Global', 'Uncaught error', new Error(`${event.message} at ${event.filename}:${event.lineno}:${event.colno}`) ); } }); /** * Performance monitoring wrapper * @param {string} label - Operation label * @param {Function} fn - Function to monitor * @returns {Function} Wrapped function */ const measurePerformance = (label, fn) => { /** @this {any} */ return function (...args) { const start = performance.now(); try { const result = fn.apply(this, args); const duration = performance.now() - start; if (duration > 100) { console.warn(`[YouTube+][Performance] ${label} took ${duration.toFixed(2)}ms`); } return result; } catch (error) { logError('Performance', `${label} failed`, error); throw error; } }; }; /** * Async performance monitoring wrapper * @param {string} label - Operation label * @param {Function} fn - Async function to monitor * @returns {Function} Wrapped async function */ const measurePerformanceAsync = (label, fn) => { /** @this {any} */ return async function (...args) { const start = performance.now(); try { const result = await fn.apply(this, args); const duration = performance.now() - start; if (duration > 100) { console.warn(`[YouTube+][Performance] ${label} took ${duration.toFixed(2)}ms`); } return result; } catch (error) { logError('Performance', `${label} failed`, error); throw error; } }; }; /** * Mobile device detection * @returns {boolean} True if mobile device */ const isMobile = () => { return ( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || window.innerWidth <= 768 ); }; /** * Get viewport dimensions * @returns {Object} Width and height */ const getViewport = () => ({ width: Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0), height: Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0), }); /** * Safe async retry wrapper * @param {Function} fn - Async function to retry * @param {number} retries - Number of retries * @param {number} delay - Delay between retries * @returns {Promise} Result or error */ const retryAsync = async (fn, retries = 3, delay = 1000) => { for (let i = 0; i < retries; i++) { try { return await fn(); } catch (error) { if (i === retries - 1) throw error; await new Promise(resolve => setTimeout(resolve, delay * (i + 1))); } } }; // Export public API return { logError, safeExecute, safeExecuteAsync, sanitizeHTML, isValidURL, storage, debounce, throttle, createElement, querySelector, waitForElement, cleanupManager, SettingsManager, StyleManager, NotificationManager, clearCache: () => selectorCache.clear(), isMobile, getViewport, retryAsync, measurePerformance, measurePerformanceAsync, t, // Translation function }; })(); // Make available globally if (typeof window !== 'undefined') { // Merge utilities into existing global YouTubeUtils without overwriting /** @type {any} */ (window).YouTubeUtils = /** @type {any} */ (window).YouTubeUtils || {}; const existing = /** @type {any} */ (window).YouTubeUtils; try { for (const k of Object.keys(YouTubeUtils)) { if (existing[k] === undefined) existing[k] = YouTubeUtils[k]; } } catch { } // Add initialization health check (non-intrusive) console.log('[YouTube+ v2.0.1] Core utilities merged'); // Expose debug info /** @type {any} */ (window).YouTubePlusDebug = { version: '2.0.1', cacheSize: () => YouTubeUtils.cleanupManager.observers.size + YouTubeUtils.cleanupManager.listeners.size + YouTubeUtils.cleanupManager.intervals.size, clearAll: () => { YouTubeUtils.cleanupManager.cleanup(); YouTubeUtils.clearCache(); YouTubeUtils.StyleManager.clear(); YouTubeUtils.NotificationManager.clearAll(); console.log('[YouTube+] All resources cleared'); }, stats: () => ({ observers: YouTubeUtils.cleanupManager.observers.size, listeners: YouTubeUtils.cleanupManager.listeners.size, intervals: YouTubeUtils.cleanupManager.intervals.size, timeouts: YouTubeUtils.cleanupManager.timeouts.size, animationFrames: YouTubeUtils.cleanupManager.animationFrames.size, styles: YouTubeUtils.StyleManager.styles.size, notifications: YouTubeUtils.NotificationManager.activeNotifications.size, }), }; // Show subtle startup notification (only once per session) if (!sessionStorage.getItem('youtube_plus_started')) { sessionStorage.setItem('youtube_plus_started', 'true'); setTimeout(() => { if (YouTubeUtils.NotificationManager) { YouTubeUtils.NotificationManager.show('YouTube+ v2.0.1 loaded', { type: 'success', duration: 2000, position: 'bottom-right', }); } }, 1000); } } //----------------------------------------------------------------------------- // YouTube enhancements module (function () { 'use strict'; // Local reference to translation function const t = YouTubeUtils.t; const YouTubeEnhancer = { // Speed control variables speedControl: { currentSpeed: 1, activeAnimationId: null, storageKey: 'youtube_playback_speed', }, _initialized: false, // Settings settings: { enableSpeedControl: true, enableScreenshot: true, enableDownload: true, // Состояние сайтов внутри сабменю кнопки Download (ytdl всегда включён) downloadSites: { y2mate: true, xbbuddy: true, }, // Настройки кастомизации download сайтов downloadSiteCustomization: { y2mate: { name: 'Y2Mate', url: 'https://www.y2mate.com/youtube/{videoId}', }, xbbuddy: { name: '9xbuddy', url: 'https://9xbuddy.org/process?url={videoUrl}', }, }, storageKey: 'youtube_plus_settings', }, // Cache DOM queries _cache: new Map(), // Cached element getter getElement(selector, useCache = true) { if (useCache && this._cache.has(selector)) { const element = this._cache.get(selector); if (element?.isConnected) return element; this._cache.delete(selector); } const element = document.querySelector(selector); if (element && useCache) this._cache.set(selector, element); return element; }, loadSettings() { try { const saved = localStorage.getItem(this.settings.storageKey); if (saved) Object.assign(this.settings, JSON.parse(saved)); } catch (e) { console.error('Error loading settings:', e); } }, init() { if (this._initialized) { return; } this._initialized = true; try { this.loadSettings(); } catch (error) { console.warn('[YouTube Enhancer] Failed to load settings during init:', error); } this.insertStyles(); this.addSettingsButtonToHeader(); this.setupNavigationObserver(); if (location.href.includes('watch?v=')) { this.setupCurrentPage(); } document.addEventListener('visibilitychange', () => { if (!document.hidden && location.href.includes('watch?v=')) { this.setupCurrentPage(); } }); }, saveSettings() { localStorage.setItem(this.settings.storageKey, JSON.stringify(this.settings)); this.updatePageBasedOnSettings(); this.refreshDownloadButton(); }, updatePageBasedOnSettings() { const settingsMap = { 'ytp-screenshot-button': 'enableScreenshot', 'ytp-download-button': 'enableDownload', 'speed-control-btn': 'enableSpeedControl', }; Object.entries(settingsMap).forEach(([className, setting]) => { const button = this.getElement(`.${className}`, false); if (button) button.style.display = this.settings[setting] ? '' : 'none'; }); }, refreshDownloadButton() { const selector = '.ytp-download-button'; // Очистить кеш, чтобы избежать возврата удалённых элементов if (this._cache.has(selector)) { this._cache.delete(selector); } const existingButton = document.querySelector(selector); if (existingButton?.parentElement) { existingButton.remove(); } if (!this.settings.enableDownload) { return; } const controls = this.getElement('.ytp-right-controls', false); if (!controls) { return; } this.addDownloadButton(controls); }, setupCurrentPage() { this.waitForElement('#player-container-outer .html5-video-player, .ytp-right-controls', 5000) .then(() => { this.addCustomButtons(); this.setupVideoObserver(); this.applyCurrentSpeed(); this.updatePageBasedOnSettings(); this.refreshDownloadButton(); }) .catch(() => { }); }, insertStyles() { // Glassmorphism styles for modal and controls const styles = `:root{--yt-accent:#ff0000;--yt-accent-hover:#cc0000;--yt-radius-sm:6px;--yt-radius-md:10px;--yt-radius-lg:16px;--yt-transition:all .2s ease;--yt-space-xs:4px;--yt-space-sm:8px;--yt-space-md:16px;--yt-space-lg:24px;--yt-glass-blur:blur(18px) saturate(180%);--yt-glass-blur-light:blur(12px) saturate(160%);--yt-glass-blur-heavy:blur(24px) saturate(200%);} html[dark],html:not([dark]):not([light]){--yt-bg-primary:rgba(15,15,15,.85);--yt-bg-secondary:rgba(28,28,28,.85);--yt-bg-tertiary:rgba(34,34,34,.85);--yt-text-primary:#fff;--yt-text-secondary:#aaa;--yt-border-color:rgba(255,255,255,.2);--yt-hover-bg:rgba(255,255,255,.1);--yt-shadow:0 4px 12px rgba(0,0,0,.25);--yt-glass-bg:rgba(255,255,255,.1);--yt-glass-border:rgba(255,255,255,.2);--yt-glass-shadow:0 8px 32px rgba(0,0,0,.2);--yt-modal-bg:rgba(0,0,0,.75);--yt-notification-bg:rgba(28,28,28,.9);--yt-panel-bg:rgba(34,34,34,.3);--yt-header-bg:rgba(20,20,20,.6);--yt-input-bg:rgba(255,255,255,.1);--yt-button-bg:rgba(255,255,255,.2);--yt-text-stroke:white;} html[light]{--yt-bg-primary:rgba(255,255,255,.85);--yt-bg-secondary:rgba(248,248,248,.85);--yt-bg-tertiary:rgba(240,240,240,.85);--yt-text-primary:#030303;--yt-text-secondary:#606060;--yt-border-color:rgba(0,0,0,.2);--yt-hover-bg:rgba(0,0,0,.05);--yt-shadow:0 4px 12px rgba(0,0,0,.15);--yt-glass-bg:rgba(255,255,255,.7);--yt-glass-border:rgba(0,0,0,.1);--yt-glass-shadow:0 8px 32px rgba(0,0,0,.1);--yt-modal-bg:rgba(0,0,0,.5);--yt-notification-bg:rgba(255,255,255,.95);--yt-panel-bg:rgba(255,255,255,.7);--yt-header-bg:rgba(248,248,248,.8);--yt-input-bg:rgba(0,0,0,.05);--yt-button-bg:rgba(0,0,0,.1);--yt-text-stroke:#030303;} .ytp-screenshot-button,.ytp-cobalt-button,.ytp-pip-button{position:relative;bottom:12px;width:44px;transition:opacity .15s,transform .15s;} .ytp-screenshot-button:hover,.ytp-cobalt-button:hover,.ytp-pip-button:hover{transform:scale(1.1);} .speed-control-btn{width:4em!important;float:left;text-align:center!important;border-radius:var(--yt-radius-sm);font-size:13px;color:var(--yt-text-primary);cursor:pointer;user-select:none;font-family:system-ui,-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;transition:color .2s;} .speed-control-btn:hover{color:var(--yt-accent);font-weight:bold;} .speed-options{position:absolute!important;background:var(--yt-glass-bg)!important;color:var(--yt-text-primary)!important;border-radius:var(--yt-radius-md)!important;display:none;bottom: 100%!important;width:48px!important;z-index:9999!important;box-shadow:var(--yt-glass-shadow);border:1px solid var(--yt-glass-border);overflow:hidden;backdrop-filter:var(--yt-glass-blur);-webkit-backdrop-filter:var(--yt-glass-blur);} .speed-option-item{cursor:pointer!important;height:25px!important;line-height:25px!important;font-size:12px!important;text-align:center!important;transition:background-color .15s,color .15s;} .speed-option-active,.speed-option-item:hover{color:var(--yt-accent)!important;font-weight:bold!important;background:var(--yt-hover-bg)!important;} #speed-indicator{position:absolute!important;margin:auto!important;top:0!important;right:0!important;bottom:0!important;left:0!important;border-radius:24px!important;font-size:30px!important;background:var(--yt-glass-bg)!important;color:var(--yt-text-primary)!important;z-index:99999!important;width:80px!important;height:80px!important;line-height:80px!important;text-align:center!important;display:none;box-shadow:var(--yt-glass-shadow);backdrop-filter:var(--yt-glass-blur);-webkit-backdrop-filter:var(--yt-glass-blur);border:1px solid var(--yt-glass-border);} .youtube-enhancer-notification{position:fixed;bottom:75px;right:50%;max-width:500px;width:auto;background:var(--yt-glass-bg);color:var(--yt-text-primary);padding:8px 14px;font-size:13px;border-radius:var(--yt-radius-md);z-index:9999;transition:opacity .35s,transform .32s;box-shadow:var(--yt-glass-shadow);border:1px solid var(--yt-glass-border);backdrop-filter:var(--yt-glass-blur); -webkit-backdrop-filter:var(--yt-glass-blur);font-weight:500;box-sizing:border-box;display:flex;align-items:center;gap:10px;} .ytp-plus-settings-button{background:transparent;border:none;color:var(--yt-text-secondary);cursor:pointer;padding:var(--yt-space-sm);margin-right:var(--yt-space-sm);border-radius:50%;display:flex;align-items:center;justify-content:center;transition:background-color .2s,transform .2s;} .ytp-plus-settings-button svg{width:24px;height:24px;} .ytp-plus-settings-button:hover{background:var(--yt-hover-bg);transform:rotate(30deg);color:var(--yt-text-secondary);} .ytp-plus-settings-modal{position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.45);display:flex;align-items:center;justify-content:center;z-index:100000;backdrop-filter:blur(8px) saturate(140%);-webkit-backdrop-filter:blur(8px) saturate(140%);animation:ytEnhanceFadeIn .25s ease-out;} .ytp-plus-settings-panel{background:var(--yt-glass-bg);color:var(--yt-text-primary);border-radius:20px;width:760px;max-width:94%;max-height:90vh;overflow:hidden;box-shadow:0 12px 40px rgba(0,0,0,0.45);animation:ytEnhanceScaleIn .28s cubic-bezier(.4,0,.2,1);backdrop-filter:blur(14px) saturate(140%);-webkit-backdrop-filter:blur(14px) saturate(140%);border:1.5px solid var(--yt-glass-border);will-change:transform,opacity;display:flex;flex-direction:row} .ytp-plus-settings-sidebar{width:240px;background:var(--yt-header-bg);border-right:1px solid var(--yt-glass-border);display:flex;flex-direction:column;backdrop-filter:var(--yt-glass-blur-light);-webkit-backdrop-filter:var(--yt-glass-blur-light);} .ytp-plus-settings-sidebar-header{padding:var(--yt-space-md) var(--yt-space-lg);border-bottom:1px solid var(--yt-glass-border);display:flex;justify-content:space-between;align-items:center;} .ytp-plus-settings-title{font-size:18px;font-weight:500;margin:0;color:var(--yt-text-primary);} .ytp-plus-settings-sidebar-close{padding:var(--yt-space-md) var(--yt-space-lg);display:flex;justify-content:flex-end;background:transparent;} .ytp-plus-settings-close{background:none;border:none;cursor:pointer;padding:var(--yt-space-sm);margin:-8px;color:var(--yt-text-primary);transition:color .2s,transform .2s;} .ytp-plus-settings-close:hover{color:var(--yt-accent);transform:scale(1.25) rotate(90deg);} .ytp-plus-settings-nav{flex:1;padding:var(--yt-space-md) 0;} .ytp-plus-settings-nav-item{display:flex;align-items:center;padding:12px var(--yt-space-lg);cursor:pointer;transition:all .2s cubic-bezier(.4,0,.2,1);font-size:14px;border-left:3px solid transparent;color:var(--yt-text-primary);} .ytp-plus-settings-nav-item:hover{background:var(--yt-hover-bg);} .ytp-plus-settings-nav-item.active{background:rgba(255,0,0,.1);border-left-color:var(--yt-accent);color:var(--yt-accent);font-weight:500;} .ytp-plus-settings-nav-item svg{width:18px;height:18px;margin-right:12px;opacity:.8;transition:opacity .2s,transform .2s;} .ytp-plus-settings-nav-item.active svg{opacity:1;transform:scale(1.1);} .ytp-plus-settings-nav-item:hover svg{transform:scale(1.05);} .ytp-plus-settings-main{flex:1;display:flex;flex-direction:column;overflow-y:auto;} .ytp-plus-settings-header{padding:var(--yt-space-md) var(--yt-space-lg);border-bottom:1px solid var(--yt-glass-border);background:var(--yt-header-bg);backdrop-filter:var(--yt-glass-blur-light);-webkit-backdrop-filter:var(--yt-glass-blur-light);} .ytp-plus-settings-content{flex:1;padding:var(--yt-space-md) var(--yt-space-lg);overflow-y:auto;} .ytp-plus-settings-section{margin-bottom:var(--yt-space-lg);} .ytp-plus-settings-section-title{font-size:16px;font-weight:500;margin-bottom:var(--yt-space-md);color:var(--yt-text-primary);} .ytp-plus-settings-section.hidden{display:none;} .ytp-plus-settings-item{display:flex;align-items:center;margin-bottom:var(--yt-space-md);padding:14px 18px;background:transparent;transition:all .25s cubic-bezier(.4,0,.2,1);border-radius:var(--yt-radius-md);} .ytp-plus-settings-item:hover{background:var(--yt-hover-bg);transform:translateX(6px);box-shadow:0 2px 8px rgba(0,0,0,.1);} .ytp-plus-settings-item-label{flex:1;font-size:14px;color:var(--yt-text-primary);} .ytp-plus-settings-item-description{font-size:12px;color:var(--yt-text-secondary);margin-top:4px;} .ytp-plus-settings-checkbox{appearance:none;-webkit-appearance:none;-moz-appearance:none;width:15px;height:15px;margin-left:auto;border:1px solid var(--yt-glass-border);border-radius:50%;background:transparent;display:inline-flex;align-items:center;justify-content:center;transition:all 250ms cubic-bezier(.4,0,.23,1);cursor:pointer;position:relative;flex-shrink:0;color:#fff;} html:not([dark]) .ytp-plus-settings-checkbox{border-color:rgba(0,0,0,.25);color:#222;} .ytp-plus-settings-checkbox:focus-visible{outline:2px solid var(--yt-accent);outline-offset:2px;} .ytp-plus-settings-checkbox:hover{background:var(--yt-hover-bg);transform:scale(1.1);} .ytp-plus-settings-checkbox::before{content:"";width:4px;height:2px;background:var(--yt-text-primary);position:absolute;transform:rotate(45deg);top:4px;left:3px;transition:width 100ms ease 50ms,opacity 50ms;transform-origin:0% 0%;opacity:0;} .ytp-plus-settings-checkbox::after{content:"";width:0;height:2px;background:var(--yt-text-primary);position:absolute;transform:rotate(305deg);top:9px;left:6px;transition:width 100ms ease,opacity 50ms;transform-origin:0% 0%;opacity:0;} .ytp-plus-settings-checkbox:checked{transform:rotate(0deg) scale(1.2);} .ytp-plus-settings-checkbox:checked::before{width:8px;opacity:1;background:#fff;transition:width 150ms ease 100ms,opacity 150ms ease 100ms;} .ytp-plus-settings-checkbox:checked::after{width:15px;opacity:1;background:#fff;transition:width 150ms ease 250ms,opacity 150ms ease 250ms;} .ytp-plus-footer{padding:var(--yt-space-md) var(--yt-space-lg);border-top:1px solid var(--yt-glass-border);display:flex;justify-content:flex-end;background:transparent;} .ytp-plus-button{padding:var(--yt-space-sm) var(--yt-space-md);border-radius:18px;border:none;font-size:14px;font-weight:500;cursor:pointer;transition:all .25s cubic-bezier(.4,0,.2,1);} .ytp-plus-button-primary{background:transparent;border:1px solid var(--yt-glass-border);color:var(--yt-text-primary);} .ytp-plus-button-primary:hover{background:var(--yt-accent);color:#fff;box-shadow:0 6px 16px rgba(255,0,0,.35);transform:translateY(-2px);} .app-icon{fill:var(--yt-text-primary);stroke:var(--yt-text-primary);transition:all .3s;} @keyframes ytEnhanceFadeIn{from{opacity:0;}to{opacity:1;}} @keyframes ytEnhanceScaleIn{from{opacity:0;transform:scale(.92) translateY(10px);}to{opacity:1;transform:scale(1) translateY(0);}} @media(max-width:768px){.ytp-plus-settings-panel{width:95%;max-height:80vh;flex-direction:column;} .ytp-plus-settings-sidebar{width:100%;max-height:120px;flex-direction:row;overflow-x:auto;} .ytp-plus-settings-nav{display:flex;flex-direction:row;padding:0;} .ytp-plus-settings-nav-item{white-space:nowrap;border-left:none;border-bottom:3px solid transparent;} .ytp-plus-settings-nav-item.active{border-left:none;border-bottom-color:var(--yt-accent);} .ytp-plus-settings-item{padding:10px 12px;}} .ytp-plus-settings-section h1{margin:-95px 90px 8px;font-family:'Montserrat',sans-serif;font-size:52px;font-weight:600;color:transparent;-webkit-text-stroke-width:1px;-webkit-text-stroke-color:var(--yt-text-stroke);cursor:pointer;transition:color .2s;} .ytp-plus-settings-section h1:hover{color:var(--yt-accent);-webkit-text-stroke-width:1px;-webkit-text-stroke-color:transparent;} .download-options{position:fixed;background:var(--yt-glass-bg);color:var(--yt-text-primary);border-radius:var(--yt-radius-md);width:150px;z-index:99999;box-shadow:var(--yt-glass-shadow);border:1px solid var(--yt-glass-border);overflow:hidden;backdrop-filter:var(--yt-glass-blur);-webkit-backdrop-filter:var(--yt-glass-blur);display:none;} .download-options.visible{display:block;} .download-options-list{display:flex;flex-direction:column;align-items:center;justify-content:center;width:100%;} .download-option-item{cursor:pointer;padding:12px;text-align:center;transition:background .2s,color .2s;width:100%;} .download-option-item:hover{background:var(--yt-hover-bg);color:var(--yt-accent);} .glass-panel{background:var(--yt-glass-bg);border:1px solid var(--yt-glass-border);border-radius:var(--yt-radius-md);backdrop-filter:var(--yt-glass-blur);-webkit-backdrop-filter:var(--yt-glass-blur);box-shadow:var(--yt-glass-shadow);} .glass-card{background:var(--yt-panel-bg);border:1px solid var(--yt-glass-border);border-radius:var(--yt-radius-md);padding:var(--yt-space-md);backdrop-filter:var(--yt-glass-blur-light);-webkit-backdrop-filter:var(--yt-glass-blur-light);box-shadow:var(--yt-shadow);} .glass-modal{position:fixed;top:0;left:0;right:0;bottom:0;background:var(--yt-modal-bg);display:flex;align-items:center;justify-content:center;z-index:99999;backdrop-filter:var(--yt-glass-blur);-webkit-backdrop-filter:var(--yt-glass-blur);} .glass-button{background:var(--yt-button-bg);border:1px solid var(--yt-glass-border);border-radius:var(--yt-radius-md);padding:var(--yt-space-sm) var(--yt-space-md);color:var(--yt-text-primary);cursor:pointer;transition:all .2s ease;backdrop-filter:var(--yt-glass-blur-light);-webkit-backdrop-filter:var(--yt-glass-blur-light);} .glass-button:hover{background:var(--yt-hover-bg);transform:translateY(-1px);box-shadow:var(--yt-shadow);} .download-site-option{display:flex;flex-direction:column;align-items:stretch;gap:8px;} .download-site-header{display:flex;flex-direction:row;align-items:center;justify-content:space-between;width:100%;gap:8px;} .download-site-controls{width:100%;margin-top:6px;} .download-site-cta{display:flex;flex-direction:row;gap:8px;margin-top:6px;} .download-site-cta .glass-button{width:100%;} .download-site-option .ytp-plus-settings-checkbox{margin:0;} .download-site-name{font-weight:600;color:var(--yt-text-primary);} .download-site-desc{font-size:12px;color:var(--yt-text-secondary);margin-top:2px;} /* Ensure custom YouTube searchbox input backgrounds are transparent to match theme */ .ytSearchboxComponentInputBox { background: transparent !important; } `; // ✅ Use StyleManager instead of createElement('style') if (!document.getElementById('yt-enhancer-styles')) { YouTubeUtils.StyleManager.add('yt-enhancer-main', styles); } }, addSettingsButtonToHeader() { this.waitForElement('ytd-masthead #end', 5000) .then(headerEnd => { if (!this.getElement('.ytp-plus-settings-button')) { const settingsButton = document.createElement('div'); settingsButton.className = 'ytp-plus-settings-button'; settingsButton.setAttribute('title', t('youtubeSettings')); settingsButton.innerHTML = ` `; settingsButton.addEventListener('click', this.openSettingsModal.bind(this)); const avatarButton = headerEnd.querySelector('ytd-topbar-menu-button-renderer'); if (avatarButton) { headerEnd.insertBefore(settingsButton, avatarButton); } else { headerEnd.appendChild(settingsButton); } } }) .catch(() => { }); }, createSettingsModal() { const modal = document.createElement('div'); modal.className = 'ytp-plus-settings-modal'; modal.innerHTML = `

${t('settingsTitle')}

${t('basicTab')}
${t('advancedTab')}
${t('experimentalTab')}
${t('aboutTab')}
${t('speedControlDesc')}
${t('screenshotButtonDesc')}
${t('downloadButtonDesc')}
${this.settings.downloadSiteCustomization?.y2mate?.name || 'Y2Mate'}
${t('customDownloader')}
${this.settings.downloadSiteCustomization?.xbbuddy?.name || '9xbuddy'}
${t('customDownloader')}
${t('byYTDL')}
${t('alwaysEnabled')}
`; // Event delegation for better performance modal.addEventListener('click', e => { const target = /** @type {HTMLElement} */ (e.target); if (target === modal) modal.remove(); if ( target.classList.contains('ytp-plus-settings-close') || target.closest('.ytp-plus-settings-close') ) { modal.remove(); } // Обработка кнопки GitHub для YTDL if (target.id === 'open-ytdl-github' || target.closest('#open-ytdl-github')) { window.open('https://github.com/diorhc/YouTube-Downloader', '_blank'); return; } if (target.classList.contains('ytp-plus-settings-nav-item')) { // Handle sidebar navigation const section = /** @type {HTMLElement} */ (target).dataset.section; modal .querySelectorAll('.ytp-plus-settings-nav-item') .forEach(item => item.classList.remove('active')); modal .querySelectorAll('.ytp-plus-settings-section') .forEach(section => section.classList.add('hidden')); target.classList.add('active'); modal .querySelector(`.ytp-plus-settings-section[data-section="${section}"]`) .classList.remove('hidden'); } if (target.classList.contains('ytp-plus-settings-checkbox')) { const setting = /** @type {HTMLElement} */ (target).dataset.setting; if (!setting) return; // Сохранение простых настроек (enableSpeedControl, enableScreenshot, enableDownload) if (!setting.startsWith('downloadSite_')) { this.settings[setting] = /** @type {HTMLInputElement} */ (target).checked; // Показывать/скрывать сабменю при переключении Download if (setting === 'enableDownload') { const submenu = modal.querySelector('.download-submenu'); if (submenu) { submenu.style.display = /** @type {HTMLInputElement} */ (target).checked ? 'block' : 'none'; } } } else { // Обработка чекбоксов в сабменю: data-setting = downloadSite_ const key = setting.replace('downloadSite_', ''); if (!this.settings.downloadSites) { this.settings.downloadSites = { y2mate: true, xbbuddy: true }; } const checkbox = /** @type {HTMLElement} */ (target); this.settings.downloadSites[key] = /** @type {HTMLInputElement} */ (checkbox).checked; // Toggle visibility of controls for this site (if present in DOM) try { const container = checkbox.closest('.download-site-option'); if (container) { const controls = container.querySelector('.download-site-controls'); if (controls) { controls.style.display = /** @type {HTMLInputElement} */ (checkbox).checked ? 'block' : 'none'; } } } catch (err) { console.warn('[YouTube+] toggle download-site-controls failed:', err); } // Rebuild dropdown if present try { if ( typeof window !== 'undefined' && /** @type {any} */ (window).youtubePlus && typeof (/** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown) === 'function' ) { /** @type {any} */ (window).youtubePlus.settings = /** @type {any} */ (window).youtubePlus.settings || this.settings; /** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown(); } } catch (err) { console.warn('[YouTube+] rebuildDownloadDropdown call failed:', err); } } } // Обработка кастомизации download сайтов if (target.classList.contains('download-site-input')) { const site = /** @type {HTMLElement} */ (target).dataset.site; const field = /** @type {HTMLElement} */ (target).dataset.field; if (!site || !field) return; if (!this.settings.downloadSiteCustomization) { this.settings.downloadSiteCustomization = { y2mate: { name: 'Y2Mate', url: 'https://www.y2mate.com/youtube/{videoId}' }, xbbuddy: { name: '9xbuddy', url: 'https://9xbuddy.org/process?url={videoUrl}' }, }; } if (!this.settings.downloadSiteCustomization[site]) { this.settings.downloadSiteCustomization[site] = { name: '', url: '' }; } this.settings.downloadSiteCustomization[site][field] = /** @type {HTMLInputElement} */ ( target ).value; // Обновить имя в UI в реальном времени if (field === 'name') { const nameDisplay = target .closest('.download-site-option') ?.querySelector('.download-site-name'); if (nameDisplay) { nameDisplay.textContent = /** @type {HTMLInputElement} */ (target).value || (site === 'y2mate' ? 'Y2Mate' : '9xbuddy'); } } // Rebuild dropdown if present so changes reflect immediately try { if ( typeof window !== 'undefined' && /** @type {any} */ (window).youtubePlus && typeof (/** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown) === 'function' ) { /** @type {any} */ (window).youtubePlus.settings = /** @type {any} */ (window).youtubePlus.settings || this.settings; /** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown(); } } catch (err) { console.warn('[YouTube+] rebuildDownloadDropdown call failed:', err); } } if (target.id === 'ytp-plus-save-settings') { this.saveSettings(); modal.remove(); this.showNotification(t('settingsSaved')); } // Save specific Y2Mate customization if (target.id === 'download-y2mate-save') { // Ensure settings structure if (!this.settings.downloadSiteCustomization) { this.settings.downloadSiteCustomization = { y2mate: { name: 'Y2Mate', url: 'https://www.y2mate.com/youtube/{videoId}' }, xbbuddy: { name: '9xbuddy', url: 'https://9xbuddy.org/process?url={videoUrl}' }, }; } if (!this.settings.downloadSiteCustomization.y2mate) { this.settings.downloadSiteCustomization.y2mate = { name: '', url: '' }; } // Read current inputs inside this download-site-option const container = /** @type {HTMLElement|null} */ ( /** @type {unknown} */ (target.closest('.download-site-option')) ); if (container) { const nameInput = container.querySelector( 'input.download-site-input[data-site="y2mate"][data-field="name"]' ); const urlInput = container.querySelector( 'input.download-site-input[data-site="y2mate"][data-field="url"]' ); if (nameInput) this.settings.downloadSiteCustomization.y2mate.name = nameInput.value; if (urlInput) this.settings.downloadSiteCustomization.y2mate.url = urlInput.value; } this.saveSettings(); try { if ( typeof window !== 'undefined' && /** @type {any} */ (window).youtubePlus && typeof (/** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown) === 'function' ) { /** @type {any} */ (window).youtubePlus.settings = /** @type {any} */ (window).youtubePlus.settings || this.settings; /** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown(); } } catch (err) { console.warn('[YouTube+] rebuildDownloadDropdown call failed:', err); } this.showNotification(t('y2mateSettingsSaved')); } // Reset Y2Mate to defaults if (target.id === 'download-y2mate-reset') { if (!this.settings.downloadSiteCustomization) { // Initialize with expected structure to satisfy type checks this.settings.downloadSiteCustomization = { y2mate: { name: 'Y2Mate', url: 'https://www.y2mate.com/youtube/{videoId}' }, xbbuddy: { name: '9xbuddy', url: 'https://9xbuddy.org/process?url={videoUrl}' }, }; } this.settings.downloadSiteCustomization.y2mate = { name: 'Y2Mate', url: 'https://www.y2mate.com/youtube/{videoId}', }; // Update inputs in modal if present const container = /** @type {HTMLElement|null} */ ( /** @type {unknown} */ (modal.querySelector('.download-site-option')) ); if (container) { const nameInput = container.querySelector( 'input.download-site-input[data-site="y2mate"][data-field="name"]' ); const urlInput = container.querySelector( 'input.download-site-input[data-site="y2mate"][data-field="url"]' ); const nameDisplay = container.querySelector('.download-site-name'); if (nameInput) nameInput.value = this.settings.downloadSiteCustomization.y2mate.name; if (urlInput) urlInput.value = this.settings.downloadSiteCustomization.y2mate.url; if (nameDisplay) { nameDisplay.textContent = this.settings.downloadSiteCustomization.y2mate.name; } } this.saveSettings(); try { if ( typeof window !== 'undefined' && /** @type {any} */ (window).youtubePlus && typeof (/** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown) === 'function' ) { /** @type {any} */ (window).youtubePlus.settings = /** @type {any} */ (window).youtubePlus.settings || this.settings; /** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown(); } } catch (err) { console.warn('[YouTube+] rebuildDownloadDropdown call failed:', err); } this.showNotification(t('y2mateReset')); } // Save specific 9xBuddy customization if (target.id === 'download-xbbuddy-save') { if (!this.settings.downloadSiteCustomization) { // Initialize expected structure this.settings.downloadSiteCustomization = { y2mate: { name: 'Y2Mate', url: 'https://www.y2mate.com/youtube/{videoId}' }, xbbuddy: { name: '9xbuddy', url: 'https://9xbuddy.org/process?url={videoUrl}' }, }; } if (!this.settings.downloadSiteCustomization.xbbuddy) { this.settings.downloadSiteCustomization.xbbuddy = { name: '', url: '' }; } const container = /** @type {HTMLElement|null} */ ( /** @type {unknown} */ (target.closest('.download-site-option')) ); if (container) { const nameInput = container.querySelector( 'input.download-site-input[data-site="xbbuddy"][data-field="name"]' ); const urlInput = container.querySelector( 'input.download-site-input[data-site="xbbuddy"][data-field="url"]' ); if (nameInput) this.settings.downloadSiteCustomization.xbbuddy.name = nameInput.value; if (urlInput) this.settings.downloadSiteCustomization.xbbuddy.url = urlInput.value; } this.saveSettings(); try { if ( typeof window !== 'undefined' && /** @type {any} */ (window).youtubePlus && typeof (/** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown) === 'function' ) { /** @type {any} */ (window).youtubePlus.settings = /** @type {any} */ (window).youtubePlus.settings || this.settings; /** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown(); } } catch (err) { console.warn('[YouTube+] rebuildDownloadDropdown call failed:', err); } this.showNotification(t('xbuddySettingsSaved')); } // Reset 9xBuddy to defaults if (target.id === 'download-xbbuddy-reset') { if (!this.settings.downloadSiteCustomization) { this.settings.downloadSiteCustomization = { y2mate: { name: 'Y2Mate', url: 'https://www.y2mate.com/youtube/{videoId}' }, xbbuddy: { name: '9xbuddy', url: 'https://9xbuddy.org/process?url={videoUrl}' }, }; } this.settings.downloadSiteCustomization.xbbuddy = { name: '9xbuddy', url: 'https://9xbuddy.org/process?url={videoUrl}', }; // Update inputs in modal if present const container = /** @type {HTMLElement|null} */ ( /** @type {unknown} */ (modal.querySelectorAll('.download-site-option')[1]) ); if (container) { const nameInput = container.querySelector( 'input.download-site-input[data-site="xbbuddy"][data-field="name"]' ); const urlInput = container.querySelector( 'input.download-site-input[data-site="xbbuddy"][data-field="url"]' ); const nameDisplay = container.querySelector('.download-site-name'); if (nameInput) nameInput.value = this.settings.downloadSiteCustomization.xbbuddy.name; if (urlInput) urlInput.value = this.settings.downloadSiteCustomization.xbbuddy.url; if (nameDisplay) { nameDisplay.textContent = this.settings.downloadSiteCustomization.xbbuddy.name; } } this.saveSettings(); try { if ( typeof window !== 'undefined' && /** @type {any} */ (window).youtubePlus && typeof (/** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown) === 'function' ) { /** @type {any} */ (window).youtubePlus.settings = /** @type {any} */ (window).youtubePlus.settings || this.settings; /** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown(); } } catch (err) { console.warn('[YouTube+] rebuildDownloadDropdown call failed:', err); } this.showNotification(t('xbuddyReset')); } }); // Обработка изменений input полей для кастомизации modal.addEventListener('input', e => { const target = /** @type {EventTarget & HTMLElement} */ (e.target); if (target.classList.contains('download-site-input')) { const site = /** @type {HTMLElement} */ (target).dataset.site; const field = /** @type {HTMLElement} */ (target).dataset.field; if (!site || !field) return; if (!this.settings.downloadSiteCustomization) { this.settings.downloadSiteCustomization = { y2mate: { name: 'Y2Mate', url: 'https://www.y2mate.com/youtube/{videoId}' }, xbbuddy: { name: '9xbuddy', url: 'https://9xbuddy.org/process?url={videoUrl}' }, }; } if (!this.settings.downloadSiteCustomization[site]) { this.settings.downloadSiteCustomization[site] = { name: '', url: '' }; } this.settings.downloadSiteCustomization[site][field] = /** @type {HTMLInputElement} */ ( target ).value; // Обновить имя в UI в реальном времени if (field === 'name') { const nameDisplay = /** @type {HTMLElement|null} */ ( /** @type {unknown} */ (target.closest('.download-site-option')) )?.querySelector('.download-site-name'); if (nameDisplay) { nameDisplay.textContent = /** @type {HTMLInputElement} */ (target).value || (site === 'y2mate' ? 'Y2Mate' : '9xbuddy'); } } // Rebuild dropdown if present so changes reflect immediately try { if ( typeof window !== 'undefined' && /** @type {any} */ (window).youtubePlus && typeof (/** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown) === 'function' ) { /** @type {any} */ (window).youtubePlus.settings = /** @type {any} */ (window).youtubePlus.settings || this.settings; /** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown(); } } catch (err) { console.warn('[YouTube+] rebuildDownloadDropdown call failed:', err); } } }); return modal; }, openSettingsModal() { const existingModal = this.getElement('.ytp-plus-settings-modal', false); if (existingModal) existingModal.remove(); document.body.appendChild(this.createSettingsModal()); }, waitForElement(selector, timeout = 5000) { // ✅ Use centralized utility return YouTubeUtils.waitForElement(selector, timeout); }, addCustomButtons() { const controls = this.getElement('.ytp-right-controls'); if (!controls) return; if (!this.getElement('.ytp-screenshot-button')) this.addScreenshotButton(controls); if (!this.getElement('.ytp-download-button')) this.addDownloadButton(controls); if (!this.getElement('.speed-control-btn')) this.addSpeedControlButton(controls); if (!document.getElementById('speed-indicator')) { const indicator = document.createElement('div'); indicator.id = 'speed-indicator'; const player = document.getElementById('movie_player'); if (player) player.appendChild(indicator); } this.handleFullscreenChange(); }, addScreenshotButton(controls) { const button = document.createElement('button'); button.className = 'ytp-button ytp-screenshot-button'; button.setAttribute('title', t('takeScreenshot')); button.innerHTML = ` `; button.addEventListener('click', this.captureFrame.bind(this)); controls.insertBefore(button, controls.firstChild); }, addDownloadButton(controls) { if (!this.settings.enableDownload) return; const button = document.createElement('div'); button.className = 'ytp-button ytp-download-button'; button.setAttribute('title', t('downloadOptions')); button.setAttribute('tabindex', '0'); button.setAttribute('role', 'button'); button.setAttribute('aria-haspopup', 'true'); button.setAttribute('aria-expanded', 'false'); button.style.display = 'inline-block'; button.style.padding = '0 10px 0 0'; button.style.height = '36px'; button.innerHTML = ` `; // Dropdown options const options = document.createElement('div'); options.className = 'download-options'; options.setAttribute('role', 'menu'); // Position dropdown below button function positionDropdown() { const rect = button.getBoundingClientRect(); options.style.left = `${rect.left + rect.width / 2 - 75}px`; options.style.bottom = `${window.innerHeight - rect.top + 12}px`; } // Helper to open download site function openDownloadSite(url, isYTDL = false) { if (isYTDL) { // For YTDL: copy video URL to clipboard and open localhost const videoId = new URLSearchParams(location.search).get('v'); const videoUrl = videoId ? `https://www.youtube.com/watch?v=${videoId}` : location.href; // Copy to clipboard navigator.clipboard .writeText(videoUrl) .then(() => { // Show notification YouTubeUtils.NotificationManager.show(t('copiedToClipboard'), { duration: 2000, type: 'success', }); }) .catch(() => { // Fallback for older browsers const input = document.createElement('input'); input.value = videoUrl; document.body.appendChild(input); input.select(); document.execCommand('copy'); document.body.removeChild(input); YouTubeUtils.NotificationManager.show(t('copiedToClipboard'), { duration: 2000, type: 'success', }); }); // Open YTDL in new tab window.open(url, '_blank'); } else { window.open(url, '_blank'); } options.classList.remove('visible'); button.setAttribute('aria-expanded', 'false'); } // Helper to rebuild the dropdown if settings changed while dropdown exists // Exposed on button element via dataset so external handlers can trigger a rebuild function rebuildDropdown() { try { // Remove existing list if present const existingList = options.querySelector('.download-options-list'); if (existingList) existingList.remove(); // Rebuild downloadSites from current settings const customizationNow = typeof window !== 'undefined' && /** @type {any} */ (window).youtubePlus && /** @type {any} */ (window).youtubePlus.settings && /** @type {any} */ (window).youtubePlus.settings.downloadSiteCustomization ? /** @type {any} */ (window).youtubePlus.settings.downloadSiteCustomization : customization; const videoIdNow = new URLSearchParams(location.search).get('v'); const videoUrlNow = videoIdNow ? `https://www.youtube.com/watch?v=${videoIdNow}` : location.href; const buildUrlNow = template => (template || '') .replace('{videoId}', videoIdNow || '') .replace('{videoUrl}', encodeURIComponent(videoUrlNow)); const baseSitesNow = [ { key: 'y2mate', name: customizationNow?.y2mate?.name || 'Y2Mate', url: buildUrlNow( customizationNow?.y2mate?.url || `https://www.y2mate.com/youtube/{videoId}` ), isYTDL: false, }, { key: 'xbbuddy', name: customizationNow?.xbbuddy?.name || '9xbuddy', url: buildUrlNow( customizationNow?.xbbuddy?.url || `https://9xbuddy.org/process?url={videoUrl}` ), isYTDL: false, }, { key: 'ytdl', name: 'by YTDL', url: `http://localhost:5005`, isYTDL: true }, ]; const enabledSitesNow = typeof window !== 'undefined' && /** @type {any} */ (window).youtubePlus && /** @type {any} */ (window).youtubePlus.settings && /** @type {any} */ (window).youtubePlus.settings.downloadSites ? /** @type {any} */ (window).youtubePlus.settings.downloadSites : enabledSites; const downloadSitesNow = baseSitesNow.filter(s => { if (s.key === 'ytdl') return true; return enabledSitesNow[s.key] !== false; }); // If only one site remains replace click handler if (downloadSitesNow.length === 1) { const single = downloadSitesNow[0]; // Remove any existing clickable handlers on button button.replaceWith(button.cloneNode(true)); const newButton = controls.querySelector('.ytp-download-button'); if (newButton) { newButton.addEventListener('click', () => openDownloadSite(single.url, single.isYTDL) ); } return; } // Build new list const newList = document.createElement('div'); newList.className = 'download-options-list'; downloadSitesNow.forEach(site => { const opt = document.createElement('div'); opt.className = 'download-option-item'; opt.textContent = site.name; opt.setAttribute('role', 'menuitem'); opt.setAttribute('tabindex', '0'); opt.addEventListener('click', () => openDownloadSite(site.url, site.isYTDL)); opt.addEventListener('keydown', e => { if (e.key === 'Enter' || e.key === ' ') openDownloadSite(site.url, site.isYTDL); }); newList.appendChild(opt); }); options.appendChild(newList); } catch (err) { console.warn('[YouTube+] rebuildDropdown failed:', err); } } // Get current video URL const videoId = new URLSearchParams(location.search).get('v'); const videoUrl = videoId ? `https://www.youtube.com/watch?v=${videoId}` : location.href; // Получить кастомные настройки или использовать defaults const customization = this.settings.downloadSiteCustomization || { y2mate: { name: 'Y2Mate', url: 'https://www.y2mate.com/youtube/{videoId}' }, xbbuddy: { name: '9xbuddy', url: 'https://9xbuddy.org/process?url={videoUrl}' }, }; // Функция для замены плейсхолдеров в URL const buildUrl = template => { return template .replace('{videoId}', videoId || '') .replace('{videoUrl}', encodeURIComponent(videoUrl)); }; // List of download sites (ytdl всегда включён, filter by user settings.downloadSites для остальных) const baseSites = [ { key: 'y2mate', name: customization.y2mate?.name || 'Y2Mate', url: buildUrl(customization.y2mate?.url || `https://www.y2mate.com/youtube/{videoId}`), isYTDL: false, }, { key: 'xbbuddy', name: customization.xbbuddy?.name || '9xbuddy', url: buildUrl(customization.xbbuddy?.url || `https://9xbuddy.org/process?url={videoUrl}`), isYTDL: false, }, { key: 'ytdl', name: 'by YTDL', url: `http://localhost:5005`, isYTDL: true }, ]; const enabledSites = this.settings && this.settings.downloadSites ? this.settings.downloadSites : { y2mate: true, xbbuddy: true }; // YTDL всегда включён, фильтруем остальные по настройкам const downloadSites = baseSites.filter(s => { if (s.key === 'ytdl') return true; // ytdl всегда включён return enabledSites[s.key] !== false; }); // Если активен только один сайт — прямой переход без dropdown if (downloadSites.length === 1) { const singleSite = downloadSites[0]; button.style.cursor = 'pointer'; button.addEventListener('click', () => openDownloadSite(singleSite.url, singleSite.isYTDL)); controls.insertBefore(button, controls.firstChild); return; // Не создаём dropdown } // Centered list const list = document.createElement('div'); list.className = 'download-options-list'; downloadSites.forEach(site => { const opt = document.createElement('div'); opt.className = 'download-option-item'; opt.textContent = site.name; opt.setAttribute('role', 'menuitem'); opt.setAttribute('tabindex', '0'); opt.addEventListener('click', () => openDownloadSite(site.url, site.isYTDL)); opt.addEventListener('keydown', e => { if (e.key === 'Enter' || e.key === ' ') { openDownloadSite(site.url, site.isYTDL); } }); list.appendChild(opt); }); options.appendChild(list); button.appendChild(options); // Expose rebuild function globally (safe guard) so settings handlers can call it try { if (typeof window !== 'undefined') { /** @type {any} */ (window).youtubePlus = /** @type {any} */ (window).youtubePlus || {}; /** @type {any} */ (window).youtubePlus.rebuildDownloadDropdown = rebuildDropdown; // also store settings ref for rebuildDropdown to read /** @type {any} */ (window).youtubePlus.settings = /** @type {any} */ (window).youtubePlus.settings || this.settings; } } catch (e) { console.warn('[YouTube+] expose rebuildDownloadDropdown failed:', e); } let dropdownTimeout; function showDropdown() { clearTimeout(dropdownTimeout); positionDropdown(); options.classList.add('visible'); button.setAttribute('aria-expanded', 'true'); } function hideDropdown() { dropdownTimeout = setTimeout(() => { options.classList.remove('visible'); button.setAttribute('aria-expanded', 'false'); }, 150); } button.addEventListener('mouseenter', showDropdown); button.addEventListener('mouseleave', hideDropdown); options.addEventListener('mouseenter', showDropdown); options.addEventListener('mouseleave', hideDropdown); button.addEventListener('keydown', e => { if (e.key === 'Enter' || e.key === ' ') { if (options.classList.contains('visible')) { hideDropdown(); } else { showDropdown(); } } }); controls.insertBefore(button, controls.firstChild); }, addSpeedControlButton(controls) { const speedBtn = document.createElement('div'); speedBtn.className = 'ytp-button speed-control-btn'; speedBtn.innerHTML = `${this.speedControl.currentSpeed}×`; const speedOptions = document.createElement('div'); speedOptions.className = 'speed-options'; [0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0, 3.0].forEach(speed => { const option = document.createElement('div'); option.className = `speed-option-item${Number(speed) === this.speedControl.currentSpeed ? ' speed-option-active' : ''}`; option.textContent = `${speed}x`; option.dataset.speed = String(speed); option.addEventListener('click', () => this.changeSpeed(speed)); speedOptions.appendChild(option); }); speedBtn.appendChild(speedOptions); let isHovering = false; speedBtn.addEventListener('mouseenter', () => { isHovering = true; speedOptions.style.display = 'block'; }); speedBtn.addEventListener('mouseleave', () => { isHovering = false; setTimeout(() => { if (!isHovering) speedOptions.style.display = 'none'; }, 150); }); controls.insertBefore(speedBtn, controls.firstChild); }, captureFrame() { const video = this.getElement('video', false); if (!video) return; const canvas = document.createElement('canvas'); canvas.width = video.videoWidth; canvas.height = video.videoHeight; const ctx = canvas.getContext('2d'); ctx.drawImage(video, 0, 0, canvas.width, canvas.height); const videoTitle = document.title.replace(/\s-\sYouTube$/, '').trim(); const link = document.createElement('a'); link.href = canvas.toDataURL('image/png'); link.download = `${videoTitle}.png`; link.click(); }, showNotification(message, duration = 2000) { YouTubeUtils.NotificationManager.show(message, { duration, type: 'info' }); }, handleFullscreenChange() { const isFullscreen = document.fullscreenElement || document.webkitFullscreenElement; document.querySelectorAll('.ytp-screenshot-button, .ytp-cobalt-button').forEach(button => { button.style.bottom = isFullscreen ? '15px' : '12px'; }); }, changeSpeed(speed) { speed = Number(speed); this.speedControl.currentSpeed = speed; localStorage.setItem(this.speedControl.storageKey, String(speed)); const speedBtn = this.getElement('.speed-control-btn span', false); if (speedBtn) speedBtn.textContent = `${speed}×`; document.querySelectorAll('.speed-option-item').forEach(option => { option.classList.toggle('speed-option-active', parseFloat(option.dataset.speed) === speed); }); this.applyCurrentSpeed(); this.showSpeedIndicator(speed); }, applyCurrentSpeed() { document.querySelectorAll('video').forEach(video => { if (video && video.playbackRate !== this.speedControl.currentSpeed) { video.playbackRate = this.speedControl.currentSpeed; } }); }, setupVideoObserver() { if (this._speedInterval) clearInterval(this._speedInterval); this._speedInterval = setInterval(() => this.applyCurrentSpeed(), 1000); // ✅ Register interval in cleanupManager YouTubeUtils.cleanupManager.registerInterval(this._speedInterval); }, setupNavigationObserver() { let lastUrl = location.href; document.addEventListener('fullscreenchange', this.handleFullscreenChange.bind(this)); document.addEventListener('yt-navigate-finish', () => { if (location.href.includes('watch?v=')) this.setupCurrentPage(); this.addSettingsButtonToHeader(); }); // ✅ Register observer in cleanupManager const observer = new MutationObserver(() => { if (lastUrl !== location.href) { lastUrl = location.href; if (location.href.includes('watch?v=')) { setTimeout(() => this.setupCurrentPage(), 500); } this.addSettingsButtonToHeader(); } }); YouTubeUtils.cleanupManager.registerObserver(observer); // ✅ Safe observe with document.body check if (document.body) { observer.observe(document.body, { childList: true, subtree: true }); } else { document.addEventListener('DOMContentLoaded', () => { observer.observe(document.body, { childList: true, subtree: true }); }); } }, showSpeedIndicator(speed) { const indicator = document.getElementById('speed-indicator'); if (!indicator) return; if (this.speedControl.activeAnimationId) { cancelAnimationFrame(this.speedControl.activeAnimationId); YouTubeUtils.cleanupManager.unregisterAnimationFrame(this.speedControl.activeAnimationId); this.speedControl.activeAnimationId = null; } indicator.textContent = `${speed}×`; indicator.style.display = 'block'; indicator.style.opacity = '0.8'; const startTime = performance.now(); const fadeOut = timestamp => { const elapsed = timestamp - startTime; const progress = Math.min(elapsed / 1500, 1); indicator.style.opacity = String(0.8 * (1 - progress)); if (progress < 1) { this.speedControl.activeAnimationId = YouTubeUtils.cleanupManager.registerAnimationFrame( requestAnimationFrame(fadeOut) ); } else { indicator.style.display = 'none'; this.speedControl.activeAnimationId = null; } }; this.speedControl.activeAnimationId = YouTubeUtils.cleanupManager.registerAnimationFrame( requestAnimationFrame(fadeOut) ); }, }; // Save reference to init function BEFORE IIFE closes (critical for DOMContentLoaded) const initFunction = YouTubeEnhancer.init.bind(YouTubeEnhancer); // Initialize immediately or on DOMContentLoaded if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initFunction); } else { initFunction(); } })(); // --- MODULE: enhanced.js --- // Enhanced Tabviews (function () { 'use strict'; // Localization const i18n = { en: { scrollToTop: 'Scroll to top', }, ru: { scrollToTop: 'Прокрутить вверх', }, }; const getLanguage = () => { const htmlLang = document.documentElement.lang || 'en'; if (htmlLang.startsWith('ru')) return 'ru'; return 'en'; }; const lang = getLanguage(); const t = key => i18n[lang][key] || i18n.en[key] || key; /** * Configuration object for scroll-to-top button * @type {Object} * @property {boolean} enabled - Whether the feature is enabled * @property {string} storageKey - LocalStorage key for settings */ const config = { enabled: true, storageKey: 'youtube_top_button_settings', }; /** * Adds CSS styles for scroll-to-top button and scrollbars * @returns {void} */ const addStyles = () => { if (document.getElementById('custom-styles')) return; const style = document.createElement('style'); style.id = 'custom-styles'; style.textContent = ` :root{--scrollbar-width:8px;--scrollbar-track:transparent;--scrollbar-thumb:rgba(144,144,144,.5);--scrollbar-thumb-hover:rgba(170,170,170,.7);--scrollbar-thumb-active:rgba(190,190,190,.9);} ::-webkit-scrollbar{width:var(--scrollbar-width)!important;height:var(--scrollbar-width)!important;} ::-webkit-scrollbar-track{background:var(--scrollbar-track)!important;border-radius:4px!important;} ::-webkit-scrollbar-thumb{background:var(--scrollbar-thumb)!important;border-radius:4px!important;transition:background .2s!important;} ::-webkit-scrollbar-thumb:hover{background:var(--scrollbar-thumb-hover)!important;} ::-webkit-scrollbar-thumb:active{background:var(--scrollbar-thumb-active)!important;} ::-webkit-scrollbar-corner{background:transparent!important;} *{scrollbar-width:thin;scrollbar-color:var(--scrollbar-thumb) var(--scrollbar-track);} html[dark]{--scrollbar-thumb:rgba(144,144,144,.4);--scrollbar-thumb-hover:rgba(170,170,170,.6);--scrollbar-thumb-active:rgba(190,190,190,.8);} .top-button{position:absolute;bottom:16px;right:16px;width:40px;height:40px;background:var(--yt-top-btn-bg,rgba(0,0,0,.7));color:var(--yt-top-btn-color,#fff);border:none;border-radius:50%;cursor:pointer;display:flex;align-items:center;justify-content:center;z-index:1000;opacity:0;visibility:hidden;transition:all .3s;backdrop-filter:blur(12px) saturate(180%);-webkit-backdrop-filter:blur(12px) saturate(180%);border:1px solid var(--yt-top-btn-border,rgba(255,255,255,.1));background:rgba(255,255,255,.12);box-shadow:0 8px 32px 0 rgba(31,38,135,.18);} .top-button:hover{background:var(--yt-top-btn-hover,rgba(0,0,0,.15));transform:translateY(-2px) scale(1.07);box-shadow:0 8px 32px rgba(0,0,0,.25);} .top-button.visible{opacity:1;visibility:visible;} .top-button svg{transition:transform .2s;} .top-button:hover svg{transform:translateY(-1px) scale(1.1);} html[dark]{--yt-top-btn-bg:rgba(255,255,255,.10);--yt-top-btn-color:#fff;--yt-top-btn-border:rgba(255,255,255,.18);--yt-top-btn-hover:rgba(255,255,255,.18);} html:not([dark]){--yt-top-btn-bg:rgba(255,255,255,.12);--yt-top-btn-color:#222;--yt-top-btn-border:rgba(0,0,0,.08);--yt-top-btn-hover:rgba(255,255,255,.18);} ytd-watch-flexy:not([tyt-tab^="#"]) .top-button{display:none;} ytd-watch-flexy[flexy] #movie_player, ytd-watch-flexy[flexy] #movie_player .html5-video-container, ytd-watch-flexy[flexy] .html5-main-video{ width:100%!important; max-width:100%!important; } ytd-watch-flexy[flexy] .html5-main-video{ height:auto!important; max-height:100%!important; object-fit:contain!important; transform:none!important; } ytd-watch-flexy[flexy] #player-container-outer, ytd-watch-flexy[flexy] #movie_player{ display:flex!important; align-items:center!important; justify-content:center!important; } `; document.head.appendChild(style); }; /** * Updates button visibility based on scroll position * @param {HTMLElement} scrollContainer - The container being scrolled * @returns {void} */ const handleScroll = scrollContainer => { const button = document.getElementById('right-tabs-top-button'); if (!button || !scrollContainer) return; button.classList.toggle('visible', scrollContainer.scrollTop > 100); }; /** * Sets up scroll event listener on active tab * @returns {void} */ const setupScrollListener = () => { document.querySelectorAll('.tab-content-cld').forEach(tab => { tab.removeEventListener('scroll', tab._topButtonScrollHandler); }); const activeTab = document.querySelector( '#right-tabs .tab-content-cld:not(.tab-content-hidden)' ); if (activeTab) { const scrollHandler = () => handleScroll(activeTab); activeTab._topButtonScrollHandler = scrollHandler; activeTab.addEventListener('scroll', scrollHandler, { passive: true }); handleScroll(activeTab); } }; /** * Creates and appends scroll-to-top button * @returns {void} */ const createButton = () => { const rightTabs = document.querySelector('#right-tabs'); if (!rightTabs || document.getElementById('right-tabs-top-button')) return; if (!config.enabled) return; const button = document.createElement('button'); button.id = 'right-tabs-top-button'; button.className = 'top-button'; button.title = t('scrollToTop'); button.innerHTML = ''; button.addEventListener('click', () => { const activeTab = document.querySelector( '#right-tabs .tab-content-cld:not(.tab-content-hidden)' ); if (activeTab) activeTab.scrollTo({ top: 0, behavior: 'smooth' }); }); rightTabs.style.position = 'relative'; rightTabs.appendChild(button); setupScrollListener(); }; /** * Observes DOM changes to detect tab switches * @returns {void} */ const observeTabChanges = () => { const observer = new MutationObserver(mutations => { if ( mutations.some( m => m.type === 'attributes' && m.attributeName === 'class' && m.target instanceof Element && m.target.classList.contains('tab-content-cld') ) ) { setTimeout(setupScrollListener, 100); } }); const rightTabs = document.querySelector('#right-tabs'); if (rightTabs) { observer.observe(rightTabs, { attributes: true, subtree: true, attributeFilter: ['class'], }); } }; // Events const setupEvents = () => { document.addEventListener( 'click', e => { const target = /** @type {EventTarget & HTMLElement} */ (e.target); if (target.closest && target.closest('.tab-btn[tyt-tab-content]')) { setTimeout(setupScrollListener, 100); } }, true ); }; // Initialize const init = () => { addStyles(); setupEvents(); const checkForTabs = () => { if (document.querySelector('#right-tabs')) { createButton(); observeTabChanges(); } else { setTimeout(checkForTabs, 500); } }; checkForTabs(); }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })(); // YouTube End Screen Remover (function () { 'use strict'; // Optimized configuration const CONFIG = { enabled: true, storageKey: 'youtube_endscreen_settings', selectors: '.ytp-ce-element-show,.ytp-ce-element,.ytp-endscreen-element,.ytp-ce-covering-overlay,.ytp-cards-teaser,.ytp-cards-button,.iv-drawer,.video-annotations', debounceMs: 32, batchSize: 20, }; // Minimal state with better tracking const state = { observer: null, styleEl: null, isActive: false, removeCount: 0, lastCheck: 0, ytNavigateListenerKey: null, settingsNavListenerKey: null, }; // High-performance utilities: use shared debounce when available const debounce = (fn, ms) => { try { return ( (window.YouTubeUtils && window.YouTubeUtils.debounce) || ((f, t) => { let id; return (...args) => { clearTimeout(id); id = setTimeout(() => f(...args), t); }; })(fn, ms) ); } catch { let id; return (...args) => { clearTimeout(id); id = setTimeout(() => fn(...args), ms); }; } }; const fastRemove = elements => { const len = Math.min(elements.length, CONFIG.batchSize); for (let i = 0; i < len; i++) { const el = elements[i]; if (el?.isConnected) { el.style.cssText = 'display:none!important;visibility:hidden!important'; try { el.remove(); state.removeCount++; } catch { } } } }; // Settings with caching const settings = { load: () => { try { const data = localStorage.getItem(CONFIG.storageKey); CONFIG.enabled = data ? (JSON.parse(data).enabled ?? true) : true; } catch { CONFIG.enabled = true; } }, save: () => { try { localStorage.setItem(CONFIG.storageKey, JSON.stringify({ enabled: CONFIG.enabled })); } catch { } settings.apply(); }, apply: () => (CONFIG.enabled ? init() : cleanup()), }; // Optimized core functions const injectCSS = () => { if (state.styleEl || !CONFIG.enabled) return; // ✅ Use StyleManager instead of createElement('style') const styles = `${CONFIG.selectors}{display:none!important;opacity:0!important;visibility:hidden!important;pointer-events:none!important;transform:scale(0)!important}`; YouTubeUtils.StyleManager.add('end-screen-remover', styles); state.styleEl = true; // Mark as added }; const removeEndScreens = () => { if (!CONFIG.enabled) return; const now = performance.now(); if (now - state.lastCheck < CONFIG.debounceMs) return; state.lastCheck = now; const elements = document.querySelectorAll(CONFIG.selectors); if (elements.length) fastRemove(elements); }; const setupWatcher = () => { if (state.observer || !CONFIG.enabled) return; const throttledRemove = debounce(removeEndScreens, CONFIG.debounceMs); state.observer = new MutationObserver(mutations => { let hasRelevantChanges = false; for (const { addedNodes } of mutations) { for (const node of addedNodes) { if ( node instanceof Element && (node.className?.includes('ytp-') || node.querySelector?.('.ytp-ce-element')) ) { hasRelevantChanges = true; break; } } if (hasRelevantChanges) break; } if (hasRelevantChanges) throttledRemove(); }); // ✅ Register observer in cleanupManager YouTubeUtils.cleanupManager.registerObserver(state.observer); const target = document.querySelector('#movie_player') || document.body; state.observer.observe(target, { childList: true, subtree: true, attributeFilter: ['class', 'style'], }); }; const cleanup = () => { state.observer?.disconnect(); state.observer = null; state.styleEl?.remove(); state.styleEl = null; state.isActive = false; }; const init = () => { if (state.isActive || !CONFIG.enabled) return; state.isActive = true; injectCSS(); removeEndScreens(); setupWatcher(); }; // Streamlined settings UI const addSettingsUI = () => { const section = document.querySelector('.ytp-plus-settings-section[data-section="advanced"]'); if (!section || section.querySelector('.endscreen-settings')) return; const container = document.createElement('div'); container.className = 'ytp-plus-settings-item endscreen-settings'; container.innerHTML = `
Remove end screen suggestions and info cards${state.removeCount ? ` (${state.removeCount} removed)` : ''}
`; section.appendChild(container); container.querySelector('input').addEventListener( 'change', e => { const target = /** @type {EventTarget & HTMLInputElement} */ (e.target); CONFIG.enabled = target.checked; settings.save(); }, { passive: true } ); }; // Optimized navigation handler const handlePageChange = debounce(() => { if (location.pathname === '/watch') { cleanup(); requestIdleCallback ? requestIdleCallback(init) : setTimeout(init, 1); } }, 50); // Initialize settings.load(); if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init, { once: true }); } else { init(); } const handleSettingsNavClick = e => { const target = /** @type {EventTarget & HTMLElement} */ (e.target); if (target.dataset?.section === 'advanced') { setTimeout(addSettingsUI, 10); } }; if (!state.ytNavigateListenerKey) { state.ytNavigateListenerKey = YouTubeUtils.cleanupManager.registerListener( document, 'yt-navigate-finish', handlePageChange, { passive: true } ); } // Settings modal integration const settingsObserver = new MutationObserver(mutations => { for (const { addedNodes } of mutations) { for (const node of addedNodes) { if (node instanceof Element && node.classList?.contains('ytp-plus-settings-modal')) { setTimeout(addSettingsUI, 25); return; } } } }); // ✅ Register observer in cleanupManager YouTubeUtils.cleanupManager.registerObserver(settingsObserver); // ✅ Safe observe with document.body check if (document.body) { settingsObserver.observe(document.body, { childList: true }); } else { document.addEventListener('DOMContentLoaded', () => { settingsObserver.observe(document.body, { childList: true }); }); } if (!state.settingsNavListenerKey) { state.settingsNavListenerKey = YouTubeUtils.cleanupManager.registerListener( document, 'click', handleSettingsNavClick, { passive: true, capture: true } ); } })(); // Time to Read (Resume Playback) (function () { 'use strict'; const RESUME_STORAGE_KEY = 'youtube_resume_times_v1'; const OVERLAY_ID = 'yt-resume-overlay'; const AUTO_HIDE_MS = 20000; // hide overlay after 20s // Localization const i18n = { en: { resumePlayback: 'Resume playback?', resume: 'Resume', startOver: 'Start over', }, ru: { resumePlayback: 'Продолжить воспроизведение?', resume: 'Продолжить', startOver: 'Начать сначала', }, }; // Detect language const getLanguage = () => { const htmlLang = document.documentElement.lang || 'en'; if (htmlLang.startsWith('ru')) return 'ru'; return 'en'; }; const lang = getLanguage(); const t = key => i18n[lang][key] || i18n.en[key] || key; const readStorage = () => { try { return JSON.parse(localStorage.getItem(RESUME_STORAGE_KEY) || '{}'); } catch { return {}; } }; const writeStorage = obj => { try { localStorage.setItem(RESUME_STORAGE_KEY, JSON.stringify(obj)); } catch { } }; // Get current video id from the page (works on standard watch pages) const getVideoId = () => { try { const meta = document.querySelector('link[rel="canonical"]'); if (meta && meta.href) { const u = new URL(meta.href); return u.searchParams.get('v') || (u.pathname && u.pathname.split('/').pop()); } // Fallback to ytInitialPlayerResponse return ( (window.ytInitialPlayerResponse && window.ytInitialPlayerResponse.videoDetails && window.ytInitialPlayerResponse.videoDetails.videoId) || null ); } catch { return null; } }; const createOverlay = (seconds, onResume, onRestart) => { if (document.getElementById(OVERLAY_ID)) return null; const wrap = document.createElement('div'); wrap.id = OVERLAY_ID; // Try to insert overlay inside the player so it appears above the progress bar const player = document.querySelector('#movie_player'); const inPlayer = !!player; // Ensure glassmorphism styles are available for the overlay const resumeOverlayStyles = ` .ytpa-resume-overlay{min-width:180px;max-width:36vw;background:rgba(24, 24, 24, 0.3);color:var(--yt-spec-text-primary,#fff);padding:12px 14px;border-radius:12px;backdrop-filter:blur(8px) saturate(150%);-webkit-backdrop-filter:blur(8px) saturate(150%);box-shadow:0 14px 40px rgba(0,0,0,0.48);border:1.25px solid rgba(255,255,255,0.06);font-family:Arial,Helvetica,sans-serif;display:flex;flex-direction:column;align-items:center;text-align:center} .ytpa-resume-overlay .ytpa-resume-title{font-weight:600;margin-bottom:8px} .ytpa-resume-overlay .ytpa-resume-actions{display:flex;gap:8px;justify-content:center;margin-top:6px} .ytpa-resume-overlay .ytpa-resume-btn{padding:6px 12px;border-radius:8px;border:none;cursor:pointer} .ytpa-resume-overlay .ytpa-resume-btn.primary{background:#1e88e5;color:#fff} .ytpa-resume-overlay .ytpa-resume-btn.ghost{background:rgba(255,255,255,0.06);color:#fff} `; try { if (window.YouTubeUtils && YouTubeUtils.StyleManager) { YouTubeUtils.StyleManager.add('ytpa-resume-overlay-styles', resumeOverlayStyles); } else if (!document.getElementById('ytpa-resume-overlay-styles')) { const s = document.createElement('style'); s.id = 'ytpa-resume-overlay-styles'; s.textContent = resumeOverlayStyles; document.head.appendChild(s); } } catch { } if (inPlayer) { try { // Ensure player can be a positioning context const playerStyle = window.getComputedStyle(/** @type {Element} */(/** @type {unknown} */ (player))); if (playerStyle.position === 'static') player.style.position = 'relative'; } catch { } // Position centered inside the player wrap.className = 'ytpa-resume-overlay'; // absolute center (use transform to center by both axes) wrap.style.cssText = 'position:absolute;left:50%;bottom:5%;transform:translate(-50%,-50%);z-index:9999;pointer-events:auto;'; player.appendChild(wrap); } else { // Fallback: fixed centered on the page wrap.className = 'ytpa-resume-overlay'; wrap.style.cssText = 'position:fixed;left:50%;bottom:5%;transform:translate(-50%,-50%);z-index:1200;pointer-events:auto;'; document.body.appendChild(wrap); } const title = document.createElement('div'); title.className = 'ytpa-resume-title'; title.textContent = `${t('resumePlayback')} (${formatTime(seconds)})`; const btnResume = document.createElement('button'); btnResume.className = 'ytpa-resume-btn primary'; btnResume.textContent = t('resume'); const btnRestart = document.createElement('button'); btnRestart.className = 'ytpa-resume-btn ghost'; btnRestart.textContent = t('startOver'); btnResume.addEventListener('click', () => { try { onResume(); } catch { } try { wrap.remove(); } catch { } }); btnRestart.addEventListener('click', () => { try { onRestart(); } catch { } try { wrap.remove(); } catch { } }); // group actions and center them const actions = document.createElement('div'); actions.className = 'ytpa-resume-actions'; actions.appendChild(btnResume); actions.appendChild(btnRestart); wrap.appendChild(title); wrap.appendChild(actions); const to = setTimeout(() => { try { wrap.remove(); } catch { } }, AUTO_HIDE_MS); // Return function to cancel timeout const cancel = () => clearTimeout(to); // Register cleanup: cancel timeout and remove overlay when cleanup runs if (window.YouTubeUtils && YouTubeUtils.cleanupManager) { YouTubeUtils.cleanupManager.register(() => { try { cancel(); } catch { } try { wrap.remove(); } catch { } }); } return cancel; }; const formatTime = secs => { const s = Math.floor(secs % 60) .toString() .padStart(2, '0'); const m = Math.floor((secs / 60) % 60).toString(); const h = Math.floor(secs / 3600); return h ? `${h}:${m.padStart(2, '0')}:${s}` : `${m}:${s}`; }; const attachResumeHandlers = videoEl => { if (!videoEl) return; const vid = getVideoId(); if (!vid) return; const storage = readStorage(); const saved = storage[vid]; // Save current time using `timeupdate` event (throttled) instead of interval let timeUpdateHandler = null; let lastSavedAt = 0; const SAVE_THROTTLE_MS = 800; // minimum ms between writes const startSaving = () => { if (timeUpdateHandler) return; timeUpdateHandler = () => { try { const t = Math.floor(videoEl.currentTime || 0); const now = Date.now(); if (t && (!lastSavedAt || now - lastSavedAt > SAVE_THROTTLE_MS)) { const s = readStorage(); s[vid] = t; writeStorage(s); lastSavedAt = now; } } catch { } }; videoEl.addEventListener('timeupdate', timeUpdateHandler, { passive: true }); // register cleanup to remove listener if (window.YouTubeUtils && YouTubeUtils.cleanupManager) { YouTubeUtils.cleanupManager.register(() => { try { videoEl.removeEventListener('timeupdate', timeUpdateHandler); } catch { } }); } }; const stopSaving = () => { if (!timeUpdateHandler) return; try { videoEl.removeEventListener('timeupdate', timeUpdateHandler); } catch { } timeUpdateHandler = null; lastSavedAt = 0; }; // If saved time exists and is > 5s, show overlay if (saved && saved > 5 && !document.getElementById(OVERLAY_ID)) { const cancelTimeout = createOverlay( saved, () => { try { videoEl.currentTime = saved; videoEl.play(); } catch { } }, () => { try { videoEl.currentTime = 0; videoEl.play(); } catch { } } ); // register cleanup for overlay timeout if (window.YouTubeUtils && YouTubeUtils.cleanupManager && cancelTimeout) { YouTubeUtils.cleanupManager.register(cancelTimeout); } } // Start saving when playing const onPlay = () => startSaving(); const onPause = () => stopSaving(); videoEl.addEventListener('play', onPlay, { passive: true }); videoEl.addEventListener('pause', onPause, { passive: true }); // Cleanup listeners when needed if (window.YouTubeUtils && YouTubeUtils.cleanupManager) { YouTubeUtils.cleanupManager.register(() => { try { videoEl.removeEventListener('play', onPlay); videoEl.removeEventListener('pause', onPause); } catch { } }); } }; // Try to find the primary HTML5 video element on the YouTube watch page const findVideoElement = () => { // Try multiple selectors for better compatibility const selectors = [ 'video.html5-main-video', 'video.video-stream', '#movie_player video', 'video', ]; for (const selector of selectors) { const video = document.querySelector(selector); if (video && video.tagName === 'VIDEO') { return /** @type {HTMLVideoElement} */ (video); } } return null; }; const initResume = () => { // Only run on watch pages if (window.location.pathname !== '/watch') return; const videoEl = findVideoElement(); if (videoEl) { attachResumeHandlers(videoEl); } else { // Retry after a short delay if video not found yet setTimeout(initResume, 500); } }; // Listen for navigation events used by YouTube SPA const onNavigate = () => setTimeout(initResume, 150); if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initResume, { once: true }); } else { initResume(); } // YouTube internal navigation event if (window && window.document) { // Prefer custom event registered in other modules if (window.YouTubeUtils && YouTubeUtils.cleanupManager) { YouTubeUtils.cleanupManager.registerListener(document, 'yt-navigate-finish', onNavigate, { passive: true, }); } else { document.addEventListener('yt-navigate-finish', onNavigate, { passive: true }); } } })(); // Play All (async function () { 'use strict'; // Safe access to Greasemonkey/Tampermonkey globals via window lookup to avoid // static analysis errors when those globals are not present. const GM_info_safe = typeof window !== 'undefined' && window['GM_info'] ? window['GM_info'] : null; const GM_safe = typeof window !== 'undefined' && window['GM'] ? window['GM'] : null; // Localization for YouTube Play All const i18n = { en: { loadingPlaylist: 'Loading playlist...', playPopular: 'Play Popular', playAll: 'Play All', playRandom: 'Play Random', preferNewest: 'Prefer newest', preferOldest: 'Prefer oldest', randomMode: 'Random', externalApiWarning: "Make sure to allow the external API call to ytplaylist.robert.wesner.io to keep viewing playlists that YouTube doesn't natively support!", }, ru: { loadingPlaylist: 'Загрузка плейлиста...', playPopular: 'Популярные', playAll: 'Воспроизвести все', playRandom: 'Случайно', preferNewest: 'Сначала новые', preferOldest: 'Сначала старые', randomMode: 'Случайный', externalApiWarning: 'Разрешите внешний API вызов на ytplaylist.robert.wesner.io для просмотра плейлистов, которые YouTube не поддерживает изначально!', }, }; const getLanguage = () => { const htmlLang = document.documentElement.lang || 'en'; if (htmlLang.startsWith('ru')) return 'ru'; return 'en'; }; const lang = getLanguage(); const t = key => i18n[lang][key] || i18n.en[key] || key; const scriptVersion = GM_info_safe?.script?.version || null; if (scriptVersion && /-(alpha|beta|dev|test)$/.test(scriptVersion)) { console.log( '%cYTPA - YouTube Play All\n', 'color: #bf4bcc; font-size: 32px; font-weight: bold', 'You are currently running a test version:', scriptVersion, ); } if (window.hasOwnProperty('trustedTypes') && !window.trustedTypes.defaultPolicy) { window.trustedTypes.createPolicy('default', { createHTML: string => string }); } document.head.insertAdjacentHTML('beforeend', ``); const getVideoId = url => new URLSearchParams(new URL(url).search).get('v'); /** * @return {{ getProgressState: () => { current: number, duration, number }, pauseVideo: () => void, seekTo: (number) => void, isLifaAdPlaying: () => boolean }} player */ /** * Safe player accessor. * Returns either the native player element (if it exposes the expected API) * or a lightweight wrapper implementing the small API surface used by this * module. This prevents runtime/TS complaints when YouTube's player does not * expose the expected methods. */ const getPlayer = () => { const playerEl = document.querySelector('#movie_player'); if (!playerEl) return null; // If the player element already implements the methods we need, return it if ( typeof playerEl.getProgressState === 'function' && typeof playerEl.pauseVideo === 'function' && typeof playerEl.seekTo === 'function' && typeof playerEl.isLifaAdPlaying === 'function' ) { return playerEl; } // Otherwise return a safe wrapper that proxies to the HTML5