2020-05-17 22:57:28 +00:00
|
|
|
import './meta'
|
2019-11-03 19:13:06 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
import { ScorePlayerData } from './types'
|
|
|
|
import { waitForDocumentLoaded } from './utils'
|
|
|
|
import * as recaptcha from './recaptcha'
|
2019-11-03 19:13:06 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
import { PDFWorkerHelper } from './worker-helper'
|
|
|
|
import FileSaver from 'file-saver/dist/FileSaver.js'
|
2019-12-01 23:30:38 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const saveAs: typeof import('file-saver').saveAs = FileSaver.saveAs
|
2019-12-01 09:11:40 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const PROCESSING_TEXT = 'Processing…'
|
|
|
|
const FAILED_TEXT = '❌Download Failed!'
|
|
|
|
const WEBMSCORE_URL = 'https://cdn.jsdelivr.net/npm/webmscore@0.5/webmscore.js'
|
2020-05-13 10:29:11 +00:00
|
|
|
|
2019-12-01 09:11:40 +00:00
|
|
|
let pdfBlob: Blob
|
2020-05-17 22:57:28 +00:00
|
|
|
let msczBufferP: Promise<ArrayBuffer> | undefined
|
2019-12-01 07:10:51 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const generatePDF = async (imgURLs: string[], imgType: 'svg' | 'png', name?: string): Promise<void> => {
|
|
|
|
if (pdfBlob) {
|
|
|
|
return saveAs(pdfBlob, `${name}.pdf`)
|
|
|
|
}
|
2019-12-01 09:38:26 +00:00
|
|
|
|
2020-05-18 20:33:15 +00:00
|
|
|
const cachedImg = document.querySelector('img[src*=score_]') as HTMLImageElement
|
2020-05-17 22:57:28 +00:00
|
|
|
const { naturalWidth: width, naturalHeight: height } = cachedImg
|
2019-12-01 09:38:26 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const worker = new PDFWorkerHelper()
|
|
|
|
const pdfArrayBuffer = await worker.generatePDF(imgURLs, imgType, width, height)
|
|
|
|
worker.terminate()
|
2019-12-01 07:10:51 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
pdfBlob = new Blob([pdfArrayBuffer])
|
2019-12-01 09:11:40 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
saveAs(pdfBlob, `${name}.pdf`)
|
2019-12-01 07:10:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const getPagesNumber = (scorePlayerData: ScorePlayerData): number => {
|
|
|
|
try {
|
|
|
|
return scorePlayerData.json.metadata.pages
|
|
|
|
} catch (_) {
|
|
|
|
return document.querySelectorAll('img[src*=score_]').length
|
|
|
|
}
|
2019-12-01 09:38:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 20:33:15 +00:00
|
|
|
const getImgType = (): 'svg' | 'png' | null => {
|
2020-05-17 22:57:28 +00:00
|
|
|
try {
|
2020-05-18 20:33:15 +00:00
|
|
|
const imgE = document.querySelector('img[src*=score_]') as HTMLImageElement
|
2020-05-17 22:57:28 +00:00
|
|
|
const { pathname } = new URL(imgE.src)
|
2020-05-18 20:33:15 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
const imgtype = pathname.match(/\.(\w+)$/)![1]
|
2020-05-17 22:57:28 +00:00
|
|
|
return imgtype as 'svg' | 'png'
|
|
|
|
} catch (_) {
|
|
|
|
return null
|
|
|
|
}
|
2019-12-01 23:30:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const getTitle = (scorePlayerData: ScorePlayerData): string => {
|
|
|
|
try {
|
|
|
|
return scorePlayerData.json.metadata.title
|
|
|
|
} catch (_) {
|
|
|
|
return ''
|
|
|
|
}
|
2019-12-01 07:10:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const getScoreFileName = (scorePlayerData: ScorePlayerData): string => {
|
|
|
|
return getTitle(scorePlayerData).replace(/[\s<>:{}"/\\|?*~.\0\cA-\cZ]+/g, '_')
|
2019-12-01 07:10:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 10:29:11 +00:00
|
|
|
const fetchMscz = async (url: string): Promise<ArrayBuffer> => {
|
2020-05-17 22:57:28 +00:00
|
|
|
if (!msczBufferP) {
|
|
|
|
msczBufferP = (async (): Promise<ArrayBuffer> => {
|
|
|
|
const token = await recaptcha.execute()
|
|
|
|
const r = await fetch(url + token)
|
|
|
|
const data = await r.arrayBuffer()
|
|
|
|
return data
|
|
|
|
})()
|
|
|
|
}
|
|
|
|
|
|
|
|
return msczBufferP
|
2020-05-13 10:29:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const main = (): void => {
|
|
|
|
// @ts-ignore
|
|
|
|
if (!window.UGAPP || !window.UGAPP.store || !window.UGAPP.store.jmuse_settings) { return }
|
2019-11-03 19:13:06 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
// init recaptcha
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
|
|
recaptcha.init()
|
2019-12-01 05:13:14 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
// @ts-ignore
|
|
|
|
const scorePlayer: ScorePlayerData = window.UGAPP.store.jmuse_settings.score_player
|
2020-03-08 21:36:37 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const { id } = scorePlayer.json
|
|
|
|
const baseURL = scorePlayer.urls.image_path
|
2019-11-29 23:07:43 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const filename = getScoreFileName(scorePlayer)
|
2019-11-03 19:13:06 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
// https://github.com/Xmader/cloudflare-worker-musescore-mscz
|
|
|
|
const msczURL = `https://musescore.now.sh/api/mscz?id=${id}&token=`
|
2019-12-01 05:16:07 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const mxlURL = baseURL + 'score.mxl'
|
|
|
|
const { midi: midiURL, mp3: mp3URL } = scorePlayer.urls
|
2019-12-01 05:16:07 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const btnsDiv = document.querySelector('.score-right .buttons-wrapper') || document.querySelectorAll('aside section > div')[4]
|
|
|
|
const downloadBtn = btnsDiv.querySelector('button, .button') as HTMLElement
|
|
|
|
downloadBtn.onclick = null
|
2019-11-29 23:07:43 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
// fix the icon of the download btn
|
|
|
|
// if the `downloadBtn` seleted was a `Print` btn, replace the `print` icon with the `download` icon
|
2020-05-18 20:33:15 +00:00
|
|
|
const svgPath: SVGPathElement | null = downloadBtn.querySelector('svg > path')
|
2020-05-17 22:57:28 +00:00
|
|
|
if (svgPath) {
|
|
|
|
svgPath.setAttribute('d', 'M9.6 2.4h4.8V12h2.784l-5.18 5.18L6.823 12H9.6V2.4zM19.2 19.2H4.8v2.4h14.4v-2.4z')
|
|
|
|
}
|
2019-11-29 23:07:43 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const imgType = getImgType() || 'svg'
|
|
|
|
|
|
|
|
const sheetImgURLs = Array.from({ length: getPagesNumber(scorePlayer) }).fill(null).map((_, i) => {
|
|
|
|
return baseURL + `score_${i}.${imgType}`
|
|
|
|
})
|
|
|
|
|
|
|
|
const downloadURLs = {
|
|
|
|
MSCZ: null,
|
|
|
|
PDF: null,
|
|
|
|
MusicXML: mxlURL,
|
|
|
|
MIDI: midiURL,
|
|
|
|
MP3: mp3URL,
|
|
|
|
Parts: null,
|
|
|
|
}
|
2020-03-29 21:31:33 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
|
|
const createBtn = (name: string) => {
|
|
|
|
const btn: HTMLButtonElement = downloadBtn.cloneNode(true) as any
|
|
|
|
|
|
|
|
if (btn.nodeName.toLowerCase() === 'button') {
|
|
|
|
btn.setAttribute('style', 'width: 205px !important')
|
|
|
|
} else {
|
|
|
|
btn.dataset.target = ''
|
|
|
|
}
|
2019-12-01 23:30:38 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const textNode = [...btn.childNodes].find((x) => {
|
2020-05-18 20:33:15 +00:00
|
|
|
const txt = x.textContent as string
|
|
|
|
return txt.includes('Download') || txt.includes('Print')
|
|
|
|
}) as Node
|
2020-05-17 22:57:28 +00:00
|
|
|
textNode.textContent = `Download ${name}`
|
2019-12-01 09:38:26 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
return {
|
|
|
|
btn,
|
|
|
|
textNode,
|
2019-11-03 20:21:59 +00:00
|
|
|
}
|
2020-05-17 22:57:28 +00:00
|
|
|
}
|
2019-11-03 20:12:15 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const newDownloadBtns = Object.keys(downloadURLs).map((name) => {
|
|
|
|
const url = downloadURLs[name]
|
|
|
|
const { btn, textNode } = createBtn(name)
|
2019-11-29 23:07:43 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
if (name === 'PDF') {
|
|
|
|
btn.onclick = async (): Promise<void> => {
|
|
|
|
const filename = getScoreFileName(scorePlayer)
|
2019-11-29 23:07:43 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
textNode.textContent = PROCESSING_TEXT
|
2019-12-01 07:10:51 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
try {
|
|
|
|
await generatePDF(sheetImgURLs, imgType, filename)
|
|
|
|
textNode.textContent = 'Download PDF'
|
|
|
|
} catch (err) {
|
|
|
|
textNode.textContent = FAILED_TEXT
|
|
|
|
console.error(err)
|
2019-12-01 07:10:51 +00:00
|
|
|
}
|
2020-05-17 22:57:28 +00:00
|
|
|
}
|
|
|
|
} else if (name === 'MSCZ') {
|
|
|
|
btn.onclick = async (): Promise<void> => {
|
|
|
|
textNode.textContent = PROCESSING_TEXT
|
|
|
|
|
|
|
|
try {
|
|
|
|
const data = new Blob([await fetchMscz(msczURL)])
|
|
|
|
textNode.textContent = 'Download MSCZ'
|
|
|
|
saveAs(data, `${filename}.mscz`)
|
|
|
|
} catch (err) {
|
|
|
|
textNode.textContent = FAILED_TEXT
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (name === 'Parts') { // download individual parts
|
|
|
|
btn.title = 'Download individual parts (BETA)'
|
|
|
|
const cb = btn.onclick = async (): Promise<void> => {
|
|
|
|
btn.onclick = null
|
|
|
|
textNode.textContent = PROCESSING_TEXT
|
|
|
|
|
2020-05-18 20:33:15 +00:00
|
|
|
const w = window.open('') as Window
|
2020-05-17 22:57:28 +00:00
|
|
|
const txt = document.createTextNode(PROCESSING_TEXT)
|
|
|
|
w.document.body.append(txt)
|
|
|
|
|
|
|
|
// set page hooks
|
|
|
|
// eslint-disable-next-line prefer-const
|
|
|
|
let score: any
|
|
|
|
const destroy = (): void => {
|
|
|
|
score.destroy()
|
|
|
|
w.close()
|
|
|
|
}
|
|
|
|
window.addEventListener('unload', destroy)
|
|
|
|
w.addEventListener('beforeunload', () => {
|
|
|
|
score.destroy()
|
|
|
|
window.removeEventListener('unload', destroy)
|
|
|
|
textNode.textContent = 'Download Parts'
|
|
|
|
btn.onclick = cb
|
|
|
|
})
|
2019-12-01 07:10:51 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
// load webmscore (https://github.com/LibreScore/webmscore)
|
|
|
|
const script = w.document.createElement('script')
|
|
|
|
script.src = WEBMSCORE_URL
|
|
|
|
w.document.body.append(script)
|
|
|
|
await new Promise(resolve => { script.onload = resolve })
|
|
|
|
|
|
|
|
// parse mscz data
|
|
|
|
const data = new Uint8Array(
|
|
|
|
new Uint8Array(await fetchMscz(msczURL)) // copy its ArrayBuffer
|
|
|
|
)
|
|
|
|
score = await w['WebMscore'].load('mscz', data)
|
|
|
|
await score.generateExcerpts()
|
|
|
|
const metadata = await score.metadata()
|
|
|
|
console.log('score metadata loaded by webmscore', metadata)
|
|
|
|
|
|
|
|
// render the part selection page
|
|
|
|
txt.remove()
|
|
|
|
const fieldset = w.document.createElement('fieldset')
|
|
|
|
for (const excerpt of metadata.excerpts) {
|
|
|
|
const e = w.document.createElement('input')
|
|
|
|
e.name = 'score-part'
|
|
|
|
e.type = 'radio'
|
|
|
|
e.value = excerpt.id
|
|
|
|
const label = w.document.createElement('label')
|
|
|
|
label.innerText = excerpt.title
|
|
|
|
const br = w.document.createElement('br')
|
|
|
|
fieldset.append(e, label, br)
|
2019-12-01 07:10:51 +00:00
|
|
|
}
|
2020-05-17 22:57:28 +00:00
|
|
|
const submitBtn = w.document.createElement('input')
|
|
|
|
submitBtn.type = 'submit'
|
|
|
|
submitBtn.value = 'Download PDF'
|
|
|
|
fieldset.append(submitBtn)
|
|
|
|
w.document.body.append(fieldset)
|
2019-11-29 23:07:43 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
submitBtn.onclick = async (): Promise<void> => {
|
2020-05-18 20:33:15 +00:00
|
|
|
const checked = fieldset.querySelector('input:checked') as HTMLInputElement
|
2020-05-17 22:57:28 +00:00
|
|
|
const id = checked.value
|
|
|
|
|
|
|
|
await score.setExcerptId(id)
|
|
|
|
|
|
|
|
const data = new Blob([await score.savePdf()])
|
|
|
|
saveAs(data, `${filename}-part-${id}.pdf`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
btn.onclick = (): void => {
|
|
|
|
window.open(url)
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 20:01:29 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
return btn
|
|
|
|
})
|
2019-11-29 23:07:43 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
downloadBtn.replaceWith(...newDownloadBtns)
|
2019-11-29 23:07:43 +00:00
|
|
|
}
|
2019-11-03 19:13:06 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2019-11-29 23:07:43 +00:00
|
|
|
waitForDocumentLoaded().then(main)
|