Skip to content

Instantly share code, notes, and snippets.

@SupertigerDev
Last active September 12, 2024 12:19
Show Gist options
  • Save SupertigerDev/22f7f0573229b59879a5cb56a9b1fc45 to your computer and use it in GitHub Desktop.
Save SupertigerDev/22f7f0573229b59879a5cb56a9b1fc45 to your computer and use it in GitHub Desktop.
Tampermonkey add to playlist youtube shortcut P key
// ==UserScript==
// @name YT Add to playlist shortcut
// @namespace http://tampermonkey.net/
// @version 2024-09-12
// @description Adds a shortcut to the P button to show the "add to playlist" popup
// @author SupertigerDev
// @match *://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.addEventListener("keydown",function(event) {
if (event.key.toLowerCase() === "p") {
if (["input", "textarea"].includes(document.activeElement.tagName.toLowerCase())) return;
document?.querySelector('yt-button-shape button[aria-label="More actions"]')?.dispatchEvent?.(new Event("click"))
setTimeout(() => {
const success = [...document.querySelectorAll("ytd-menu-service-item-renderer")]?.at(-2)?.dispatchEvent?.(new Event("click"))
if (!success) {
const elements = document.querySelector(".style-scope .ytd-watch-metadata #menu").querySelectorAll(".yt-spec-button-view-model");
for (let i = 0; i < elements.length; i++) {
const el = elements[i];
if (el?.textContent === "Save") {
el?.parentElement?.querySelector?.("button")?.dispatchEvent?.(new Event("click"))
}
}
}
}, 300)
}
}, false)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment