Greasy Fork

Greasy Fork is available in English.

视频倍速修改

自定义页面所有视频播放速度,默认1.5倍

当前为 2024-12-27 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         视频倍速修改
// @version      1.02
// @description  自定义页面所有视频播放速度,默认1.5倍
// @author       Lama AI 辅助
// @match        http://*/*
// @match        https://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-end
// @namespace    http://greasyfork.icu/users/1171320
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let defaultSpeed = 1.5;
    let currentSpeed = defaultSpeed;
    let savedSpeed = GM_getValue('videoSpeed');

    if (savedSpeed) {
        defaultSpeed = parseFloat(savedSpeed);
        currentSpeed = defaultSpeed;
    }

    function updateSpeed(newSpeed) {
        let videos = document.querySelectorAll('video'); // Simpler selector for now
        videos.forEach(video => {
            try {
                video.playbackRate = parseFloat(newSpeed);
            } catch (error) {
                console.error("Error setting playbackRate:", error, video);
            }
        });
        currentSpeed = newSpeed;
    }

    function saveSpeed(newSpeed) {
        GM_setValue('videoSpeed', newSpeed);
    }

    function applySpeedToAllVideos() {
        updateSpeed(currentSpeed);
        let iframes = document.querySelectorAll('iframe');
        iframes.forEach(iframe => {
            try {
                const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
                const iframeVideos = iframeDoc.querySelectorAll('video');
                iframeVideos.forEach(video => {
                    try {
                        video.playbackRate = currentSpeed;
                    } catch (error) {
                        console.error("Error setting playbackRate in iframe:", error, video);
                    }
                });
            } catch (error) {
                console.error("Error accessing iframe content:", error, iframe);
            }
        });
    }

    // Add an observer to catch videos loaded later
    const observer = new MutationObserver(applySpeedToAllVideos);
    observer.observe(document.body, { childList: true, subtree: true });

    //Initial application
    applySpeedToAllVideos();

    // UI control
    let control = document.createElement('div');
    control.textContent = '倍速';
    control.style.cssText = `
        position: fixed;
        right: 5px;
        top: 30px;
        padding: 2px;
        background: #ddd;
        border: 1px solid #999;
        cursor: pointer;
        z-index: 9999;
    `;

    document.body.appendChild(control);

    control.addEventListener('click', () => {
        let newSpeed = prompt('输入播放倍速:', currentSpeed);
        if (newSpeed) {
            saveSpeed(newSpeed);
            updateSpeed(newSpeed);
        }
    });
})();