PN Script DL Btn

Adds a download btn to download all availble Pn whenever possible

As of 2021-06-23. See the latest version.

// ==UserScript==
// @name         PN Script DL Btn
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds a download btn to download all availble Pn whenever possible
// @author       DA25
// @match        *://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  document.addEventListener('readystatechange', event => {
    if (event.target.readyState === "complete") {
      if (window.flashvars) {
        console.log("Found");
        console.log(window.flashvars);

        function fixVidUrl(vidUrl) {
          if (!!vidUrl) {
            return vidUrl.replace("function/0/", "");
          } else {
            return undefined
          }
        }
        function getDefaultVidUrl() {
          return fixVidUrl(window.flashvars["video_url"]);
        }
        function getDefaultVidText() {
          return window.flashvars["video_url_text"];
        }
        function getVidAltUrl(num) {
          let urlNumStr = "";
          if (num === 1) {
            urlNumStr = `video_alt_url`;
          } else {
            urlNumStr = `video_alt_url${num}`;
          }
          return fixVidUrl(window.flashvars[urlNumStr]);
        }
        function getVidAltText(num) {
          let textNumStr = "";
          if (num === 1) {
            textNumStr = `video_alt_url_text`;
          } else {
            textNumStr = `video_alt_url${num}_text`;
          }
          return window.flashvars[textNumStr];
        }
        function createDownloadLinkEle(altUrl, altText) {
          let altAnchorEle = document.createElement("a");
          altAnchorEle.classList.add("dropdown-item", "btn-sm");
          altAnchorEle.setAttribute("href", altUrl);
          altAnchorEle.innerText = altText;
          return altAnchorEle;
        }
        function createdDownloadEle() {
          let dropdownWrapper = document.createElement("div");
          dropdownWrapper.classList.add("dropdown");

          let downloadBtn = document.createElement("button");
          downloadBtn.classList.add("btn", "btn-dark", "btn-sm", "dropdown-toggle");
          downloadBtn.setAttribute("type", "button");
          downloadBtn.setAttribute("data-bs-toggle", "dropdown");
          downloadBtn.innerText = "DA25-Download";

          dropdownWrapper.append(downloadBtn);

          let btnList = document.createElement("ul");
          btnList.classList.add("dropdown-menu", "btn-sm");

          dropdownWrapper.append(btnList);

          return dropdownWrapper;
        }
        function appendDownloadLinkEle(downloadEle, linkEle) {
          let downloadBtnList = downloadEle.querySelector("ul.dropdown-menu");
          let listItem = document.createElement("li");
          listItem.append(linkEle);
          downloadBtnList.append(listItem);
        }

        function addBootstrap() {
          let head = document.getElementsByTagName("head")[0];
          let bootstrapCssScriptEle = document.createElement("link");
          bootstrapCssScriptEle.setAttribute("href", "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css");
          bootstrapCssScriptEle.setAttribute("rel", "stylesheet");
          bootstrapCssScriptEle.setAttribute("integrity", "sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC");
          bootstrapCssScriptEle.setAttribute("crossorigin", "anonymous");
          head.insertAdjacentElement("beforeend", bootstrapCssScriptEle);

          let body = document.getElementsByTagName("body")[0];
          let bootstrapJsScriptEle = document.createElement("script");
          bootstrapJsScriptEle.setAttribute("src", "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js");
          bootstrapJsScriptEle.setAttribute("integrity", "sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM");
          bootstrapJsScriptEle.setAttribute("crossorigin", "anonymous");
          body.insertAdjacentElement("beforeend", bootstrapJsScriptEle);
        }

        function initDownloadLink(downloadDropDownEle) {
          let listEle = document.createElement("li");
          listEle.append(downloadDropDownEle);

          let menuList = document.querySelector(".tabs-menu > ul");

          if (!!menuList) {
            menuList.prepend(listEle);
            addBootstrap();
          } else {
            alert("window.flashvars present but '.tabs-menu > ul' is not");
          }
        }

        try {
          let defaultVidUrl = getDefaultVidUrl();
          let defaultVidText = getDefaultVidText();
          let defaultDownloadEle = createdDownloadEle();
          appendDownloadLinkEle(defaultDownloadEle, createDownloadLinkEle(defaultVidUrl, defaultVidText));

          let count = 1;
          let vidAltUrl = getVidAltUrl(count);
          let vidAltText = getVidAltText(count);
          while (!!vidAltUrl) {
            appendDownloadLinkEle(defaultDownloadEle, createDownloadLinkEle(vidAltUrl, vidAltText));
            count = count + 1;
            vidAltUrl = getVidAltUrl(count);
            vidAltText = getVidAltText(count);
          }

          initDownloadLink(defaultDownloadEle);
        } catch (e) {
          alert(e);
        }
      } else {
        console.log("Not Found");
      }
    }
  });
})();