Greasy Fork

DASH Video Player Optimizer(视频协议优化DASH篇)

Comprehensive enhancement for web video players, including quality switching (including 4K), online/offline detection, and more.

目前为 2025-03-15 提交的版本。查看 最新版本

// ==UserScript==
// @name         DASH Video Player Optimizer(视频协议优化DASH篇)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Comprehensive enhancement for web video players, including quality switching (including 4K), online/offline detection, and more.
// @author       KiwiFruit
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==
(function() {
    'use strict';

    // Function to switch video quality
    function switchVideoQuality(videoElement, qualityPath) {
        const sources = videoElement.querySelectorAll('source');
        sources.forEach(function(source) {
            source.src = qualityPath;
            videoElement.load();
        });
    }

    // Enhance the video player with additional controls and functionalities
    function enhanceVideoPlayer() {
        const videos = document.querySelectorAll('video');

        videos.forEach(function(video) {
            // Add responsive design CSS
            const style = document.createElement('style');
            style.type = 'text/css';
            style.innerHTML = `
                video {
                    max-width: 100%;
                    height: auto;
                }
            `;
            document.head.appendChild(style);

            // Play/Pause button
            const playPauseButton = document.createElement('button');
            playPauseButton.textContent = 'Play/Pause';
            playPauseButton.onclick = function() {
                if (video.paused) {
                    video.play();
                } else {
                    video.pause();
                }
            };
            video.parentNode.appendChild(playPauseButton);

            // Quality buttons
            const sources = video.querySelectorAll('source');
            if (sources.length > 0) {
                const currentPath = sources[0].src;
                const qualityPaths = {
                    '360p': currentPath.replace(/(\d+p)\.mp4$/, '360p.mp4'),
                    '480p': currentPath.replace(/(\d+p)\.mp4$/, '480p.mp4'),
                    '720p': currentPath.replace(/(\d+p)\.mp4$/, '720p.mp4'),
                    '1080p': currentPath.replace(/(\d+p)\.mp4$/, '1080p.mp4'),
                    '1440p': currentPath.replace(/(\d+p)\.mp4$/, '1440p.mp4'),
                    '2160p': currentPath.replace(/(\d+p)\.mp4$/, '2160p.mp4') // Added 4K option
                };

                for (const quality in qualityPaths) {
                    const qualityButton = document.createElement('button');
                    qualityButton.textContent = quality;
                    qualityButton.onclick = function() {
                        switchVideoQuality(video, qualityPaths[quality]);
                    };
                    video.parentNode.appendChild(qualityButton);
                }
            }

            // Error handling
            video.onerror = function(event) {
                console.error('Video error:', event);
                // Optionally show error message or try another source
            };
        });

        // Online/Offline detection
        window.addEventListener('online', function() {
            console.log('You are online');
        });
        window.addEventListener('offline', function() {
            console.log('You are offline');
        });
    }

    // Run the enhancement function when the script loads
    enhanceVideoPlayer();
})();