YouTube.com add PIP picture in picture pop-out button (Mobile / Desktop)

Adds a pop out to PIP button below the video. Works on mobile, tablet (m.youtube.com) and on desktop.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         YouTube.com add PIP picture in picture pop-out button (Mobile / Desktop)
// @namespace    m-youtube-com-pip-button
// @version      1.4
// @description  Adds a pop out to PIP button below the video. Works on mobile, tablet (m.youtube.com) and on desktop.
// @author       hlorand.hu
// @match        https://m.youtube.com/*
// @match        https://youtube.com/*
// @match        https://*.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        none
// @license      https://creativecommons.org/licenses/by-nc-sa/4.0/
// ==/UserScript==


(function() {
    //'use strict';
    function addbutton(){
        document.getElementById("pipbutton").innerText = "";

        let button = document.createElement('button');
        button.textContent = "PIP";
        button.className = "pipbutton";

        button.style.margin = "4px";
        button.style.padding = "6px";

        button.style.backgroundColor = "brown";
        button.style.position = "relative";

        button.onclick = function() {
            let video = document.querySelector("video");
            video.disablePictureInPicture = false;
            video.requestPictureInPicture();
        };

        let target = document.getElementById("pipbutton");
        target.insertBefore(button, target.firstChild);


    } // end addbuttons

    // Periodically check if the buttons are visible
    // (sometimes YouTube redraws its interface).
    setInterval(()=>{
        // Creating a div that will contain buttons.
        if( document.getElementById("pipbutton") == undefined ){
            
            // placement of buttons on desktop
            let parent = document.getElementById('above-the-fold');

            // placement of buttons on tablet
            if( !parent ){
                parent = document.querySelector('.watch-below-the-player');
            }

            // placement of buttons on mobile
            if( !parent ){
                parent = document.querySelector('.related-chips-slot-wrapper');
            }

            let wrapper = document.createElement('div');
            wrapper.setAttribute("id","pipbutton");
            parent.insertBefore(wrapper, parent.firstChild);
            addbutton();

        }

        // Sometimes the buttons are not added, so I check and add them if necessary.
        if( document.getElementById("qualitybuttons") === undefined ){
            addbutton();
        }

    }, 1000);

})();