修复无法使用的问题

This commit is contained in:
Xmader 2019-11-29 18:07:43 -05:00
parent 1491cac295
commit 8f14934144
2 changed files with 62 additions and 42 deletions

View File

@ -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)

View File

@ -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()
}
}