Size
3.3 KB
Version
1.0.1
Created
Mar 6, 2026
Updated
about 1 month ago
1// ==UserScript==
2// @name YouTube - Optimize Performance
3// @description A new extension
4// @version 1.0.1
5// @match https://*.youtube.com/*
6// @icon https://www.youtube.com/s/desktop/1d8caaa1/img/favicon_32x32.png
7// ==/UserScript==
8(function() {
9 'use strict';
10
11 // Performance optimization utilities
12
13 // Debounce function to limit how often a function can run
14 function debounce(func, wait) {
15 let timeout;
16 return function executedFunction(...args) {
17 const later = () => {
18 clearTimeout(timeout);
19 func(...args);
20 };
21 clearTimeout(timeout);
22 timeout = setTimeout(later, wait);
23 };
24 }
25
26 // Throttle function to ensure a function runs at most once per specified time
27 function throttle(func, limit) {
28 let inThrottle;
29 return function(...args) {
30 if (!inThrottle) {
31 func.apply(this, args);
32 inThrottle = true;
33 setTimeout(() => inThrottle = false, limit);
34 }
35 };
36 }
37
38 // Efficient DOM observer with debouncing
39 function observeDOM(callback, options = {}) {
40 const debouncedCallback = debounce(callback, options.debounceTime || 300);
41
42 const observer = new MutationObserver((mutations) => {
43 debouncedCallback(mutations);
44 });
45
46 observer.observe(document.body, {
47 childList: true,
48 subtree: true,
49 ...options.observerOptions
50 });
51
52 return observer;
53 }
54
55 // Efficient element selector with caching
56 const elementCache = new Map();
57 function getCachedElement(selector, useCache = true) {
58 if (useCache && elementCache.has(selector)) {
59 const cached = elementCache.get(selector);
60 if (document.contains(cached)) {
61 return cached;
62 }
63 elementCache.delete(selector);
64 }
65
66 const element = document.querySelector(selector);
67 if (element && useCache) {
68 elementCache.set(selector, element);
69 }
70 return element;
71 }
72
73 // Clear cache when needed
74 function clearElementCache() {
75 elementCache.clear();
76 }
77
78 // Batch DOM operations for better performance
79 function batchDOMOperations(operations) {
80 requestAnimationFrame(() => {
81 const fragment = document.createDocumentFragment();
82 operations.forEach(op => op(fragment));
83 });
84 }
85
86 // Main initialization function
87 async function init() {
88 console.log('YouTube extension initialized with performance optimizations');
89
90 // Wait for body to be ready before executing
91 if (!document.body) {
92 await new Promise(resolve => {
93 if (document.readyState === 'loading') {
94 document.addEventListener('DOMContentLoaded', resolve);
95 } else {
96 resolve();
97 }
98 });
99 }
100
101 // Your extension functionality goes here
102 // Use the optimization utilities above for best performance
103
104 console.log('Extension ready');
105 }
106
107 // Start the extension
108 init().catch(error => {
109 console.error('Extension initialization failed:', error);
110 });
111
112})();