🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Video time tracker (localStorage)

Remembers the last watched time of videos on any website (including YouTube) and auto-deletes after 3 months

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         Video time tracker (localStorage)
// @namespace    https://greasyfork.org/
// @version      1.0
// @description  Remembers the last watched time of videos on any website (including YouTube) and auto-deletes after 3 months
// @author       Bui Quoc Dung
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const EXPIRATION_DAYS = 90; // Days
    const SAVE_INTERVAL = 1*1000; // Seconds*1000
    const SHORT_VIDEO_TIME = 10; // Days
    const YOUTUBE_URL_REGEX = /youtube\.com\/watch\?v=([^&]+)/;

    // Function to save time
    function saveTime(video, key) {
        if (!video || video.duration < SHORT_VIDEO_TIME) return;
        let data = { time: video.currentTime, timestamp: Date.now() };
        localStorage.setItem(key, JSON.stringify(data));
    }

    // Function to load and restore time
    function loadTime(video, key) {
        let savedData = localStorage.getItem(key);
        if (savedData) {
            let data = JSON.parse(savedData);
            let elapsedDays = (Date.now() - data.timestamp) / (1000 * 60 * 60 * 24);
            if (elapsedDays < EXPIRATION_DAYS && !isNaN(data.time)) {
                video.currentTime = parseFloat(data.time);
            } else {
                localStorage.removeItem(key); // Delete expired data
            }
        }
    }

    // Standard video handling
    function handleVideo(video) {
        if (!video || video.dataset.processed) return;
        video.dataset.processed = true;

        let key = `videoTime_${location.href}`;
        loadTime(video, key);
        let saveInterval = setInterval(() => saveTime(video, key), SAVE_INTERVAL);

        video.addEventListener('pause', () => clearInterval(saveInterval));
    }

    // YouTube-specific handling using the IFrame API
    function handleYouTube() {
        let match = location.href.match(YOUTUBE_URL_REGEX);
        if (!match) return;

        let videoId = match[1];
        let key = `youtubeTime_${videoId}`;

        // Wait for the YouTube player to load
        let checkPlayer = setInterval(() => {
            let player = document.querySelector('.html5-main-video');
            if (player) {
                clearInterval(checkPlayer);
                loadTime(player, key);
                let saveInterval = setInterval(() => saveTime(player, key), SAVE_INTERVAL);
                player.addEventListener('pause', () => clearInterval(saveInterval));
            }
        }, 1000);
    }

    // Observer to track new video elements
    function init() {
        if (location.hostname.includes('youtube.com')) {
            handleYouTube();
        } else {
            let observer = new MutationObserver(() => {
                document.querySelectorAll('video').forEach(video => handleVideo(video));
            });
            observer.observe(document.body, { childList: true, subtree: true });
        }
    }

    window.addEventListener('load', init);
})();