ozhegovvv

Auto Video Speed Adjuster

Aug 23rd, 2024 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Auto Video Speed Adjuster
  3. // @namespace    http://tampermonkey.net/
  4. // @version      2024-08-19
  5. // @description  Automatically set playback speed to 2.0x on specified sites and adjust based on video duration or description/title containing certain keywords on other sites.
  6. // @author       0001
  7. // @match        *://*/*
  8. // @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @grant        none
  10. // @run-at       document-end
  11. // ==/UserScript==
  12.  
  13. (function() {
  14.     'use strict';
  15.  
  16.     const keywords = ["обзор", "gameplay", "трейлер", "trailer", "review", "teaser"];
  17.     const defaultSpeed = 2.0; // Основная скорость по умолчанию
  18.     const shortVideoSpeed = 1.5; // Скорость для коротких видео
  19.  
  20.     // Array of hostnames where the playback speed should be set to 2.0x
  21.     const fastSpeedSites = [
  22.         'dtf.ru',
  23.         'pikabu.ru',
  24.         'premier.one',
  25.         'ivi.ru',
  26.         'start.ru',
  27.     ];
  28.  
  29.     function checkDescriptionOrTitle() {
  30.         const descriptionElement = document.querySelector('meta[name="description"]') ||
  31.                                    document.querySelector('#description') ||
  32.                                    document.querySelector('.content .description');
  33.         const titleElement = document.querySelector('title') ||
  34.                              document.querySelector('meta[property="og:title"]') ||
  35.                              document.querySelector('.title');
  36.  
  37.         const descriptionText = descriptionElement?.content || descriptionElement?.innerText || "";
  38.         const titleText = titleElement?.content || titleElement?.innerText || "";
  39.  
  40.         const combinedText = (descriptionText + " " + titleText).toLowerCase();
  41.  
  42.         return keywords.some(keyword => combinedText.includes(keyword));
  43.     }
  44.  
  45.     function determineSpeed(v) {
  46.         // Check if the current site is in the list of fast speed sites
  47.         if (fastSpeedSites.some(site => window.location.hostname.includes(site))) {
  48.             return defaultSpeed;
  49.         } else {
  50.             const isSpecialContent = checkDescriptionOrTitle();
  51.             if (isSpecialContent) {
  52.                 return defaultSpeed;
  53.             } else {
  54.                 const videoDuration = v.duration;
  55.                 return videoDuration <= 300 ? shortVideoSpeed : defaultSpeed; // Если продолжительность видео меньше или равна 300 секунд (5 минут)
  56.             }
  57.         }
  58.     }
  59.  
  60.     function applyToVideo(v) {
  61.         if (v.playbackRate !== undefined) {
  62.             // Устанавливаем скорость при загрузке метаданных
  63.             v.playbackRate = determineSpeed(v);
  64.  
  65.             // Также проверяем и устанавливаем скорость при начале воспроизведения
  66.             v.addEventListener('play', function() {
  67.                 v.playbackRate = determineSpeed(v);
  68.             });
  69.  
  70.             // Если видео уже воспроизводится, мгновенно применяем скорость
  71.             if (!v.paused) {
  72.                 v.playbackRate = determineSpeed(v);
  73.             }
  74.         }
  75.     }
  76.  
  77.     function handleNewVideos(mutations) {
  78.         mutations.forEach(function(mutation) {
  79.             if (mutation.addedNodes) {
  80.                 mutation.addedNodes.forEach(function(node) {
  81.                     if (node.nodeName === 'VIDEO') {
  82.                         node.addEventListener('loadedmetadata', function() {
  83.                             setTimeout(() => applyToVideo(node), 500);
  84.                         });
  85.                     } else if (node.querySelectorAll) {
  86.                         const videos = node.querySelectorAll('video');
  87.                         videos.forEach(v => {
  88.                             v.addEventListener('loadedmetadata', function() {
  89.                                 setTimeout(() => applyToVideo(v), 500);
  90.                             });
  91.                         });
  92.                     }
  93.                 });
  94.             }
  95.         });
  96.     }
  97.  
  98.     function checkAndApplySpeed() {
  99.         document.querySelectorAll('video').forEach(v => {
  100.             setTimeout(() => applyToVideo(v), 500);
  101.         });
  102.     }
  103.  
  104.     document.addEventListener('visibilitychange', function() {
  105.         if (document.visibilityState === 'visible') {
  106.             checkAndApplySpeed();
  107.         }
  108.     });
  109.  
  110.     window.addEventListener('popstate', checkAndApplySpeed);
  111.  
  112.     const observer = new MutationObserver(handleNewVideos);
  113.     observer.observe(document.body, {
  114.         childList: true,
  115.         subtree: true
  116.     });
  117.  
  118.     checkAndApplySpeed();
  119. })();
Advertisement
Add Comment
Please, Sign In to add comment