YouTube Downloader (reload page!)

Adds download button for mobile and desktop site YouTube

// ==UserScript==
// @name         YouTube Downloader (reload page!)
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Adds download button for mobile and desktop site YouTube
// @author       Agreasyforkuser
// @match        https://*.youtube.com/*
// @exclude      https://*.youtube.com/tv*
// @icon         https://www.google.com/s2/favicons?domain=youtube.com
// @grant        GM_addStyle
// ==/UserScript==

var currentURL = window.location.href;

//////////////////////// Download Button ///////////////////////////////////////

    'use strict';
    // Function to handle the redirect
    function handleRedirect() {
        // Get the current YouTube video URL
        var currentUrl = window.location.href;
        // Extract the video ID from the URL
        var videoIdMatch = currentUrl.match(/\/watch\?v=([^&]+)/);
        var videoId = videoIdMatch ? videoIdMatch[1] : null;
        if (videoId) {
            // Redirect to the new URL with "pp" added
            var newUrl = 'https://www.youtubepp.com/watch?v=' + videoId;
            // Open the new URL in a new tab
            window.open(newUrl, '_blank');
        }
    }
    // Function to create and append the "Download" button
    function appendDownloadButton() {
        // Create the button element
        var downloadButton = document.createElement('button');
        //downloadButton.innerText = '';
        downloadButton.style.margin = '13px'; // Add some margin for better appearance
        downloadButton.style.color = 'white';
        downloadButton.style.opacity = '0.8';
        downloadButton.style.border = 'none';
        downloadButton.style.cursor = 'pointer'; // Set the cursor to pointer on hov
        downloadButton.addEventListener('click', handleRedirect);
        downloadButton.classList.add('downloadbutton');
        var existingElementMobile = document.querySelector('.slim-owner-icon-and-title');
        var existingElementDesktop = document.querySelector('.ytp-time-display');
        if (existingElementMobile) {
            existingElementMobile.parentNode.insertBefore(downloadButton, existingElementMobile.nextSibling);
        }
        if (existingElementDesktop) {
            existingElementDesktop.parentNode.insertBefore(downloadButton, existingElementDesktop.nextSibling);
        }

        if (currentURL.startsWith("https://www.youtube.com")) {

        GM_addStyle(`
              .downloadbutton:before {content: "Download 🡇" !important}
              .downloadbutton {background-color: black !important; border-radius: 6px !important}
                    `);
        };

//---------------------------------------------------------------------------------------------------------------------------
        if (currentURL.startsWith("https://m.youtube.com")) {

        GM_addStyle(`
                .downloadbutton:before {content: "Download ⤓" !important}
                .downloadbutton {color: gray !important; margin: 5px !important}
                    `);
        };
   };

    
    setTimeout(()=>{appendDownloadButton()},3000);