Greasy Fork is available in English.

YouTube Timestamp Keeper

Keep recent timestamp in URL on YouTube

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         YouTube Timestamp Keeper
// @namespace    Violentmonkey Scripts
// @version      0.1
// @description  Keep recent timestamp in URL on YouTube
// @author       EtiamNullam
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @license GPL-3.0-only
// ==/UserScript==
(function() {
    'use strict'

    /**
     * @param {number} durationMs
     */
    function Watcher(durationMs) {
        const watcher = {}
        let interval = null

        function check() {
            const player = getPlayer()

            if (!player || player.paused) {
                watcher.stop()
            } else {
                updateTimestamp()
            }
        }

        watcher.stop = () => {
            clearInterval(interval)

            interval = null
        }

        watcher.start = () => {
            watcher.stop()

            interval = setInterval(check, durationMs)
        }

        return watcher
    }

    const TIMESTAMP_UPDATE_INTERVAL_MS = 900
    const PLAYER_RETRY_DELAY_MS = 1000

    const watcher = Watcher(TIMESTAMP_UPDATE_INTERVAL_MS)

    const playerActionEvents = [
        { event: 'pause', action: updateTimestamp },
        { event: 'seeked', action: updateTimestamp },
        { event: 'play', action: watcher.start },
        { event: 'pause', action: watcher.stop },
    ]


    function getPlayer() {
        return document.querySelector('video')
    }

    function updateTimestamp() {
        const player = getPlayer()

        if (!player) {
            console.warn('Cannot find player')
            return
        }

        const currentUrl = new URL(window.location.href)
        const secondsElapsed = Math.floor(player.currentTime)
        const { searchParams } = currentUrl

        searchParams.set('t', secondsElapsed + 's')

        currentUrl.search = searchParams.toString()

        window.history.replaceState({}, '', currentUrl)
    }

    function setup() {
        if (window.location.pathname !== '/watch') {
            return
        }

        const player = getPlayer()

        if (!player) {
            console.warn('Cannot find player')
            setTimeout(setup, PLAYER_RETRY_DELAY_MS)

            return
        }

        for (const actionEvent of playerActionEvents) {
            player.addEventListener(actionEvent.event, actionEvent.action)
        }

        updateTimestamp()

        if (!player.paused) {
            watcher.start()
        }
    }

    function cleanUp() {
        const player = getPlayer()

        for (const actionEvent of playerActionEvents) {
            player.removeEventListener(actionEvent.event, actionEvent.action)
        }
    }

    window.addEventListener('yt-navigate-start', cleanUp)
    window.addEventListener('yt-navigate-finish', setup)
    window.addEventListener('beforeunload', updateTimestamp)
})()