Greasy Fork

来自缓存

Greasy Fork is available in English.

带自动清理的视频播放进度恢复脚本

自动恢复视频播放进度,并在30天后清理旧记录

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         带自动清理的视频播放进度恢复脚本
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  自动恢复视频播放进度,并在30天后清理旧记录
// @author       CWJ
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const videoSelector = 'video';
    const expirationDays = 30;

    function getVideoId(video) {
        return video.src || window.location.href;
    }

    function displayAlert(message) {
        const alertBox = document.createElement('div');
        alertBox.innerText = message;
        alertBox.style.position = 'fixed';
        alertBox.style.zIndex = '10000';
        alertBox.style.left = '50%';
        alertBox.style.top = '10%';
        alertBox.style.transform = 'translateX(-50%)';
        alertBox.style.background = 'rgba(0, 0, 0, 0.7)';
        alertBox.style.color = 'white';
        alertBox.style.padding = '10px';
        alertBox.style.borderRadius = '5px';
        document.body.appendChild(alertBox);

        setTimeout(() => {
            document.body.removeChild(alertBox);
        }, 2000);
    }

    function clearExpiredData() {
        const data = localStorage.getItem('savedVideoData');
        const videosData = data ? JSON.parse(data) : {};
        const currentTime = new Date().getTime();

        Object.entries(videosData).forEach(([key, value]) => {
            if (currentTime - value.timestamp > expirationDays * 24 * 60 * 60 * 1000) {
                delete videosData[key];
            }
        });

        localStorage.setItem('savedVideoData', JSON.stringify(videosData));
    }

    window.addEventListener('load', () => {
        clearExpiredData();

        const video = document.querySelector(videoSelector);
        if (video) {
            const videoId = getVideoId(video);
            const data = localStorage.getItem('savedVideoData');
            const videosData = data ? JSON.parse(data) : {};

            if (videosData[videoId] && videosData[videoId].currentTime) {
                video.currentTime = parseFloat(videosData[videoId].currentTime);
                displayAlert('从上次播放');
            }

            video.addEventListener('timeupdate', () => {
                videosData[videoId] = { currentTime: video.currentTime, timestamp: new Date().getTime() };
                localStorage.setItem('savedVideoData', JSON.stringify(videosData));
            });
        }
    });
})();