Youtube Resumer (using the url)

Changes the ?t= parameter when pausing. Other version: https://greasyfork.org/en/scripts/455475-youtube-resumer

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Youtube Resumer (using the url)
// @description  Changes the ?t= parameter when pausing. Other version: https://greasyfork.org/en/scripts/455475-youtube-resumer
// @version      4
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @namespace    https://greasyfork.org/users/206408
// ==/UserScript==

(async () => {

    function l(...args){
        console.log(`[Youtube Resumer]`, ...args)
    }

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

    //update ?t=
    function changeUrl(time){
        const url = new URL(window.location.href)
        url.searchParams.set('t', time)
        window.history.replaceState(null, null, url)
    }

    function listen(){
        const video = findVideo()
        video.addEventListener('pause', () => {
            changeUrl(parseInt(video.currentTime))
        })
    }

    let listening = false //the video element exists even if you go back to the home page, so no need to readd event listeners

    //Event for each page change
    document.addEventListener("yt-navigate-finish", function() {
        l('navigate-finish')
        //Match page with video
        if(window.location.href.match(new RegExp('https://www.youtube.com/watch\\?v=.'))) {
            //Add video listener once
            if(!listening){
                l('listening')
                listen()
                listening = true
            }
        }
    });
})();