Export Youtube Playlist in plaintext

Shows a list of the playlist video names in plaintext to be easily copied

Verze ze dne 10. 06. 2021. Zobrazit nejnovější verzi.

// ==UserScript==
// @name         Export Youtube Playlist in plaintext
// @namespace    1N07
// @version      0.6
// @description  Shows a list of the playlist video names in plaintext to be easily copied
// @author       1N07
// @require      https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @include      https://www.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var listCreationAllowed = true;
    var urlAtLastCheck = "";

    //add some CSS
    if(true) {
        addGlobalStyle(`
			tp-yt-paper-listbox#items { overflow-x: hidden; }

			#exportPlainTextList {
				cursor: pointer;
				height: 36px;
				width: 100%;
				display: flex;
				align-items: center;
			}
			#exportPlainTextList > img {
				height: 24px; width: 24px;
				color: rgb(144, 144, 144);
				padding: 0 13px 0 16px;
				filter: contrast(0%);
			}
			#exportPlainTextList > span {
				color: var(--yt-spec-text-primary);
				white-space: nowrap;
				font-size: var(--ytd-user-comment_-_font-size);
				font-weight: var(--ytd-user-comment_-_font-weight);
				line-height: var(--ytd-user-comment_-_line-height);
				letter-spacing: var(--ytd-user-comment_-_letter-spacing);
			}

			#exportPlainTextList:hover { background-color: rgba(255,255,255,0.1); }
			ytd-menu-popup-renderer.ytd-popup-container { overflow-x: hidden !important; max-height: none !important; }

			#listDisplayContainer {
				position: fixed;
				z-index: 9999;
				margin: 0 auto;
				background-color: #464646;
				padding: 10px;
				border-radius: 5px;
				left: 0;
				right: 0;
				max-width: 100vw;
				width: 750px;
				height: 750px;
				max-height: 90vh;
				top: 5vh;
			}
			#listDisplayContainer > textarea {
				box-sizing: border-box;
				width: 100%;
				margin: 10px 0;
				height: calc(100% - 40px);
				background-color: #262626;
				border: none;
				color: #EEE;
				border-radius: 5px;
			}
			#closeTheListThing {
				float: right;
				font-weight: bold;
				background-color: RGBA(255,255,255,0.25);
				border: none;
				font-size: 17px;
				border-radius: 10px;
				height: 25px;
				width: 25px;
				cursor: pointer;
			}

			#closeTheListThing:hover { background-color: rgba(255,255,255,0.5); }
		`);
    }

    setInterval(function(){
        if(urlAtLastCheck != window.location.href)
        {
            urlAtLastCheck = window.location.href;
            if(urlAtLastCheck.includes("/playlist?list="))
                InsertButtonASAP();
        }
    }, 100);


    function InsertButtonASAP()
    {
        let buttonInsertInterval = setInterval(function(){
            //wait for possible previous buttons to stop existing (due to how youtube loads pages) and for the space for the new button to be available
            if($("#exportPlainTextList").length == 0 && $("tp-yt-paper-listbox#items").length > 0)
            {
                $("tp-yt-paper-listbox#items").append(`
					<div id="exportPlainTextList">
						<img src="https://i.imgur.com/emlur3a.png">
						<span>Export Playlist</span>
					</div>
				`);
                $("#exportPlainTextList").click(ScrollUntillAllVisible);
                clearInterval(buttonInsertInterval);
            }
        }, 100);
    }

    function ScrollUntillAllVisible()
    {
        if(!listCreationAllowed)
            return;

        $("ytd-browse[page-subtype='playlist']").click();

        listCreationAllowed = false;
        $("#exportPlainTextList").after(`<p id="listBuildMessage" style="color: red; font-size: 1.33em;">Getting list...<br>please click out of the popup to continue autoscrolling...</p>`);
        let scrollInterval = setInterval(function(){
            if($("ytd-continuation-item-renderer.ytd-playlist-video-list-renderer").length)
                $(document).scrollTop($(document).height());
            else
            {
                $("#listBuildMessage").remove();
                BuildAndDisplayList();
                clearInterval(scrollInterval);
            }
        }, 100);
    }

    function BuildAndDisplayList()
    {
        let list = "";
        $("ytd-playlist-video-renderer #content #video-title").each(function(){
            list += $(this).attr("title") + "\n";
        });
        $("body").append(`
			<div id="listDisplayContainer">
				<p style="text-align: center;">
					<span style="font-size: 21px; font-weight: bold; color: #d9d9d9;">Playlist in plain text</span>
					<button id="closeTheListThing">X</button>
				</p>
				<textarea>`+list+`</textarea>
			</div>
		`);
        $("#listBuildMessage").remove();
        $("#closeTheListThing").click(function(){
            $("#listDisplayContainer").remove();
            listCreationAllowed = true;
        });
    }

    function addGlobalStyle(css)
    {
        var head, style;
        head = document.getElementsByTagName('head')[0];
        if (!head) { return; }
        style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = css;
        head.appendChild(style);
    }
})();