Don't Change YouTube Speed on Long Click

Stops YouTube from changing video speed to 2x when you hold down the mouse button.

目前為 2024-04-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name        Don't Change YouTube Speed on Long Click
// @namespace   Violentmonkey Scripts
// @description Stops YouTube from changing video speed to 2x when you hold down the mouse button.
// @match       https://*.youtube.com/*
// @include     https://www.youtube.com/*
// @include     https://m.youtube.com/*
// @grant       none
// @version     1.0
// @author      Jupiter Liar
// @license     CC BY
// @description 4/20/2024, 9:45:00 PM
// ==/UserScript==

var speedmasterVisibility = false;
var debug = true;

if (speedmasterVisibility == false) {
    // Create a stylesheet
    var style = document.createElement('style');
    style.id = "speedmaster-hide";
    style.type = 'text/css';
    style.innerHTML = '.ytp-speedmaster-overlay { display: none; }';

    // Append the stylesheet to the head
    document.head.appendChild(style);
}


// Function to log messages with a timestamp
function log(message) {
  if (debug) {
    console.log(`[${new Date().toLocaleTimeString()}] ${message}`);
  }
}

// Function to monitor playback speed changes
function monitorPlaybackSpeed() {
    // Store the previous computed display property value
    var previousDisplay = '';

    // Store the previous playback speed
    var previousSpeed = '';
  var revertSpeed = '';

    // Function to check if playback speed has changed to 2 and revert if necessary
    function checkPlaybackSpeed(revertIfNeeded = false) {
        var currentSpeed = document.querySelector("video").playbackRate;
      log("revertIfNeeded: " + revertIfNeeded);
      // document.querySelector("video").play();

        // Log the current playback speed
        log(`Current playback speed: ${currentSpeed}`);

      // Function to handle mouse-up and touchend events
function handleMouseUpOrTouchEnd() {
    // Play the video
    document.querySelector("video").play();

    // Detach the event listeners
    document.removeEventListener('mouseup', handleMouseUpOrTouchEnd);
    document.removeEventListener('touchend', handleMouseUpOrTouchEnd);
              // Revert the playback speed to the previous value
            localStorage.setItem("yt_playbackspeed", revertSpeed);
            document.querySelector("video").playbackRate = revertSpeed;
}

        // Check if the playback speed has changed to 2
        // if (revertIfNeeded && currentSpeed === 2 && previousSpeed !== 2) {
        if (revertIfNeeded) {
            log("Playback speed changed to 2 detected. Reverting...");
          revertSpeed = previousSpeed;

            // Revert the playback speed to the previous value
            localStorage.setItem("yt_playbackspeed", revertSpeed);
            document.querySelector("video").playbackRate = revertSpeed;


            // Log the playback speed to which it has been reverted
            log(`Playback speed reverted to ${revertSpeed}. Current playback speed is ${currentSpeed}.`);
              // Set up event listeners for mouse-up and touchend events
    document.addEventListener('mouseup', handleMouseUpOrTouchEnd);
    document.addEventListener('touchend', handleMouseUpOrTouchEnd);
        }

        // Update the previous playback speed
        previousSpeed = currentSpeed;
    }

    // Function to handle mutation events
    function handleMutation(mutationsList) {
        mutationsList.forEach(function(mutation) {
            if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
                // Get the computed style of the child element of .ytp-speedmaster-overlay
                var childElement = document.querySelector('.ytp-speedmaster-overlay > *');
                if (childElement) {
                    var computedStyle = window.getComputedStyle(childElement);
                    var currentDisplay = computedStyle.getPropertyValue('display');

                    // Check if the computed display property has changed
                    if (currentDisplay !== previousDisplay) {
                        log(`Computed display property changed to: ${currentDisplay}`);
                        // Update the previous display value
                        previousDisplay = currentDisplay;

                        // Check if the display property is no longer "none"
                        if (currentDisplay !== 'none') {
                            log("Speedmaster overlay detected. Checking playback speed...");
                            // Pass true to indicate reverting should be performed
                            checkPlaybackSpeed(true);
                        }
                    }
                }
            }
        });
    }

    // Monitor for changes to the style attribute of the speedmaster overlay's child element
    var observer = new MutationObserver(function(mutationsList) {
        log("Mutation observed.");
        handleMutation(mutationsList);
    });

    // Define the target node (speedmaster overlay)
    var targetNode = document.querySelector('.ytp-speedmaster-overlay');

    // Start observing for changes to the style attribute
    if (targetNode) {
        log("Speedmaster overlay found. Observing for changes...");
        observer.observe(targetNode, { attributes: true, subtree: true });
    } else {
        log("Speedmaster overlay not found. Monitoring playback speed directly.");
        // If the speedmaster overlay is not found, monitor playback speed directly
        checkPlaybackSpeed(); // Pass true to indicate reverting should be performed
    }

    // Function to check playback speed when user interaction occurs
    function handleUserInteraction() {
        log("User interaction detected. Checking playback speed...");
        checkPlaybackSpeed(); // Pass true to indicate reverting should be performed
    }

    // List of event types to listen for
    var eventTypes = ['mousedown', 'touchstart', 'keydown'];

    // Add event listeners for each event type
    eventTypes.forEach(function(eventType) {
        document.addEventListener(eventType, handleUserInteraction);
    });
}

// Call the monitorPlaybackSpeed function to start monitoring
monitorPlaybackSpeed();