Add to playlist shortcut for youtube music

Adds a shortcut to the P button to add the current song to a playlist

目前為 2025-08-04 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Add to playlist shortcut for youtube music
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds a shortcut to the P button to add the current song to a playlist
// @author       MagnusRE
// @match        *://music.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @license MIT
// ==/UserScript==

// waitForElm from https://stackoverflow.com/a/61511955/
function waitForElm(selector) {
    return new Promise(resolve => {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector));
        }

        const observer = new MutationObserver(mutations => {
            if (document.querySelector(selector)) {
                observer.disconnect();
                resolve(document.querySelector(selector));
            }
        });

        // If you get "parameter 1 is not of type 'Node'" error, see https://stackoverflow.com/a/77855838/492336
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    });
}

(function() {
    'use strict';
    window.addEventListener("keydown",function(event) {

        var ae = document.activeElement.tagName;
        if(event.keyCode === 80 && ae != "INPUT") {
            console.log("i'm in");
            var popupMenu = document.querySelector(".middle-controls #button-shape .yt-spec-touch-feedback-shape");
            popupMenu.click();
            waitForElm("tp-yt-paper-listbox :nth-child(7) > #navigation-endpoint")
            .then((playlistLink) => {
            console.log("got ", playlistLink);
            playlistLink.click();
            });
        }
    }, false)
})();