// ==UserScript== // @name 视频触屏滑动脚本 // @namespace http://tampermonkey.net/ // @version 1.0.5 // @description 全屏下左右滑动可以拖动视频进度条 // @author sanngtsu // @match *://*/* // @grant none // @run-at document-end // ==/UserScript== (function () { 'use strict'; const BASE_SENSITIVITY = 0.15; const MAX_SENSITIVITY = 1.2; const MIN_SWIPE_DISTANCE = 1; let isTouching = false; let startX = 0, startY = 0; let startVideoTime = 0; let currentVideo = null; let hasMoved = false; let tipElement = null; let progressBarOuter = null; let progressBarInner = null; function formatTime(seconds) { if (isNaN(seconds)) return "00:00"; const m = Math.floor(seconds / 60).toString().padStart(2, '0'); const s = Math.floor(seconds % 60).toString().padStart(2, '0'); return `${m}:${s}`; } function createTip() { if (tipElement) return; tipElement = document.createElement('div'); tipElement.style.cssText = ` position: absolute; z-index: 99999999; background: rgba(0,0,0,0.7); color: #fff; padding: 14px 24px; border-radius: 10px; font-size: 24px; font-weight: bold; display: none; pointer-events: none; white-space: nowrap; text-align: center; min-width: 180px; `; progressBarOuter = document.createElement('div'); progressBarOuter.style.cssText = ` width: 100%; height: 6px; background: rgba(255,255,255,0.3); border-radius: 3px; margin-top: 10px; overflow: hidden; `; progressBarInner = document.createElement('div'); progressBarInner.style.cssText = ` height: 100%; width: 0%; background: #1E90FF; border-radius: 3px; transition: width 0.08s linear; `; progressBarOuter.appendChild(progressBarInner); tipElement.appendChild(progressBarOuter); document.body.appendChild(tipElement); } function isFullscreen() { return !!document.fullscreenElement; } function showTip(video, current, total) { if (!tipElement) createTip(); const container = document.fullscreenElement || document.documentElement; if (tipElement.parentElement !== container) container.appendChild(tipElement); tipElement.textContent = `${formatTime(current)} / ${formatTime(total)}`; tipElement.appendChild(progressBarOuter); const percent = (current / total) * 100; progressBarInner.style.width = percent + "%"; tipElement.style.left = '50%'; tipElement.style.top = '50%'; tipElement.style.transform = 'translate(-50%, -50%)'; tipElement.style.display = 'block'; } function hideTip() { if (tipElement) tipElement.style.display = 'none'; } function getActiveVideo() { const videos = document.querySelectorAll('video'); for (let v of videos) if (!v.paused && !v.ended) return v; return videos[0] || null; } document.addEventListener('touchstart', e => { if (!isFullscreen()) return; if (e.touches.length !== 1) return; currentVideo = getActiveVideo(); if (!currentVideo) return; isTouching = true; hasMoved = false; startX = e.touches[0].clientX; startY = e.touches[0].clientY; startVideoTime = currentVideo.currentTime; }, { passive: true }); document.addEventListener('touchmove', e => { if (!isFullscreen() || !isTouching || !currentVideo) return; if (e.touches.length !== 1) return; const nowX = e.touches[0].clientX; const nowY = e.touches[0].clientY; const diffX = Math.abs(nowX - startX); const diffY = Math.abs(nowY - startY); if (diffX < MIN_SWIPE_DISTANCE && diffY > MIN_SWIPE_DISTANCE) return; if (diffX > 1) hasMoved = true; const distance = Math.abs(nowX - startX); const factor = Math.min(distance / 200, 1); const sensitivity = BASE_SENSITIVITY + (factor * factor) * (MAX_SENSITIVITY - BASE_SENSITIVITY); const moveX = nowX - startX; const timeChange = moveX * sensitivity; let newTime = startVideoTime + timeChange; newTime = Math.max(0, Math.min(newTime, currentVideo.duration)); currentVideo.currentTime = newTime; showTip(currentVideo, newTime, currentVideo.duration); e.preventDefault(); }, { passive: false }); document.addEventListener('touchend', e => { if (!isFullscreen() || !isTouching) return; if (hasMoved) { e.preventDefault(); e.stopPropagation(); } setTimeout(() => hideTip(), 150); isTouching = false; currentVideo = null; hasMoved = false; }); document.addEventListener('touchcancel', () => { if (!isFullscreen()) return; hideTip(); isTouching = false; currentVideo = null; hasMoved = false; }); })();