musescore-downloader

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 8f149341445afe2a7b42a163466ed109c9b164c9
parent 1491cac295dfba2a83009ada29653fd802b28525
Author: Xmader <xmader@outlook.com>
Date:   Fri, 29 Nov 2019 18:07:43 -0500

修复无法使用的问题

Diffstat:
Msrc/main.ts | 88++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/utils.ts | 14++++++++++++++
2 files changed, 61 insertions(+), 41 deletions(-)

diff --git a/src/main.ts b/src/main.ts @@ -1,52 +1,58 @@ import "./meta" import { ScorePlayerData } from "./types" -import { getIndexPath } from "./utils" - -// @ts-ignore -const scorePlayer: ScorePlayerData = 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") as HTMLElement -downloadBtn.onclick = null - -const downloadURLs = { - "Musescore": msczURL, - "PDF": pdfURL, - "MusicXML": mxlURL, - "MIDI": midiURL, - "MP3": mp3URL, -} +import { getIndexPath, waitForDocumentLoaded } from "./utils" -const newDownloadBtns = Object.keys(downloadURLs).map((name) => { - const url = downloadURLs[name] +const main = () => { - const btn = downloadBtn.cloneNode(true) as HTMLElement - btn.onclick = () => { - window.open(url) - } + // @ts-ignore + const scorePlayer: ScorePlayerData = 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] - if (btn.nodeName.toLowerCase() == "button") { - btn.setAttribute("style", "width: 205px !important") - } else { - btn.dataset.target = "" + 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") as HTMLElement + downloadBtn.onclick = null + + const downloadURLs = { + "Musescore": msczURL, + "PDF": pdfURL, + "MusicXML": mxlURL, + "MIDI": midiURL, + "MP3": mp3URL, } - const span = [...btn.childNodes].find((x) => { - return x.textContent.includes("Download") + const newDownloadBtns = Object.keys(downloadURLs).map((name) => { + const url = downloadURLs[name] + + const btn = downloadBtn.cloneNode(true) as HTMLElement + 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 }) - span.textContent = `Download ${name}` - return btn -}) + downloadBtn.replaceWith(...newDownloadBtns) + +} -downloadBtn.replaceWith(...newDownloadBtns) +waitForDocumentLoaded().then(main) diff --git a/src/utils.ts b/src/utils.ts @@ -8,3 +8,17 @@ export const getIndexPath = (id: number) => { const indexN = idStr.split("").reverse().slice(0, 3) return indexN.join("/") } + +export const waitForDocumentLoaded = (): Promise<void> => { + if (document.readyState !== "complete") { + return new Promise(resolve => { + document.addEventListener("readystatechange", () => { + if (document.readyState == "complete") { + resolve() + } + }, { once: true }) + }) + } else { + return Promise.resolve() + } +}