nastykast / WAYL2 User Playlist

// ==UserScript==
// @name        WAYL2 User Playlist
// @description Create YouTube playlists from a user's WAYL2 posts.
// @include     https://passthepopcorn.me/forums.php?page=1&action=viewthread&threadid=36167*
// @include  https://passthepopcorn.me/forums.php?action=viewthread&threadid=36167
// @grant       none
// @version     1.0
// @copyright 2021, nastykast (https://openuserjs.org/users/nastykast)
// @license MIT
// @updateURL https://openuserjs.org/meta/nastykast/WAYL2_User_Playlist.meta.js
// ==/UserScript==

// ==OpenUserJS==
// @author nastykast
// ==/OpenUserJS==

(function() {
  'use strict';
  const op = document.querySelectorAll('.forum_post, .forum-post')[0];
  const hide_blocks = op.getElementsByClassName('spoiler');
  for (var i = 0; i < hide_blocks.length; i++) {
    var block = hide_blocks[i];
    if (block.previousSibling.nodeName == "A") {
      var button = document.createElement('a');
      button.href = 'javascript:void(0);';
      button.innerHTML = 'Generate playlist(s)';
      button.addEventListener('click', generate.bind(undefined, block, button), false);
      block.prepend(button, document.createElement('br'));
    }
  }
})();

function generate(hide_block, button) {
  var links = hide_block.getElementsByTagName('a');
  var idLists = [[]];
  var listNum = 0;
  for (var i = 0; i < links.length; i++) {
    if (idLists[listNum].length == 50) {
      listNum++;
      idLists[listNum] = [];
    }
    var id = '';
    if (links[i].href.includes("youtube")) {
      var pieces = links[i].href.split("?");
      if (pieces.length > 1) {    
        var params = pieces[1].split("&");
        for (var j = 0; j < params.length; j++) {
          if (params[j].startsWith("v=")) {
            id = params[j].substring(2);
          }
        }
      }
    } else if (links[i].href.includes("youtu.be")) {
      id = links[i].href.split("/").pop().split('?')[0];
    }
    if (id) {
      idLists[listNum].push(id);
    }
  }
  button.remove();
  for (var i = idLists.length - 1; i >= 0; i--) {
    var button = document.createElement('a');
    button.href = 'https://youtube.com/watch_videos?video_ids=' + idLists[i].join();
    button.innerHTML = 'Tracks ' + (i*50 + 1) + ' - ' + (i*50 + idLists[i].length);
    if (i != idLists.length - 1) {
      hide_block.prepend(button, document.createElement('br'));
    } else {
      hide_block.prepend(button);
    }
  }
}