From 8f149341445afe2a7b42a163466ed109c9b164c9 Mon Sep 17 00:00:00 2001 From: Xmader Date: Fri, 29 Nov 2019 18:07:43 -0500 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 90 ++++++++++++++++++++++++++++------------------------ src/utils.ts | 14 ++++++++ 2 files changed, 62 insertions(+), 42 deletions(-) diff --git a/src/main.ts b/src/main.ts index a543be9..bbdf562 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,52 +1,58 @@ import "./meta" import { ScorePlayerData } from "./types" -import { getIndexPath } from "./utils" +import { getIndexPath, waitForDocumentLoaded } from "./utils" -// @ts-ignore -const scorePlayer: ScorePlayerData = window.UGAPP.store.jmuse_settings.score_player +const main = () => { -const { id, vid } = scorePlayer.json -const baseURL = scorePlayer.urls.image_path -const scoreHexId = baseURL.split("/").filter(Boolean).reverse()[0] + // @ts-ignore + const scorePlayer: ScorePlayerData = window.UGAPP.store.jmuse_settings.score_player -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 { id, vid } = scorePlayer.json + const baseURL = scorePlayer.urls.image_path + const scoreHexId = baseURL.split("/").filter(Boolean).reverse()[0] -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 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 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 + }) + + downloadBtn.replaceWith(...newDownloadBtns) -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) 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 -}) - -downloadBtn.replaceWith(...newDownloadBtns) +waitForDocumentLoaded().then(main) diff --git a/src/utils.ts b/src/utils.ts index 2a07841..8282a62 100644 --- 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 => { + if (document.readyState !== "complete") { + return new Promise(resolve => { + document.addEventListener("readystatechange", () => { + if (document.readyState == "complete") { + resolve() + } + }, { once: true }) + }) + } else { + return Promise.resolve() + } +}