musescore-downloader/src/scoreinfo.ts

72 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-10-01 15:55:38 +00:00
/* eslint-disable @typescript-eslint/no-unsafe-return */
2020-05-18 22:44:45 +00:00
const scoreinfo = {
2020-10-01 15:55:38 +00:00
get playerdata (): any {
2020-05-18 22:44:45 +00:00
// @ts-ignore
2020-10-01 15:55:38 +00:00
return window.UGAPP.store.page.data.score
2020-05-18 22:44:45 +00:00
},
get id (this: typeof scoreinfo): number {
2020-10-01 15:55:38 +00:00
return this.playerdata.id
2020-05-18 22:44:45 +00:00
},
get title (this: typeof scoreinfo): string {
try {
2020-10-01 15:55:38 +00:00
return this.playerdata.title
2020-05-18 22:44:45 +00:00
} catch (_) {
return ''
}
},
get fileName (this: typeof scoreinfo): string {
return this.title.replace(/[\s<>:{}"/\\|?*~.\0\cA-\cZ]+/g, '_')
},
get pageCount (this: typeof scoreinfo): number {
try {
2020-10-01 15:55:38 +00:00
return this.playerdata.pages_count
2020-05-18 22:44:45 +00:00
} catch (_) {
return document.querySelectorAll('img[src*=score_]').length
}
},
get baseUrl (this: typeof scoreinfo): string {
2020-10-01 15:55:38 +00:00
const thumbnailUrl = this.playerdata.thumbnails.original
const { origin, pathname } = new URL(thumbnailUrl)
return origin + pathname.split('/').slice(0, -1).join('/') + '/'
2020-05-18 22:44:45 +00:00
},
get mxlUrl (this: typeof scoreinfo): string {
return this.baseUrl + 'score.mxl'
},
get midiUrl (this: typeof scoreinfo): string {
2020-10-01 15:55:38 +00:00
return this.baseUrl + 'score.mid'
2020-05-18 22:44:45 +00:00
},
get mp3Url (this: typeof scoreinfo): string {
2020-10-01 15:55:38 +00:00
return this.baseUrl + 'score.mp3'
2020-05-18 22:44:45 +00:00
},
get msczUrl (this: typeof scoreinfo): string {
// https://github.com/Xmader/cloudflare-worker-musescore-mscz
2020-09-27 15:31:11 +00:00
return `https://musescore.now.sh/api/mscz?id=${this.id}&token=`
2020-05-18 22:44:45 +00:00
},
get sheetImgType (): 'svg' | 'png' {
try {
const imgE = document.querySelector('img[src*=score_]') as HTMLImageElement
const { pathname } = new URL(imgE.src)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const imgtype = pathname.match(/\.(\w+)$/)![1]
return imgtype as 'svg' | 'png'
} catch (_) {
// return null
return 'svg'
}
},
}
export default scoreinfo