musescore-downloader/src/scoreinfo.ts

99 lines
2.7 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
2020-10-23 14:11:52 +00:00
// run at document-start
2020-10-23 15:00:39 +00:00
export const ugappJsStore: Record<string, any> | null = (() => {
try {
2020-10-26 19:22:54 +00:00
const l = document.body.children as HTMLCollectionOf<HTMLElement>
const el = [...l].find(e => Object.keys(e.dataset).length > 0) as HTMLDivElement
const json = Object.values(el.dataset)[0] as string
2020-10-23 15:00:39 +00:00
return JSON.parse(json)
} catch (err) {
console.error(err)
return null
}
2020-10-23 14:11:52 +00:00
})()
const IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP'
2020-11-14 22:44:25 +00:00
const RADIX = 20
2020-10-23 14:11:52 +00:00
export const scoreinfo = {
2020-05-18 22:44:45 +00:00
2020-10-01 15:55:38 +00:00
get playerdata (): any {
2020-05-18 22:44:45 +00:00
// @ts-ignore
2020-10-23 14:11:52 +00:00
return ugappJsStore.store.page.data.score
2020-05-18 22:44:45 +00:00
},
get id (this: typeof scoreinfo): number {
2020-10-23 15:00:39 +00:00
try {
return this.playerdata.id
} catch {
const el = document.querySelector("meta[property='al:ios:url']") as HTMLMetaElement
const m = el.content.match(/(\d+)$/) as RegExpMatchArray
return +m[1]
}
2020-05-18 22:44:45 +00:00
},
2020-11-14 22:44:25 +00:00
get idLastDigit (this: typeof scoreinfo): number {
const idStr = (+this.id).toString(RADIX)
return parseInt(idStr[idStr.length - 1], RADIX)
},
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-10-23 15:00:39 +00:00
} catch {
const el = document.querySelector("meta[property='og:title']") as HTMLMetaElement
return el.content
2020-05-18 22:44:45 +00:00
}
},
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-10-23 15:00:39 +00:00
} catch {
return document.querySelectorAll('.gXB83').length
2020-05-18 22:44:45 +00:00
}
},
get baseUrl (this: typeof scoreinfo): string {
2020-10-23 15:00:39 +00:00
let thumbnailUrl: string
try {
thumbnailUrl = this.playerdata.thumbnails.original
} catch {
const el = document.querySelector("meta[property='og:image']") as HTMLMetaElement
thumbnailUrl = el.content
}
2020-10-01 15:55:38 +00:00
const { origin, pathname } = new URL(thumbnailUrl)
2020-10-18 07:29:37 +00:00
// remove the last part
2020-10-01 15:55:38 +00:00
return origin + pathname.split('/').slice(0, -1).join('/') + '/'
2020-05-18 22:44:45 +00:00
},
get msczIpfsRef (this: typeof scoreinfo): string {
2020-11-14 22:44:25 +00:00
return `/ipns/${IPNS_KEY}/${this.idLastDigit}/${this.id}.mscz`
},
2020-11-14 18:37:46 +00:00
get msczCidUrl (this: typeof scoreinfo): string {
return `https://ipfs.infura.io:5001/api/v0/dag/resolve?arg=${this.msczIpfsRef}`
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