musescore-downloader/dist/main.js

89 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-11-03 19:13:06 +00:00
// ==UserScript==
// @name musescore-downloader
// @namespace https://www.xmader.com/
2019-11-03 19:27:47 +00:00
// @homepageURL https://github.com/Xmader/musescore-downloader/
// @supportURL https://github.com/Xmader/musescore-downloader/issues
2019-11-29 23:09:03 +00:00
// @version 0.1.7
2019-11-03 19:13:06 +00:00
// @description 免登录、免 Musescore Pro下载 musescore.com 上的曲谱
// @author Xmader
2019-11-04 06:32:16 +00:00
// @match https://musescore.com/*/*
2019-11-03 19:13:06 +00:00
// @license MIT
// @copyright Copyright (c) 2019 Xmader
// @grant none
// ==/UserScript==
(function () {
'use strict';
const getIndexPath = (id) => {
const idStr = String(id);
// 获取最后三位,倒序排列
2019-11-29 23:09:03 +00:00
// x, y, z are the reversed last digits of the score id. Example: id 123456789, x = 9, y = 8, z = 7
// https://developers.musescore.com/#/file-urls
2019-11-03 19:13:06 +00:00
// "5449062" -> ["2", "6", "0"]
const indexN = idStr.split("").reverse().slice(0, 3);
return indexN.join("/");
};
2019-11-29 23:09:03 +00:00
const waitForDocumentLoaded = () => {
if (document.readyState !== "complete") {
return new Promise(resolve => {
document.addEventListener("readystatechange", () => {
if (document.readyState == "complete") {
resolve();
}
}, { once: true });
});
2019-11-03 20:21:59 +00:00
}
2019-11-03 20:24:58 +00:00
else {
2019-11-29 23:09:03 +00:00
return Promise.resolve();
2019-11-03 20:24:58 +00:00
}
2019-11-29 23:09:03 +00:00
};
const main = () => {
2019-12-01 05:13:14 +00:00
// @ts-ignore
if (!window.UGAPP || !window.UGAPP.store || !window.UGAPP.store.jmuse_settings) {
return;
}
2019-11-29 23:09:03 +00:00
// @ts-ignore
const scorePlayer = window.UGAPP.store.jmuse_settings.score_player;
const { id, vid } = scorePlayer.json;
const baseURL = scorePlayer.urls.image_path;
const scoreHexId = baseURL.split("/").filter(Boolean).reverse()[0];
const msczURL = `https://musescore.com/static/musescore/scoredata/score/${getIndexPath(id)}/${id}/score_${vid}_${scoreHexId}.mscz`;
const pdfURL = baseURL + "score_full.pdf";
const mxlURL = baseURL + "score.mxl";
const { midi: midiURL, mp3: mp3URL } = scorePlayer.urls;
const btnsDiv = document.querySelector(".score-right .buttons-wrapper") || document.querySelectorAll("aside section > div")[3];
const downloadBtn = btnsDiv.querySelector("button, .button");
downloadBtn.onclick = null;
const downloadURLs = {
"Musescore": msczURL,
"PDF": pdfURL,
"MusicXML": mxlURL,
"MIDI": midiURL,
"MP3": mp3URL,
};
const newDownloadBtns = Object.keys(downloadURLs).map((name) => {
const url = downloadURLs[name];
const btn = downloadBtn.cloneNode(true);
btn.onclick = () => {
window.open(url);
};
if (btn.nodeName.toLowerCase() == "button") {
btn.setAttribute("style", "width: 205px !important");
}
else {
btn.dataset.target = "";
}
const span = [...btn.childNodes].find((x) => {
return x.textContent.includes("Download");
});
span.textContent = `Download ${name}`;
return btn;
2019-11-03 20:21:59 +00:00
});
2019-11-29 23:09:03 +00:00
downloadBtn.replaceWith(...newDownloadBtns);
};
waitForDocumentLoaded().then(main);
2019-11-03 19:13:06 +00:00
}());