|
| 1 | +// changeVideoPlaybackSpeed.js |
| 2 | +// https://github.com/0xdevalias/userscripts/tree/main/devtools-snippets |
| 3 | +// Changes the video playback speed of all video tags identified |
| 4 | + |
| 5 | +// Create a toast notification |
| 6 | +function showToast(message) { |
| 7 | + console.log("[changeVideoPlaybackSpeed]", message); |
| 8 | + |
| 9 | + // Remove existing toasts |
| 10 | + const existingToasts = document.querySelectorAll('.custom-toast'); |
| 11 | + existingToasts.forEach(toast => { |
| 12 | + toast.remove(); |
| 13 | + }); |
| 14 | + |
| 15 | + const toast = document.createElement("div"); |
| 16 | + toast.className = 'custom-toast'; |
| 17 | + toast.style.position = "fixed"; |
| 18 | + toast.style.top = "50%"; |
| 19 | + toast.style.left = "50%"; |
| 20 | + toast.style.transform = "translate(-50%, -50%)"; |
| 21 | + toast.style.padding = "10px"; |
| 22 | + toast.style.background = "#333"; |
| 23 | + toast.style.color = "#fff"; |
| 24 | + toast.style.opacity = 0; |
| 25 | + toast.style.transition = "opacity 0.5s"; |
| 26 | + toast.innerText = message; |
| 27 | + |
| 28 | + document.body.appendChild(toast); |
| 29 | + |
| 30 | + // Fade in |
| 31 | + setTimeout(() => { |
| 32 | + toast.style.opacity = 1; |
| 33 | + }, 50); |
| 34 | + |
| 35 | + // Fade out and remove |
| 36 | + setTimeout(() => { |
| 37 | + toast.style.opacity = 0; |
| 38 | + setTimeout(() => { |
| 39 | + toast.remove(); |
| 40 | + }, 500); |
| 41 | + }, 2000); |
| 42 | +} |
| 43 | + |
| 44 | +// Get all video elements |
| 45 | +const videos = Array.from(document.querySelectorAll("video")); |
| 46 | + |
| 47 | +// Check if any video tags are found |
| 48 | +if (videos.length === 0) { |
| 49 | + showToast("No video tags found."); |
| 50 | +} else { |
| 51 | + // Ask for initial speed |
| 52 | + const initialSpeed = parseFloat(prompt("Initial Playback Speed:", "1")); |
| 53 | + |
| 54 | + if (!isNaN(initialSpeed) && initialSpeed >= 0.25) { |
| 55 | + // Set initial speed for all videos |
| 56 | + videos.forEach(video => { |
| 57 | + video.playbackRate = initialSpeed; |
| 58 | + }); |
| 59 | + showToast(`Playback speed set to ${initialSpeed}x`); |
| 60 | + } else { |
| 61 | + // Set default speed for all videos |
| 62 | + videos.forEach(video => { |
| 63 | + video.playbackRate = 1; |
| 64 | + }); |
| 65 | + showToast("Invalid speed entered. Using default (1x)."); |
| 66 | + } |
| 67 | + |
| 68 | + // Listen for keypresses to adjust speed |
| 69 | + document.addEventListener("keydown", (e) => { |
| 70 | + if (e.key === ">" || e.key === "<" || (e.shiftKey && e.code === 'Space')) { |
| 71 | + e.preventDefault(); |
| 72 | + e.stopPropagation(); |
| 73 | + |
| 74 | + videos.forEach(video => { |
| 75 | + let newSpeed; |
| 76 | + |
| 77 | + if (e.key === ">") { |
| 78 | + newSpeed = Math.min(video.playbackRate + 0.25, 10); // Max speed of 10 |
| 79 | + } else if (e.key === "<") { |
| 80 | + newSpeed = Math.max(video.playbackRate - 0.25, 0.25); // Min speed of 0.25 |
| 81 | + } else if (e.shiftKey && e.code === 'Space') { |
| 82 | + newSpeed = 1; // Reset to default speed |
| 83 | + } |
| 84 | + |
| 85 | + video.playbackRate = newSpeed; |
| 86 | + showToast(`Playback speed set to ${newSpeed}x`); |
| 87 | + }); |
| 88 | + } |
| 89 | + }); |
| 90 | +} |
0 commit comments