Youtube scroll lock timestamp in picture-in-picture

When using Picture-in-Picture (PiP), don't scroll to the top of the page when clicking on a timestamp that displays the time of the video.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           Youtube scroll lock timestamp in picture-in-picture
// @name:ja        YouTubeでピクチャーインピクチャー使用時、タイムスタンプでページトップへの遷移防止
// @namespace      https://github.com/ziopuzzle/
// @version        1.4
// @description    When using Picture-in-Picture (PiP), don't scroll to the top of the page when clicking on a timestamp that displays the time of the video.
// @description:ja 動画をポップアウトしていても、タイムスタンプをクリック時にページトップに強制スクロールする現象を解決します。
// @author         ziopuzzle
// @match          https://www.youtube.com/*
// @grant          none
// @license        MIT
// ==/UserScript==

(function() {
    'use strict';

    const SEND_LOG = false;

    function sendLog(s) {
        if (SEND_LOG) {
            console.log('[PIP_SCROLL_LOCK] ' + s);
        }
    }

    function checkPIP() {
        const usingPIP = document.pictureInPictureElement != null;
        // Support for https://greasyfork.org/en/scripts/444382-youtube-mini-player
        const usingFixed = document.querySelector('.ytd-player').style.position == 'fixed';
        // Firefox does not support the Picture-in-Picture API, so we assume that PIP will always be used.
        const isFirefox = navigator.userAgent.indexOf('Firefox') !== -1;
        return usingPIP || usingFixed || isFirefox;
    }

    function isThisVideoLink(e) {
        const videoID = new URL(window.location.href).searchParams.get('v');
        const linkVideoID = new URL(e.href).searchParams.get('v');
        return videoID && videoID == linkVideoID;
    }

    function timestampToSeconds(t){
        let parts = t.split(':').reverse();
        if (parts.length < 2) {
            return false;
        }
        let seconds = 0;
        for(let i = 0; i < parts.length; i++){
            switch (i) {
                case 0: seconds += (+parts[i]); break;
                case 1: seconds += (+parts[i])*60; break;
                case 2: seconds += (+parts[i])*60*60; break;
                case 3: seconds += (+parts[i])*60*60*24; break;
            }
        }
        return Number.isInteger(seconds) ? seconds : null;
    }

    document.addEventListener("click", function(e){
        const target =
              e.target.tagName=='A' /* timestamp */
                  ? e.target
                  : e.target.closest("a#endpoint") /* chapter */
                      ? e.target.closest("a#endpoint").querySelector("#details #time")
                      : null;

        if (!isThisVideoLink(target)) {
            sendLog('Link is another video link');
            return;
        }

        const seconds = timestampToSeconds(target.innerText);
        if (seconds !== null) {
            sendLog('Link is valid');
            if (checkPIP()) {
                e.preventDefault();
                e.stopPropagation();
                e.stopImmediatePropagation();
                window.movie_player.seekTo(seconds);
                sendLog("seek to " + seconds + "s(" + target.innerText + ")");
            } else {
                sendLog('not PIP mode');
            }
            return;
        } else {
            sendLog('Link is invalid');
        }
    }, {capture: true} );

})();