-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhance-media-ux.js
More file actions
247 lines (214 loc) · 6.67 KB
/
enhance-media-ux.js
File metadata and controls
247 lines (214 loc) · 6.67 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// ==UserScript==
// @name Enhance Media UX
// @namespace https://github.com/codeaditya/browser-userscripts
// @downloadURL https://cdn.jsdelivr.net/gh/codeaditya/browser-userscripts@main/enhance-media-ux.js
// @updateURL https://cdn.jsdelivr.net/gh/codeaditya/browser-userscripts@main/enhance-media-ux.js
// @version 2025.12.24
// @description Modify media playback speed and other controls
// @author Aditya <code.aditya at gmail>
// @match *://*/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addValueChangeListener
// @run-at document-idle
// ==/UserScript==
(function () {
"use strict";
// --- Configuration and Constants ---
const SETTINGS = {
GM_SPEED_KEY: "custom-playback-rate",
SPEED_STEP: 0.1,
MIN_SPEED: 0.1,
MAX_SPEED: 6.0,
HINT_DISPLAY_DURATION_MS: 1000,
};
const SHORTCUTS = {
INCREASE_SPEED: "+",
DECREASE_SPEED: "-",
RESET_SPEED: "*",
};
// --- State Management ---
let CURRENT_PLAYBACK_RATE =
parseFloat(GM_getValue(SETTINGS.GM_SPEED_KEY, 1.0)) || 1.0;
// --- DOM Elements & CSS ---
const HINT_ID = "emu-hint";
const HINT_STYLE = `
#${HINT_ID} {
position: fixed;
bottom: 20px;
left: 20px;
background-color: rgba(0, 0, 0, 0.85);
color: white;
padding: 10px 16px;
border-radius: 6px;
font-size: 16px;
font-weight: bold;
font-family: sans-serif;
z-index: 99999;
opacity: 0;
transition: opacity 0.3s ease-in-out;
pointer-events: none; /* Allows clicks to pass through */
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
border: 1px solid rgba(255, 255, 255, 0.2);
}
`;
const MEDIA_SELECTOR = "video, audio";
// --- Helper Functions ---
/**
* Simple debounce to avoid excessive MutationObserver callbacks
*/
function debounce(func, delay) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), delay);
};
}
/**
* Applies the current playback rate to all media elements.
*/
function applyMediaSpeed() {
document.querySelectorAll(MEDIA_SELECTOR).forEach((media) => {
if (media.playbackRate !== CURRENT_PLAYBACK_RATE) {
media.playbackRate = CURRENT_PLAYBACK_RATE;
console.debug(`Set media playback speed to ${CURRENT_PLAYBACK_RATE}x`);
}
});
}
/**
* Creates and displays a subtle hint message on the screen.
* @param {string} message - The message to display.
*/
function showSubtleHint(message) {
let hintDiv = document.getElementById(HINT_ID);
if (!hintDiv) {
const styleTag = document.createElement("style");
styleTag.textContent = HINT_STYLE;
document.head.appendChild(styleTag);
hintDiv = document.createElement("div");
hintDiv.id = HINT_ID;
document.body.appendChild(hintDiv);
}
hintDiv.textContent = message;
hintDiv.style.opacity = "1";
setTimeout(() => {
hintDiv.style.opacity = "0";
}, SETTINGS.HINT_DISPLAY_DURATION_MS);
}
/**
* Updates the global playback rate, saves it, and applies it to media.
* @param {number} newRate - The new playback rate to set.
*/
function updateAndApplyPlaybackRate(newRate) {
const oldRate = CURRENT_PLAYBACK_RATE;
// Ensure the rate is within bounds and rounded to one decimal place
CURRENT_PLAYBACK_RATE = Math.min(
SETTINGS.MAX_SPEED,
Math.max(
SETTINGS.MIN_SPEED,
parseFloat(newRate.toFixed(SETTINGS.SPEED_STEP.toString().length - 2)) // Dynamically round based on step precision
)
);
if (CURRENT_PLAYBACK_RATE === oldRate) {
return; // No change, no need to update DOM or storage
}
console.log(
`Playback rate changed from ${oldRate.toFixed(
1
)}x to ${CURRENT_PLAYBACK_RATE.toFixed(1)}x`
);
showSubtleHint(`${CURRENT_PLAYBACK_RATE.toFixed(1)}x`);
GM_setValue(SETTINGS.GM_SPEED_KEY, CURRENT_PLAYBACK_RATE);
applyMediaSpeed();
}
/**
* Checks if the event target is an editable element.
* @param {KeyboardEvent} event
* @returns {boolean}
*/
function isEditingElement(event) {
// Get the actual target, even if it's inside a shadow DOM
const target = event.composedPath()[0];
return (
target.isContentEditable ||
["TEXTAREA", "SELECT", "INPUT"].includes(target.tagName) ||
target.getAttribute("role") === "textbox"
);
}
// --- Event Handlers ---
/**
* Handles keyboard shortcuts for playback rate control.
* @param {KeyboardEvent} event
*/
function handleKeyDown(event) {
// Avoid interfering with common modifier keys
if (event.ctrlKey || event.altKey || event.metaKey) {
return;
}
// Don't interfere if typing
if (isEditingElement(event)) {
return;
}
switch (event.key) {
case SHORTCUTS.INCREASE_SPEED:
updateAndApplyPlaybackRate(CURRENT_PLAYBACK_RATE + SETTINGS.SPEED_STEP);
break;
case SHORTCUTS.DECREASE_SPEED:
updateAndApplyPlaybackRate(CURRENT_PLAYBACK_RATE - SETTINGS.SPEED_STEP);
break;
case SHORTCUTS.RESET_SPEED:
updateAndApplyPlaybackRate(1.0); // Reset to default
break;
}
}
/**
* Syncs playback rate changes across windows/iframes/tabs
*/
function setupStorageSync() {
GM_addValueChangeListener(
SETTINGS.GM_SPEED_KEY,
(name, oldValue, newValue, remote) => {
if (remote && newValue !== CURRENT_PLAYBACK_RATE) {
CURRENT_PLAYBACK_RATE = parseFloat(newValue) || 1.0;
applyMediaSpeed();
showSubtleHint(`${CURRENT_PLAYBACK_RATE.toFixed(1)}x`);
}
}
);
}
// --- Initialization ---
/**
* Initializes the userscript
*/
function initialize() {
applyMediaSpeed();
setupStorageSync();
// Observe DOM changes to catch dynamically loaded media elements
const debouncedApply = debounce(applyMediaSpeed, 150);
const observer = new MutationObserver(debouncedApply);
observer.observe(document.body, {
childList: true,
subtree: true,
});
// Apply speed on play event in iframes
if (window !== window.top) {
document.addEventListener(
"play",
(event) => {
if (event.target.matches(MEDIA_SELECTOR)) {
event.target.playbackRate = CURRENT_PLAYBACK_RATE;
}
},
true
);
}
document.addEventListener("keydown", handleKeyDown);
// Clean up observer on page unload
window.addEventListener("unload", () => {
observer.disconnect();
});
console.info("Enhance Media UX initialized.");
}
// Run initialization after the DOM is ready
initialize();
})();