YouTube - URL Playtime Keeper

Updates the browser URL with the current timestamp every 30 seconds so playback resumes where you left if you restart the browser,

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube - URL Playtime Keeper
// @namespace    https://github.com/ChemaZapiens/dev/tree/main/random/userscript
// @version      01
// @description  Updates the browser URL with the current timestamp every 30 seconds so playback resumes where you left if you restart the browser, 
// @description  bookmark the video, use The Greater Discarder, etc.
// @description  You can also quickly copy the URL or modify the time (&t=1m30s)
// @author       Chema Zapiens
// @license      GPL 3.0
// @match        *://www.youtube.com/watch*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function formatTime(seconds) {
        const minutes = Math.floor(seconds / 60);
        const secs = seconds % 60;
        return `${minutes}m${secs}s`;
    }

    function updateUrlWithTimestamp() {
        const player = document.querySelector('video');
        if (!player) return;

        const currentTime = Math.floor(player.currentTime);
        const formattedTime = formatTime(currentTime);
        const url = new URL(window.location.href);

        if (url.searchParams.get('t') !== formattedTime) {
            url.searchParams.set('t', formattedTime);
            window.history.replaceState(null, '', url.toString());
        }
    }

    setInterval(updateUrlWithTimestamp, 30000);
})();