From aafb71fc4b33b41e3e3a1319a9c722e112a461b9 Mon Sep 17 00:00:00 2001 From: Xmader Date: Tue, 24 Nov 2020 02:31:31 -0500 Subject: [PATCH] refactor: implement `sheetImgType` in abstract class --- src/scoreinfo.ts | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/scoreinfo.ts b/src/scoreinfo.ts index a8e74f6..16c6b48 100644 --- a/src/scoreinfo.ts +++ b/src/scoreinfo.ts @@ -9,7 +9,6 @@ export abstract class ScoreInfo { abstract pageCount: number; abstract thumbnailUrl: string; - abstract sheetImgType: 'svg' | 'png' get idLastDigit (): number { return (+this.id) % RADIX @@ -26,6 +25,12 @@ export abstract class ScoreInfo { get msczCidUrl (): string { return `https://ipfs.infura.io:5001/api/v0/block/stat?arg=${this.msczIpfsRef}` } + + get sheetImgType (): 'svg' | 'png' { + const thumbnail = this.thumbnailUrl + const imgtype = thumbnail.match(/\.(\w+)$/)![1] + return imgtype as 'svg' | 'png' + } } export class ScoreInfoInPage extends ScoreInfo { @@ -48,23 +53,10 @@ export class ScoreInfoInPage extends ScoreInfo { get thumbnailUrl (): string { // url to the image of the first page - const el = this.document.querySelector("meta[property='og:image']") as HTMLMetaElement - const url = el.content + const el = this.document.querySelector('link[as=image]') as HTMLLinkElement + const url = el.href return url.split('@')[0] } - - get sheetImgType (): 'svg' | 'png' { - try { - const imgE = this.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 const scoreinfo = new ScoreInfoInPage(document)