Download No Wait by sylin527

Download No Wait. No memory leak, performance. When you open mod files page or click files tab, it will add buttons, texted "Download No Wait", below file description. Also works on archived files.

目前為 2023-01-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name        Download No Wait by sylin527
// @namespace   https://www.nexusmods.com
// @match       https://www.nexusmods.com/*/mods/*
// @run-at      document-idle
// @version     0.1.0.23.1.25
// @license     GPLv3
// @grant       unsafeWindow
// @icon        https://www.nexusmods.com/favicon.ico
// @author      sylin527
// @description Download No Wait. No memory leak, performance. When you open mod files page or click files tab, it will add buttons, texted "Download No Wait", below file description. Also works on archived files.
// ==/UserScript==
(() => {
  // src/api/download_api.ts
  async function generateDownloadUrl(gameId, fileId) {
    const res = await fetch("/Core/Libs/Common/Managers/Downloads?GenerateDownloadUrl", {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
      },
      body: `game_id=${gameId}&fid=${fileId}`,
      method: "POST"
    });
    const resJson = await res.json();
    return resJson.url;
  }

  // src/shared.ts
  function getGameId() {
    return current_game_id;
  }

  // src/userscripts/shared.ts
  var body = document.body;

  // src/userscripts/tabs_shared.ts
  var { body: body2, head } = document;
  var section = null;
  function getSection() {
    !section && (section = document.getElementById("section"));
    return section;
  }
  var modTabsUl = null;
  function getModTabs() {
    !modTabsUl && (modTabsUl = getSection().querySelector("div.tabs ul.modtabs"));
    return modTabsUl;
  }
  function getCurrentTab() {
    const modTabs = getModTabs();
    const tabSpan = modTabs.querySelector("li a.selected span");
    return tabSpan.innerText.toLowerCase();
  }
  function clickModTabs(callback) {
    const modTabs = getModTabs();
    modTabs.addEventListener("click", (event) => {
      const target = event.target;
      let tabSpan;
      if (target instanceof HTMLAnchorElement) {
        tabSpan = target.querySelector("span:first-child");
      } else {
        tabSpan = target.parentElement.querySelector("span:first-child");
      }
      const newTab = tabSpan.innerText.toLowerCase();
      callback(newTab, event);
    });
  }
  var oldTab = getCurrentTab();

  // src/userscripts/files_tab.ts
  var _modFilesDiv = null;
  function getModFilesDiv() {
    !_modFilesDiv && (_modFilesDiv = document.getElementById("mod_files"));
    return _modFilesDiv;
  }
  function clearElementCache() {
    _modFilesDiv?.remove();
    _modFilesDiv = null;
  }
  function getAllFileDescriptionDds() {
    const modFilesDiv = getModFilesDiv();
    return modFilesDiv ? modFilesDiv.querySelectorAll("dl.accordion > dd") : null;
  }
  function getDownloadButtonsDiv(fileDescriptionDd) {
    return fileDescriptionDd.querySelector("div.tabbed-block:nth-of-type(2)");
  }
  function getDownloadButtonsUl(fileDescriptionDd) {
    return getDownloadButtonsDiv(fileDescriptionDd).querySelector("ul.accordion-downloads");
  }
  function getFileId(fileDescriptionDd) {
    return parseInt(fileDescriptionDd.getAttribute("data-id"));
  }

  // src/userscripts/util.ts
  var delay = function delay2(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
  };

  // src/userscripts/files_tab_actions.ts
  function insertDownloadNoWaitComponent(fileDescriptionDd) {
    const newLi = document.createElement("li");
    const newAnchor = document.createElement("a");
    newAnchor.className = "btn inline-flex";
    newAnchor.href = "#";
    newLi.appendChild(newAnchor);
    const newSvg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
    newSvg.setAttribute("class", "icon icon-manual");
    newSvg.innerHTML = `<use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-manual"></use>`;
    newAnchor.appendChild(newSvg);
    const newSpan = document.createElement("span");
    newSpan.innerText = "Download No Wait";
    newSpan.className = "flex-label";
    newSpan.style.textTransform = "none";
    newAnchor.appendChild(newSpan);
    newAnchor.addEventListener("click", async (event) => {
      event.preventDefault();
      const latestDownloadUrlAttr = "latest-download-url";
      const dateLastDownloadedAttr = "date-last-downloaded";
      let latestDownloadUrl = fileDescriptionDd.getAttribute(latestDownloadUrlAttr);
      const dateLastDownloaded = fileDescriptionDd.getAttribute(dateLastDownloadedAttr);
      if (!latestDownloadUrl || dateLastDownloaded && Date.now() - parseInt(dateLastDownloaded) > 60 * 60 * 1e3) {
        latestDownloadUrl = await generateDownloadUrl(getGameId(), getFileId(fileDescriptionDd));
        fileDescriptionDd.setAttribute(latestDownloadUrlAttr, latestDownloadUrl);
        fileDescriptionDd.setAttribute(dateLastDownloadedAttr, Date.now().toString());
      }
      window.open(latestDownloadUrl, "_self");
    });
    getDownloadButtonsUl(fileDescriptionDd).appendChild(newLi);
  }
  function insertDownloadNoWaitComponents() {
    async function _inner() {
      let fileDescriptionDds = getAllFileDescriptionDds();
      let count = 0;
      while (count <= 20 && !fileDescriptionDds) {
        await delay(50);
        fileDescriptionDds = getAllFileDescriptionDds();
        count++;
      }
      if (fileDescriptionDds) {
        for (const fileDescriptionDd of fileDescriptionDds) {
          insertDownloadNoWaitComponent(fileDescriptionDd);
        }
      }
    }
    getCurrentTab() === "files" && _inner();
    clickModTabs((newTab) => {
      clearElementCache();
      newTab === "files" && _inner();
    });
  }

  // src/userscript_shared.ts
  var isProduction = true;
  var author = "sylin527";
  function getAuthor() {
    return author;
  }

  // src/userscripts/download_no_wait/userscript.header.ts
  var name = `Download No Wait by ${getAuthor()}${isProduction ? "" : " Development Version"}`;
  var version = `0.1.0.23.1.25`;

  // src/userscripts/download_no_wait/userscript.main.ts
  function main() {
    insertDownloadNoWaitComponents();
    const scriptInfo = `Load userscript: ${name} ${version}`;
    console.log("%c [Info] " + scriptInfo, "color: green");
  }
  main();
})();