-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniversal-video-sharpness-filter.user.js
More file actions
210 lines (179 loc) · 6.58 KB
/
universal-video-sharpness-filter.user.js
File metadata and controls
210 lines (179 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// ==UserScript==
// @name Universal Video Sharpener
// @namespace http://tampermonkey.net/
// @version 1.3
// @description Applies video sharpening filter to streaming videos across websites with smooth transitions
// @match *://*/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @grant GM_notification
// @license MIT
// ==/UserScript==
(function() {
'use strict';
// Configuration
const CONFIG = {
sharpnessValue: 0.0003,
contrastBoost: 0.997,
brightnessBoost: 1.02,
transitionDuration: '0.5s',
debugMode: false
};
// Logging function
function log(message) {
if (CONFIG.debugMode) {
console.log(`[Video Sharpener] ${message}`);
}
}
// Create SVG filter more efficiently
function createSVGFilter() {
if (document.getElementById('universal-sharpness-filter')) return;
const svgNamespace = 'http://www.w3.org/2000/svg';
const svg = document.createElementNS(svgNamespace, 'svg');
svg.setAttribute('width', '0');
svg.setAttribute('height', '0');
svg.setAttribute('id', 'universal-sharpness-filter');
const filter = document.createElementNS(svgNamespace, 'filter');
filter.setAttribute('id', 'sharpness-filter');
const feConvolve = document.createElementNS(svgNamespace, 'feConvolveMatrix');
feConvolve.setAttribute('order', '3');
feConvolve.setAttribute('preserveAlpha', 'true');
feConvolve.setAttribute('kernelMatrix', '0 -1 0 -1 5 -1 0 -1 0');
filter.appendChild(feConvolve);
svg.appendChild(filter);
document.body.appendChild(svg);
log('SVG Filter created');
}
// Setup transition style
function setupTransitionStyle() {
if (document.getElementById('sharpener-transition-style')) return;
const style = document.createElement('style');
style.id = 'sharpener-transition-style';
style.textContent = `
.video-sharpener-transition {
transition: filter ${CONFIG.transitionDuration} ease-in-out !important;
}
`;
document.head.appendChild(style);
}
// Detect if an element is likely a video
function isVideoElement(element) {
return element instanceof HTMLVideoElement &&
element.videoWidth > 0 &&
element.videoHeight > 0;
}
// Apply sharpness filter with transition
function applySharpnessFilter(video, isEnabled) {
if (!video) return;
try {
// Add transition class if not already present
if (!video.classList.contains('video-sharpener-transition')) {
video.classList.add('video-sharpener-transition');
}
if (isEnabled) {
const { contrastBoost, brightnessBoost } = CONFIG;
// Start with no filter and then apply gradually
requestAnimationFrame(() => {
video.style.filter = `
url(#sharpness-filter)
contrast(${contrastBoost})
brightness(${brightnessBoost})
`;
});
video.dataset.sharpened = 'true';
log('Sharpness filter applied');
} else {
video.style.filter = 'none';
delete video.dataset.sharpened;
log('Sharpness filter removed');
}
} catch (error) {
console.error('Error applying sharpness filter:', error);
}
}
// Update all videos on the page
function updateAllVideos(isEnabled) {
const videos = document.querySelectorAll('video');
videos.forEach(video => {
if (isVideoElement(video)) {
applySharpnessFilter(video, isEnabled);
}
});
}
// Main processing function
function processVideos() {
const isScriptEnabled = GM_getValue('universalSharpenerEnabled', false);
const videos = document.querySelectorAll('video:not([data-sharpened])');
videos.forEach(video => {
if (isVideoElement(video)) {
applySharpnessFilter(video, isScriptEnabled);
}
});
}
// Toggle function with notification
function toggleSharpener() {
const currentState = GM_getValue('universalSharpenerEnabled', false);
const newState = !currentState;
GM_setValue('universalSharpenerEnabled', newState);
if (newState) {
createSVGFilter();
}
// Update all existing videos
updateAllVideos(newState);
// Show notification
GM_notification({
text: `Video Sharpener: ${newState ? 'Enabled' : 'Disabled'}`,
timeout: 2000,
title: 'Video Sharpener'
});
}
// Initialize script
function initScript() {
// Get initial state
const isEnabled = GM_getValue('universalSharpenerEnabled', false);
// Create SVG filter if enabled
if (isEnabled) {
createSVGFilter();
}
// Setup transition styles
setupTransitionStyle();
// Register menu command with state indicator
GM_registerMenuCommand(
`${isEnabled ? '✓' : '✗'} Toggle Video Sharpener`,
toggleSharpener
);
// Use IntersectionObserver for efficient video tracking
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting && isVideoElement(entry.target)) {
processVideos();
}
});
}, { threshold: 0.5 });
// Start observing new videos
const videoObserver = new MutationObserver((mutations) => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.nodeName === 'VIDEO') {
observer.observe(node);
}
});
});
});
// Observe the entire document for new videos
videoObserver.observe(document.body, {
childList: true,
subtree: true
});
// Observe existing videos
document.querySelectorAll('video').forEach(video => {
observer.observe(video);
});
// Initial processing and periodic check
processVideos();
setInterval(processVideos, 2000);
}
// Start script
initScript();
})();