Youtube Save to Playlist Filter

When you want to add a video to a certain playlist, it is sometimes difficult to find the right playlist again. That's why I add a filter input to the Save to Playlist popup at the top so you can filter the playlists.

Fra 27.11.2021. Se den seneste versjonen.

/**
  The MIT License (MIT)

  Copyright (c) 2021 Jan-Felix Wittmann
  
  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/
// ==UserScript==
// @id               YoutubeSavetoPlaylistFilter
// @name             Youtube Save to Playlist Filter
// @description      When you want to add a video to a certain playlist, it is sometimes difficult to find the right playlist again. That's why I add a filter input to the Save to Playlist popup at the top so you can filter the playlists.
// @version          0.2
// @author           Jan-Felix Wittmann <[email protected]>
// @copyright 2021, leobm (https://openuserjs.org/users/leobm)
// @license MIT
// @match            http://www.youtube.com/*
// @match            https://www.youtube.com/*
// @include          http://www.youtube.com/*
// @include          https://www.youtube.com/*
// @run-at           document-start
// @grant            none
// @namespace https://greasyfork.org/users/844851
// ==/UserScript==


(function (d) {

  var updatePlaylist = function (playlist, filterTitle) {
    var options = query(playlist, 'ytd-playlist-add-to-option-renderer');
    options.forEach(op => {
      var formattedString = query(op, 'yt-formatted-string')[0];
      var title = formattedString.getAttribute('title');
      let re = new RegExp('^' + filterTitle, 'i');
      if (filterTitle && !re.test(title)) {
        op.style.display = "none";
      }
      else {
        op.style.display = "block";
      }
    });
  };

  var handlePopupContainer = function (popupContainer) {
    var curFilterTitle = '';
    const popupContainerObserver = new MutationObserver(function () {
      var playlist = query(popupContainer, 'ytd-add-to-playlist-renderer #playlists')[0];
      if (playlist) {
        var filterInput = query(playlist, '#playlists-filter')[0];
        if (!filterInput) {
          curFilterTitle = '';
          filterInput = d.createElement("input");
          filterInput.setAttribute('type', 'text');
          filterInput.setAttribute('id', 'playlists-filter');
          filterInput.setAttribute('placeholder', 'Playlist Filter');
          filterInput.setAttribute('style', 'display:inline-block;font-family: Roboto, Arial, sans-serif; padding: 0.4em 0.6em;  margin: 0.8em 0; outline: 0;width: 100%;border: 1px solid #ccc;box-shadow: none;border-radius: 4px;');
          filterInput.addEventListener('input', (evt) => {
            var options = query(playlist, 'ytd-playlist-add-to-option-renderer');
            curFilterTitle = evt.target.value;
            updatePlaylist(playlist, curFilterTitle);
          }, true);

          playlist.insertBefore(filterInput, playlist.firstChild);
        }
        updatePlaylist(playlist, curFilterTitle);
      }
    });

    popupContainerObserver.observe(popupContainer, {
      subtree: true,
      childList: true
    });
  };

  var documentObserver = new MutationObserver(function (mutations, me) {
    var popupContainer = query(d, 'ytd-popup-container')[0];
    if (popupContainer) {
      handlePopupContainer(popupContainer);
      me.disconnect(); // stop observing
      return;
    }
  });

  documentObserver.observe(d, {
    childList: true,
    subtree: true
  });

  function query(startNode, selector) {
    try {
      var nodes = Array.prototype.slice.call(
        startNode.querySelectorAll(selector));
      return nodes;
    }
    catch (e) {}
    return [];
  }

})(document);