PIP youtube enabled

Adds a button for watching YouTube videos in picture in picture mode while viewing comments.

当前为 2023-11-02 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         PIP youtube enabled
// @namespace    none
// @version      0.1
// @description  Adds a button for watching YouTube videos in picture in picture mode while viewing comments.
// @author       Johnny Vo
// @license      GPLv3
// @match        https://youtube.com/watch*
// @match        https://m.youtube.com/watch*
// @icon         https://cdn-icons-png.flaticon.com/512/1412/1412577.png
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    if ('pictureInPictureEnabled' in document) {
        const videoElement = document.getElementsByClassName("video-stream")[0];
        const pipButtonElement = document.createElement("button");
        pipButtonElement.innerHTML = "Pip Mode";
        pipButtonElement.style.position = "fixed";
        pipButtonElement.style.right = "2rem";
        pipButtonElement.style.bottom = "2rem";
        pipButtonElement.style.visibility = "hidden";
        pipButtonElement.style.width = "10rem";
        pipButtonElement.style.height = "3.5rem";
        pipButtonElement.style.borderRadius = "20px";
        pipButtonElement.style.border = "1px solid";
        pipButtonElement.style.backgroundColor = "#212121";
        pipButtonElement.style.color = "#f1f1f1";
        pipButtonElement.style.cursor = "pointer";
        document.body.appendChild(pipButtonElement);

        const isVideoVisible = (element) => {
            const rect = element.getBoundingClientRect();

            if (rect.top + rect.height > 0 && rect.top < window.innerHeight) {
                return true;
            }
            return false;
        };

        document.addEventListener("scroll", async () => {
            if (isVideoVisible(videoElement)) {
                pipButtonElement.style.visibility = "hidden";
                try {
                    if (videoElement === document.pictureInPictureElement) {
                        await document.exitPictureInPicture();
                    }
                } catch (err) {
                    console.log(err)
                }
            } else {
                if (videoElement !== document.pictureInPictureElement) {
                    pipButtonElement.style.visibility = "visible";
                    pipButtonElement.addEventListener("click", async () => {
                        pipButtonElement.disabled = true;

                        try {
                            await videoElement.requestPictureInPicture();
                            pipButtonElement.style.visibility = "hidden";
                        } catch (err) {
                            console.log(err)
                        } finally {
                            pipButtonElement.disabled = false;
                        }
                    });
                }
            }
        });
    } else {
        alert('No support of PIP in Browser');
    }
})();