-
Notifications
You must be signed in to change notification settings - Fork 0
/
android-double-tap-seek-video.user.js
313 lines (266 loc) · 11 KB
/
android-double-tap-seek-video.user.js
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// ==UserScript==
// @name Android Double Tap to Seek Video
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Adds double-tap seeking to Firefox android or any other browser efficiently
// @author Faisal Bhuiyan
// @match *://*/*
// @run-at document-start
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// ==/UserScript==
(function () {
'use strict';
let lastTap = 0;
const DOUBLE_TAP_DELAY = 300;
let overlayContainer = null;
let isInFullscreen = false;
let isPlaying = false;
const CONTROLS_SAFE_ZONE = 80; // Height in pixels for controls area
const SIDE_ZONE_WIDTH = 25; // Width percentage for side touch areas
let toastTimeout = null;
let lastTouchTime = 0;
let lastTouchX = 0;
let lastTouchY = 0;
// Get current domain
function getCurrentDomain() {
return window.location.hostname;
}
// Check if current site is blacklisted
function isBlacklisted() {
const blacklist = GM_getValue('blacklistedSites', []);
return blacklist.includes(getCurrentDomain());
}
// Toggle blacklist for current site
function toggleBlacklist() {
const domain = getCurrentDomain();
const blacklist = GM_getValue('blacklistedSites', []);
const isCurrentlyBlacklisted = blacklist.includes(domain);
if (isCurrentlyBlacklisted) {
const newBlacklist = blacklist.filter(site => site !== domain);
GM_setValue('blacklistedSites', newBlacklist);
showToast('Site removed from blacklist');
} else {
blacklist.push(domain);
GM_setValue('blacklistedSites', blacklist);
showToast('Site added to blacklist');
removeExistingOverlays();
}
}
// Register menu command
GM_registerMenuCommand('Toggle Blacklist for Current Site', toggleBlacklist);
function showToast(message) {
let toast = document.getElementById('video-seeker-toast');
if (!toast) {
toast = document.createElement('div');
toast.id = 'video-seeker-toast';
toast.style.cssText = `
position: fixed !important;
top: 50% !important;
left: 50% !important;
transform: translate(-50%, -50%) !important;
background: rgba(0, 0, 0, 0.7) !important;
color: white !important;
padding: 10px 20px !important;
border-radius: 5px !important;
z-index: 2147483647 !important;
pointer-events: none !important;
transition: opacity 0.3s !important;
font-family: Arial, sans-serif !important;
`;
document.body.appendChild(toast);
}
toast.textContent = message;
toast.style.opacity = '1';
if (toastTimeout) clearTimeout(toastTimeout);
toastTimeout = setTimeout(() => {
toast.style.opacity = '0';
}, 2000);
}
function removeExistingOverlays() {
const existingOverlays = document.querySelectorAll('#video-overlay-container');
existingOverlays.forEach(overlay => overlay.remove());
overlayContainer = null;
}
function createOverlayContainer() {
removeExistingOverlays();
const container = document.createElement('div');
container.id = 'video-overlay-container';
container.style.cssText = `
position: absolute !important;
top: 0 !important;
left: 0 !important;
right: 0 !important;
bottom: ${CONTROLS_SAFE_ZONE}px !important;
width: 100% !important;
height: calc(100% - ${CONTROLS_SAFE_ZONE}px) !important;
pointer-events: auto !important;
z-index: 2147483647 !important;
background: transparent !important;
touch-action: manipulation !important;
display: ${isPlaying ? 'block' : 'none'} !important;
`;
const touchAreas = ['left', 'right'].map((position) => {
const area = document.createElement('div');
area.id = `touch-area-${position}`;
area.style.cssText = `
position: absolute !important;
${position}: 0 !important;
top: 0 !important;
width: ${SIDE_ZONE_WIDTH}% !important;
height: 100% !important;
pointer-events: auto !important;
background: rgba(255, 255, 255, 0.01) !important;
z-index: 2147483647 !important;
touch-action: manipulation !important;
`;
function handleTouch(e) {
try {
if (!isPlaying || !isInFullscreen || isBlacklisted()) return;
const currentTime = new Date().getTime();
const touch = e.touches && e.touches[0];
if (!touch && e.type === 'touchstart') return;
if (e.type === 'touchstart') {
const tapLength = currentTime - lastTap;
const touchX = touch.clientX;
const touchY = touch.clientY;
// Check if this is a double tap (time and position)
const isDoubleTap = tapLength < DOUBLE_TAP_DELAY &&
Math.abs(touchX - lastTouchX) < 30 &&
Math.abs(touchY - lastTouchY) < 30;
if (isDoubleTap) {
const video = document.querySelector('video');
if (video) {
const seekAmount = position === 'right' ? 10 : -10;
if (video.player && typeof video.player.currentTime === 'function') {
video.player.currentTime(video.player.currentTime() + seekAmount);
} else {
video.currentTime += seekAmount;
}
showToast(`${seekAmount > 0 ? '+' : ''}${seekAmount} seconds`);
}
e.preventDefault();
e.stopPropagation();
}
// Update last touch info
lastTap = currentTime;
lastTouchX = touchX;
lastTouchY = touchY;
}
} catch (err) {
console.log('Touch handler error:', err);
}
}
['touchstart', 'touchend'].forEach(eventType => {
area.addEventListener(eventType, handleTouch, {
passive: false,
capture: true
});
});
return area;
});
touchAreas.forEach(area => container.appendChild(area));
return container;
}
function updateOverlayVisibility() {
if (overlayContainer) {
overlayContainer.style.display = (isInFullscreen && isPlaying) ? 'block' : 'none';
}
}
function attachOverlay() {
const fullscreenElement =
document.fullscreenElement ||
document.webkitFullscreenElement ||
document.querySelector('.video-js.vjs-fullscreen') ||
document.querySelector('video');
if (fullscreenElement && isInFullscreen) {
overlayContainer = createOverlayContainer();
const container =
fullscreenElement.querySelector('.vjs-tech-container') ||
fullscreenElement.querySelector('.video-js') ||
fullscreenElement;
if (container) {
container.appendChild(overlayContainer);
updateOverlayVisibility();
}
}
}
function handleFullscreenChange() {
const isNowFullscreen = !!(
document.fullscreenElement ||
document.webkitFullscreenElement ||
document.querySelector('.video-js.vjs-fullscreen')
);
if (isNowFullscreen && !isInFullscreen) {
isInFullscreen = true;
setTimeout(attachOverlay, 100);
showToast('Double-tap controls enabled');
} else if (!isNowFullscreen && isInFullscreen) {
isInFullscreen = false;
removeExistingOverlays();
showToast('Double-tap controls disabled');
}
}
function handlePlayPause(video) {
isPlaying = !(video.paused || video.ended || video.seeking || video.readyState < 3);
updateOverlayVisibility();
}
// Cleanup function for removing events
function cleanup() {
removeExistingOverlays();
isInFullscreen = false;
isPlaying = false;
}
// Listen for fullscreen changes
document.addEventListener('fullscreenchange', handleFullscreenChange, true);
document.addEventListener('webkitfullscreenchange', handleFullscreenChange, true);
// Listen for page visibility changes
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
cleanup();
}
});
// Watch for video elements
const observer = new MutationObserver((mutations, obs) => {
const video = document.querySelector('video');
if (video) {
// Listen for all playback state changes
['play', 'pause', 'seeking', 'seeked', 'waiting', 'playing'].forEach(eventType => {
video.addEventListener(eventType, () => handlePlayPause(video));
});
video.addEventListener('webkitbeginfullscreen', () => {
isInFullscreen = true;
setTimeout(attachOverlay, 100);
});
video.addEventListener('webkitendfullscreen', () => {
cleanup();
});
// Monitor video.js fullscreen class changes
const videoJs = document.querySelector('.video-js');
if (videoJs) {
const classObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
const isNowFullscreen = videoJs.classList.contains('vjs-fullscreen');
if (isNowFullscreen !== isInFullscreen) {
handleFullscreenChange();
}
}
});
});
classObserver.observe(videoJs, {
attributes: true,
attributeFilter: ['class']
});
}
// Initial state check
handlePlayPause(video);
obs.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
})();