GMTI-Gazelle Music Tracker Importer

Just a simple script to import torrent metadata from other Gazelle sites when uploading. To use it you need to paste the permalink in the import field (on the upload page).

当前为 2019-03-21 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name    GMTI-Gazelle Music Tracker Importer
// @description    Just a simple script to import torrent metadata from other Gazelle sites when uploading. To use it you need to paste the permalink in the import field (on the upload page).
// @version  1.0.0
// @namespace      gmti
// @grant GM_xmlhttpRequest
// @include http*://*dicmusic.club/upload.php*
// @include http*://*orpheus.network/upload.php*

// ==/UserScript==

// Licensed under the Open Software License version 3.0
// Copy of the license can be found at: https://opensource.org/licenses/OSL-3.0

"use strict";

function decodeEntities(encodedString) {
  let textArea = document.createElement('textarea');
  textArea.innerHTML = encodedString;
  return textArea.value;
}

function parseHtml(html, ...selectors) {
  let formData = new FormData();
  formData.append("html", html);
  let options = {
    method: "POST",
    body: formData
  };

  let req = new Request("upload.php?action=parse_html", options);
  fetch(req).then(async (rsp) => {
    let text = await rsp.text();
    for (let selector of selectors) {
      let el = document.querySelector(selector);
      if (el !== null) {
        el.value = text;
      }
    }
  });
}

function applyJson(data) {
  let group = data['response']['group'];
  group["name"] = decodeEntities(group["name"]);
  group["recordLabel"] = decodeEntities(group["recordLabel"]);
  let torrent = data['response']['torrent'];
  let mapping = {
    title: 'name',
    year: 'year',
    image: 'wikiImage'
  };

  unsafeWindow.FillInFields(mapping, group);
  unsafeWindow.ParseMusicJson(group, torrent);

  if (group["tags"]) {
    document.querySelector("#tags").value = group["tags"].join(",");
  }

  if (group["wikiBody"]) {
    parseHtml(group["wikiBody"], "#desc", "#album_desc");

  }

  if (torrent["description"]) {
    parseHtml(torrent["description"], "#release_desc");
  }
}

function loadJson(url) {
  GM_xmlhttpRequest({
    method: "GET",
    url: url,
    onload: (response) => {
      let data = JSON.parse(response.responseText);
      console.log(data);
      applyJson(data);
    }
  });
}

function rewriteUrl(url) {
  return url.replace(/(.*)torrents.php\?torrentid=(\d+)/g, "$1ajax.php?action=torrent&id=$2");
}

let importRow = document.createElement("tr");

let textColumn = document.createElement("td");
let text = document.createTextNode("Import(其他PT站点PL链接):")
textColumn.append(text);
textColumn.classList.add("label");
importRow.append(textColumn);

let input = document.createElement("input");
let inputColumn = document.createElement("td");
let importButton = document.createElement("input");
input.setAttribute("id", "import_input");
input.setAttribute("type", "text");
input.setAttribute("size", "40");
importButton.setAttribute("type", "button");
importButton.setAttribute("value", "Import(导入)!");
inputColumn.append(input);
importRow.append(inputColumn);
inputColumn.append(importButton);

let musicbrainzRow = document.querySelector("#musicbrainz_tr");
musicbrainzRow.parentNode.insertBefore(importRow, musicbrainzRow);

importButton.addEventListener("click", (e) => {
  loadJson(rewriteUrl(input.value));
})