Paradb Download Button

Add download button to paradb song list. Sometimes the bulk download misses certain songs, use this button to prevent missing downloads.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Paradb Download Button
// @version      0.1
// @description  Add download button to paradb song list. Sometimes the bulk download misses certain songs, use this button to prevent missing downloads.
// @author       barryZZJ
// @namespace    https://github.com/barryZZJ
// @match        http*://paradb.net/*
// @icon         https://paradb.net/icon.png
// @run-at document-end
// @license      MIT
// ==/UserScript==

function copyClassList (sourceElem, targetElem) {
  let classList = sourceElem.classList;
  for (const cls of classList) {
    targetElem.classList.add(cls);
  }
}

function insertDownloadHeader (table) {
  let headerRow = table.querySelector('thead tr');
  if (!headerRow) return;
  let downloadHeader = document.createElement('th');
  downloadHeader.style = 'width:calc(8px * 10)';
  copyClassList(headerRow.querySelector('th'), downloadHeader);
  let downloadHeaderSpan = document.createElement('span');
  downloadHeaderSpan.textContent = '⭳';
  copyClassList(headerRow.querySelector('th span'), downloadHeaderSpan);

  downloadHeader.appendChild(downloadHeaderSpan);
  headerRow.appendChild(downloadHeader);
}

function insertDownloadButtons(table, row) {
  let mapId = row.querySelector('td a')?.getAttribute('href')?.split('/').pop();
  if (!mapId) return;
  let downloadCell = document.createElement('td');
  copyClassList(row.querySelector('td'), downloadCell);

  let downloadLink = document.createElement('a');
  downloadLink.href = `/api/maps/${mapId}/download`;
  copyClassList(row.querySelectorAll('td a')[1], downloadLink);
  downloadCell.appendChild(downloadLink);

  let downloadSpan = document.createElement('span');
  downloadSpan.textContent = '⭳';
  copyClassList(row.querySelector('td a span'), downloadSpan);
  downloadLink.appendChild(downloadSpan);

  row.appendChild(downloadCell);
}

(function () {
  'use strict';

  let table = document.querySelector('table');
  if (!table) return;

  let observer = new MutationObserver((mutationsList, observer) => {
    for (let mutation of mutationsList) {
      if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
        if (table.querySelector('tbody tr')) {
          if (table.querySelector('th:last-child')?.textContent !== '⭳') {
            insertDownloadHeader(table);
          }
          for (const row of mutation.addedNodes) {
            if (row.nodeType === Node.ELEMENT_NODE && row.tagName === 'TR') {
              if (row.querySelector('td:last-child a')?.getAttribute('href')?.includes('/download')) {
                continue; // Download button already exists
              }
              insertDownloadButtons(table, row);
            }
          }
        }
      }
    }

  });
  observer.observe(table, { childList: true, subtree: true });

})();