Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Auto Video Speed Adjuster
- // @namespace http://tampermonkey.net/
- // @version 2024-08-19
- // @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.
- // @author 0001
- // @match *://*/*
- // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
- // @grant none
- // @run-at document-end
- // ==/UserScript==
- (function() {
- 'use strict';
- const keywords = ["обзор", "gameplay", "трейлер", "trailer", "review", "teaser"];
- const defaultSpeed = 2.0; // Основная скорость по умолчанию
- const shortVideoSpeed = 1.5; // Скорость для коротких видео
- // Array of hostnames where the playback speed should be set to 2.0x
- const fastSpeedSites = [
- 'dtf.ru',
- 'pikabu.ru',
- 'premier.one',
- 'ivi.ru',
- 'start.ru',
- ];
- function checkDescriptionOrTitle() {
- const descriptionElement = document.querySelector('meta[name="description"]') ||
- document.querySelector('#description') ||
- document.querySelector('.content .description');
- const titleElement = document.querySelector('title') ||
- document.querySelector('meta[property="og:title"]') ||
- document.querySelector('.title');
- const descriptionText = descriptionElement?.content || descriptionElement?.innerText || "";
- const titleText = titleElement?.content || titleElement?.innerText || "";
- const combinedText = (descriptionText + " " + titleText).toLowerCase();
- return keywords.some(keyword => combinedText.includes(keyword));
- }
- function determineSpeed(v) {
- // Check if the current site is in the list of fast speed sites
- if (fastSpeedSites.some(site => window.location.hostname.includes(site))) {
- return defaultSpeed;
- } else {
- const isSpecialContent = checkDescriptionOrTitle();
- if (isSpecialContent) {
- return defaultSpeed;
- } else {
- const videoDuration = v.duration;
- return videoDuration <= 300 ? shortVideoSpeed : defaultSpeed; // Если продолжительность видео меньше или равна 300 секунд (5 минут)
- }
- }
- }
- function applyToVideo(v) {
- if (v.playbackRate !== undefined) {
- // Устанавливаем скорость при загрузке метаданных
- v.playbackRate = determineSpeed(v);
- // Также проверяем и устанавливаем скорость при начале воспроизведения
- v.addEventListener('play', function() {
- v.playbackRate = determineSpeed(v);
- });
- // Если видео уже воспроизводится, мгновенно применяем скорость
- if (!v.paused) {
- v.playbackRate = determineSpeed(v);
- }
- }
- }
- function handleNewVideos(mutations) {
- mutations.forEach(function(mutation) {
- if (mutation.addedNodes) {
- mutation.addedNodes.forEach(function(node) {
- if (node.nodeName === 'VIDEO') {
- node.addEventListener('loadedmetadata', function() {
- setTimeout(() => applyToVideo(node), 500);
- });
- } else if (node.querySelectorAll) {
- const videos = node.querySelectorAll('video');
- videos.forEach(v => {
- v.addEventListener('loadedmetadata', function() {
- setTimeout(() => applyToVideo(v), 500);
- });
- });
- }
- });
- }
- });
- }
- function checkAndApplySpeed() {
- document.querySelectorAll('video').forEach(v => {
- setTimeout(() => applyToVideo(v), 500);
- });
- }
- document.addEventListener('visibilitychange', function() {
- if (document.visibilityState === 'visible') {
- checkAndApplySpeed();
- }
- });
- window.addEventListener('popstate', checkAndApplySpeed);
- const observer = new MutationObserver(handleNewVideos);
- observer.observe(document.body, {
- childList: true,
- subtree: true
- });
- checkAndApplySpeed();
- })();
Advertisement
Add Comment
Please, Sign In to add comment