Skip to content

Instantly share code, notes, and snippets.

@Kuju29
Last active March 25, 2025 04:31
Show Gist options
  • Save Kuju29/11be1d31eff3e47e27738dcc348a80a6 to your computer and use it in GitHub Desktop.
Save Kuju29/11be1d31eff3e47e27738dcc348a80a6 to your computer and use it in GitHub Desktop.
youtube download button tampermonkey

📦 Step 1: Install yt-dlp for Command-Line Usage

Before using the extension, you need to install yt-dlp and make it available in the system so it can be triggered via command line. You can choose either method below:


✅ Option 1: Install via Chocolatey (Recommended)

  1. Install Chocolatey (if not already installed)
    Open PowerShell as Administrator and run:

    Set-ExecutionPolicy Bypass -Scope Process -Force; `
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; `
    iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
  2. Install yt-dlp using Chocolatey

    choco install yt-dlp

Chocolatey will automatically add yt-dlp to your system PATH, allowing it to be used from anywhere.

  1. Verify installation
    yt-dlp --version

🛠️ Option 2: Manual Installation

  1. Download yt-dlp.exe
    Visit yt-dlp Releases and download the latest yt-dlp.exe.

  2. Move the file to a permanent folder, such as:

    C:\yt-dlp\
    
  3. Add the folder to Environment Variables

    • Press Win + S and search for Environment Variables
    • Click “Edit the system environment variables”
    • In the System Properties window, click “Environment Variables...”
    • Under System variables, find and select Path, then click Edit
    • Click New, then enter the path:
      C:\yt-dlp\
      
    • Click OK to save and close all dialogs
  4. Verify installation

    yt-dlp --version

🧩 Step 2: Set Up Extension Integration (Tampermonkey + yt-dlp)

After installing yt-dlp, follow these steps to enable the extension to use it for downloading content:

  1. Install the Tampermonkey Script
    Click the link below to install the custom download button script (requires Tampermonkey extension installed in your browser):
    👉 Install Script

  2. Download and Apply the Registry File
    This file registers a custom command handler on your system so the script can trigger yt-dlp through the command: protocol.

    • Download: setcommand.reg
    • After downloading, double-click the file to apply it.
    • Click "Yes" when asked for confirmation.
  3. Move the yt-dlp.bat File to the Correct Folder
    This batch file will be triggered by the extension to run yt-dlp.

    • Download: yt-dlp.bat
    • Move or copy the downloaded yt-dlp.bat file to the following folder:
      C:\download
      
    • Create the folder if it does not already exist.

✅ Once all steps are completed, the extension will be able to call yt-dlp using the registered command: protocol and download videos directly.


image

; ----------------------------------------
; Press to use only once
; ----------------------------------------
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\yt-dlp]
@="URL:yt-dlp Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\yt-dlp\shell]
[HKEY_CLASSES_ROOT\yt-dlp\shell\open]
[HKEY_CLASSES_ROOT\yt-dlp\shell\open\command]
@="\"C:\\download\\yt-dlp.bat\" \"%1\""
// ==UserScript==
// @name yt-dlp Download Button
// @version 1.1
// @description This method downloads using yt-dlp to reduce reliance on external APIs. You need to install yt-dlp, run the setcommand.reg file, and move yt-dlp.bat to the C:\download folder first.
// @author Kuju29
// @match *://www.youtube.com/*
// @grant none
// @updateURL https://gist.githubusercontent.com/Kuju29/11be1d31eff3e47e27738dcc348a80a6/raw/Youtube%2520download%2520button.user.js
// @downloadURL https://gist.githubusercontent.com/Kuju29/11be1d31eff3e47e27738dcc348a80a6/raw/Youtube%2520download%2520button.user.js
// ==/UserScript==
(function() {
'use strict';
function addYtDlpButton() {
const container = document.querySelector('ytd-menu-renderer.style-scope.ytd-watch-metadata #top-level-buttons-computed');
if (!container) return;
if (container.querySelector('#yt-dlp-button')) return;
const button = document.createElement('button');
button.id = 'yt-dlp-button';
button.className = 'yt-spec-button-shape-next yt-spec-button-shape-next--tonal yt-spec-button-shape-next--mono yt-spec-button-shape-next--size-m';
button.style.cursor = 'pointer';
button.style.marginRight = '8px';
const iconDiv = document.createElement('div');
iconDiv.className = 'yt-spec-button-shape-next__icon';
iconDiv.setAttribute('aria-hidden', 'true');
iconDiv.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"
focusable="false" aria-hidden="true"
style="pointer-events: none; display: inherit; width: 100%; height: 100%;">
<path d="M17 18v1H6v-1h11zm-.5-6.6-.7-.7-3.8 3.7V4h-1v10.4l-3.8-3.8-.7.7 5 5 5-4.9z"></path>
</svg>`;
const textDiv = document.createElement('div');
textDiv.className = 'yt-spec-button-shape-next__button-text-content';
textDiv.innerHTML = '<span class="yt-core-attributed-string">yt-dlp</span>';
button.appendChild(iconDiv);
button.appendChild(textDiv);
button.addEventListener('click', function(e) {
e.preventDefault();
const videoUrl = window.location.href;
const customUrl = "yt-dlp://download?url=" + encodeURIComponent(videoUrl);
window.location.href = customUrl;
});
container.insertBefore(button, container.firstChild);
}
document.addEventListener('DOMContentLoaded', addYtDlpButton);
window.addEventListener('yt-page-data-updated', addYtDlpButton);
const observer = new MutationObserver(function() {
addYtDlpButton();
});
observer.observe(document.body, { childList: true, subtree: true });
})();
:: ----------------------------------------
:: move file to C:\download
:: ----------------------------------------
@echo off
setlocal EnableDelayedExpansion
set "outDir=%USERPROFILE%\Downloads"
set "fullUrl=%~1"
set "encodedUrl=%fullUrl:~24%"
set "videoId=%encodedUrl:~43%"
set "decodedUrl=https://www.youtube.com/watch?v=%videoId%"
echo Enter folder (e.g. C:\Downloads) or press ENTER for default: %outDir%:
set /p userInput="Output directory: "
if not "%userInput%"=="" set "outDir=%userInput%"
echo yt-dlp -f "bv*[vcodec^=avc1]+ba[ext=m4a]/b[vcodec^=avc1]" --merge-output-format mp4 -o "%outDir%\%%(title)s.%%(ext)s" "%decodedUrl%"
yt-dlp -f "bv*[vcodec^=avc1]+ba[ext=m4a]/b[vcodec^=avc1]" --merge-output-format mp4 -o "%outDir%\%%(title)s.%%(ext)s" "%decodedUrl%"

Comments are disabled for this gist.