refactor: abstract class ScoreInfo

This commit is contained in:
Xmader 2020-11-24 02:29:37 -05:00
parent c334d8d124
commit 120d57b0e0
No known key found for this signature in database
GPG key ID: A20B97FB9EB730E4

View file

@ -3,33 +3,59 @@
const IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP' const IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP'
const RADIX = 20 const RADIX = 20
export const scoreinfo = { export abstract class ScoreInfo {
abstract id: number;
abstract title: string;
get id (this: typeof scoreinfo): number { abstract pageCount: number;
const el = document.querySelector("meta[property='al:ios:url']") as HTMLMetaElement abstract thumbnailUrl: string;
abstract sheetImgType: 'svg' | 'png'
get idLastDigit (): number {
return (+this.id) % RADIX
}
get fileName (): string {
return this.title.replace(/[\s<>:{}"/\\|?*~.\0\cA-\cZ]+/g, '_')
}
get msczIpfsRef (): string {
return `/ipns/${IPNS_KEY}/${this.idLastDigit}/${this.id}.mscz`
}
get msczCidUrl (): string {
return `https://ipfs.infura.io:5001/api/v0/block/stat?arg=${this.msczIpfsRef}`
}
}
export class ScoreInfoInPage extends ScoreInfo {
constructor (private document: Document) { super() }
get id (): number {
const el = this.document.querySelector("meta[property='al:ios:url']") as HTMLMetaElement
const m = el.content.match(/(\d+)$/) as RegExpMatchArray const m = el.content.match(/(\d+)$/) as RegExpMatchArray
return +m[1] return +m[1]
}, }
get title (this: typeof scoreinfo): string { get title (): string {
const el = document.querySelector("meta[property='og:title']") as HTMLMetaElement const el = this.document.querySelector("meta[property='og:title']") as HTMLMetaElement
return el.content return el.content
}, }
get pageCount (this: typeof scoreinfo): number { get pageCount (): number {
return document.querySelectorAll('.gXB83').length return this.document.querySelectorAll('.gXB83').length
}, }
get thumbnailUrl (this: typeof scoreinfo): string { get thumbnailUrl (): string {
// url to the image of the first page // url to the image of the first page
const el = document.querySelector("meta[property='og:image']") as HTMLMetaElement const el = this.document.querySelector("meta[property='og:image']") as HTMLMetaElement
const url = el.content const url = el.content
return url.split('@')[0] return url.split('@')[0]
}, }
get sheetImgType (): 'svg' | 'png' { get sheetImgType (): 'svg' | 'png' {
try { try {
const imgE = document.querySelector('img[src*=score_]') as HTMLImageElement const imgE = this.document.querySelector('img[src*=score_]') as HTMLImageElement
const { pathname } = new URL(imgE.src) const { pathname } = new URL(imgE.src)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const imgtype = pathname.match(/\.(\w+)$/)![1] const imgtype = pathname.match(/\.(\w+)$/)![1]
@ -38,23 +64,9 @@ export const scoreinfo = {
// return null // return null
return 'svg' return 'svg'
} }
}, }
get idLastDigit (this: typeof scoreinfo): number {
return (+this.id) % RADIX
},
get fileName (this: typeof scoreinfo): string {
return this.title.replace(/[\s<>:{}"/\\|?*~.\0\cA-\cZ]+/g, '_')
},
get msczIpfsRef (this: typeof scoreinfo): string {
return `/ipns/${IPNS_KEY}/${this.idLastDigit}/${this.id}.mscz`
},
get msczCidUrl (this: typeof scoreinfo): string {
return `https://ipfs.infura.io:5001/api/v0/block/stat?arg=${this.msczIpfsRef}`
},
} }
export const scoreinfo = new ScoreInfoInPage(document)
export default scoreinfo export default scoreinfo