ozhegovvv

video speed control 1/1.5/2.0

Sep 2nd, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript: (function() {
  2.     var speeds = [1, 1.5, 2];
  3.     var currentSpeedIndex = parseInt(localStorage.getItem('videoSpeedIndex') || '0');
  4.  
  5.     function showFixedSpeedIndicator(message, duration) {
  6.         var speedIndicator = document.querySelector('.fixed-speed-indicator');
  7.         if (!speedIndicator) {
  8.             speedIndicator = document.createElement('div');
  9.             speedIndicator.className = 'fixed-speed-indicator';
  10.             speedIndicator.style.cssText = 'position:fixed;top:0px;left:50%;transform:translateX(-50%);background-color:rgba(0,0,0,0.7);color:white;padding:0 1px;border-radius:0 0 3px 3px;font-size:14px;font-family:Arial,sans-serif;z-index:999999;transition:opacity 0.3s;opacity:0;display:none;';
  11.             document.body.appendChild(speedIndicator);
  12.         }
  13.         speedIndicator.textContent = message;
  14.         speedIndicator.style.opacity = '1';
  15.         speedIndicator.style.display = 'block';
  16.         setTimeout(function() {
  17.             speedIndicator.style.opacity = '0';
  18.             setTimeout(() => {
  19.                 speedIndicator.style.display = 'none';
  20.             }, 300);
  21.         }, duration);
  22.     }
  23.  
  24.     function applyToVideo(v, showIndicator = false) {
  25.         if (v.playbackRate !== undefined) {
  26.             v.playbackRate = speeds[currentSpeedIndex];
  27.             if (showIndicator) {
  28.                 if (!window.location.href.includes('rutube.ru/shorts/') && !window.location.href.includes('dzen.ru') && !window.location.href.includes('my.mail.ru') && !window.location.href.includes('pikabu.ru') && !window.location.href.includes('store.steampowered.com') && !window.location.href.includes('wink.ru')) {
  29.                     var container = v.closest('.jwplayer') || v.closest('.video_box_wrap') || v.closest('.player') || v.closest('.vjs_video') || v.closest('.video-js') || v.parentElement;
  30.                     container.style.position = 'relative';
  31.                     var speedIndicator = container.querySelector('.speed-indicator');
  32.                     if (!speedIndicator) {
  33.                         speedIndicator = document.createElement('div');
  34.                         speedIndicator.className = 'speed-indicator';
  35.                         speedIndicator.style.cssText = 'position:absolute;top:0px;left:50%;transform:translateX(-50%);background-color:rgba(0,0,0,0.7);color:white;padding:0 1px;border-radius:0 0 3px 3px;font-size:14px;font-family:Arial,sans-serif;z-index:999999;transition:opacity 0.3s;opacity:0;display:none;';
  36.                         container.appendChild(speedIndicator);
  37.                     }
  38.                     speedIndicator.textContent = v.playbackRate.toFixed(1) + 'x';
  39.                     speedIndicator.style.opacity = '1';
  40.                     speedIndicator.style.display = 'block';
  41.                     setTimeout(() => {
  42.                         speedIndicator.style.opacity = '0';
  43.                         setTimeout(() => {
  44.                             speedIndicator.style.display = 'none';
  45.                         }, 300);
  46.                     }, 1000);
  47.                 } else {
  48.                     showFixedSpeedIndicator(v.playbackRate.toFixed(1) + 'x', 1000);
  49.                 }
  50.             }
  51.         }
  52.     }
  53. function changeSpeed() {
  54.     var videos = document.querySelectorAll('video');
  55.     if (videos.length > 0) {
  56.         var currentSpeed = videos[0].playbackRate;
  57.         var currentIndex = speeds.indexOf(currentSpeed);
  58.         if (currentIndex === -1) {
  59.             currentIndex = speeds.findIndex(speed => speed > currentSpeed) - 1;
  60.             if (currentIndex === -2) currentIndex = speeds.length - 1;
  61.         }
  62.         currentSpeedIndex = (currentIndex + 1) % speeds.length;
  63.         localStorage.setItem('videoSpeedIndex', currentSpeedIndex);
  64.         videos.forEach(v => applyToVideo(v, true));
  65.     } else {
  66.         showFixedSpeedIndicator('No video found on this page', 2000);
  67.     }
  68. }
  69.  
  70. function handleNewVideos(mutations) {
  71.     mutations.forEach(function(mutation) {
  72.         if (mutation.addedNodes) {
  73.             mutation.addedNodes.forEach(function(node) {
  74.                 if (node.nodeName === 'VIDEO') {
  75.                     applyToVideo(node, false);
  76.                 } else if (node.classList && (node.classList.contains('jwplayer') || node.classList.contains('player') || node.classList.contains('vjs_video') || node.classList.contains('video-js'))) {
  77.                     var video = node.querySelector('video');
  78.                     if (video) {
  79.                         applyToVideo(video, false);
  80.                     }
  81.                 } else if (node.querySelector) {
  82.                     var videos = node.querySelectorAll('video');
  83.                     videos.forEach(v => applyToVideo(v, false));
  84.                 }
  85.             });
  86.         }
  87.     });
  88. }
  89.  
  90. function checkAndApplySpeed() {
  91.     document.querySelectorAll('video').forEach(v => {
  92.         if (v.playbackRate !== speeds[currentSpeedIndex]) {
  93.             applyToVideo(v, false);
  94.         }
  95.     });
  96. }
  97.  
  98. document.addEventListener('seeked', function(e) {
  99.     if (e.target.tagName === 'VIDEO') {
  100.         setTimeout(() => applyToVideo(e.target, false), 0);
  101.     }
  102. }, true);
  103.  
  104. ['loadedmetadata', 'canplay', 'playing'].forEach(function(event) {
  105.     document.addEventListener(event, function(e) {
  106.         if (e.target.tagName === 'VIDEO') {
  107.             applyToVideo(e.target, false);
  108.         }
  109.     }, true);
  110. });
  111.  
  112. var observer = new MutationObserver(handleNewVideos);
  113. observer.observe(document.body, {
  114.     childList: true,
  115.     subtree: true
  116. });
  117.  
  118. var videos = document.querySelectorAll('video');
  119. if (videos.length > 0) {
  120.     var currentSpeed = videos[0].playbackRate;
  121.     var currentIndex = speeds.indexOf(currentSpeed);
  122.     if (currentIndex === -1) {
  123.         currentIndex = speeds.findIndex(speed => speed > currentSpeed) - 1;
  124.         if (currentIndex === -2) currentIndex = speeds.length - 1;
  125.     }
  126.     currentSpeedIndex = currentIndex;
  127.     localStorage.setItem('videoSpeedIndex', currentSpeedIndex);
  128. }
  129.  
  130. videos.forEach(v => applyToVideo(v, false));
  131.  
  132. document.addEventListener('play', function(e) {
  133.     if (e.target.tagName === 'VIDEO') {
  134.         applyToVideo(e.target, false);
  135.     }
  136. }, true);
  137.  
  138. window.addEventListener('popstate', checkAndApplySpeed);
  139.  
  140. changeSpeed();
  141. })();
Tags: bookmark
Advertisement
Add Comment
Please, Sign In to add comment