diff --git a/src/file.ts b/src/file.ts index 4ef738a..fb92b16 100644 --- a/src/file.ts +++ b/src/file.ts @@ -1,7 +1,6 @@ /* eslint-disable no-extend-native */ /* eslint-disable @typescript-eslint/no-unsafe-return */ -import scoreinfo from './scoreinfo' import { ALL, webpackGlobalOverride } from './webpack-hook' import { console } from './utils' @@ -53,8 +52,8 @@ const magics: Record> = { mp3: magicHookConstr('mp3'), } -const getApiUrl = (type: FileType, index: number): string => { - return `/api/jmuse?id=${scoreinfo.id}&type=${type}&index=${index}&v2=1` +const getApiUrl = (id: number, type: FileType, index: number): string => { + return `/api/jmuse?id=${id}&type=${type}&index=${index}&v2=1` } const getApiAuth = async (type: FileType, index: number): Promise => { @@ -87,8 +86,8 @@ const getApiAuth = async (type: FileType, index: number): Promise => { return magic } -export const getFileUrl = async (type: FileType, index = 0): Promise => { - const url = getApiUrl(type, index) +export const getFileUrl = async (id: number, type: FileType, index = 0): Promise => { + const url = getApiUrl(id, type, index) const auth = await getApiAuth(type, index) const r = await fetch(url, { diff --git a/src/main.ts b/src/main.ts index 012e5d8..db87a74 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,12 +6,13 @@ import { downloadMscz } from './mscz' import { getFileUrl } from './file' import { WebMscore, loadSoundFont } from './mscore' import { BtnList, BtnAction, BtnListMode } from './btn' -import scoreinfo from './scoreinfo' +import { ScoreInfoInPage } from './scoreinfo' import i18n from './i18n' const main = (): void => { const btnList = new BtnList() - const filename = scoreinfo.fileName + const scoreinfo = new ScoreInfoInPage(document) + const { fileName, id } = scoreinfo let indvPartBtn: HTMLButtonElement | null = null const fallback = () => { @@ -21,12 +22,12 @@ const main = (): void => { btnList.add({ name: i18n('DOWNLOAD')('MSCZ'), - action: BtnAction.process(downloadMscz), + action: BtnAction.process(() => downloadMscz(scoreinfo)), }) btnList.add({ name: i18n('DOWNLOAD')('PDF'), - action: BtnAction.process(downloadPDF, fallback, 3 * 60 * 1000 /* 3min */), + action: BtnAction.process(() => downloadPDF(scoreinfo), fallback, 3 * 60 * 1000 /* 3min */), }) btnList.add({ @@ -34,19 +35,19 @@ const main = (): void => { action: BtnAction.mscoreWindow(async (w, score) => { const mxl = await score.saveMxl() const data = new Blob([mxl]) - saveAs(data, `${filename}.mxl`) + saveAs(data, `${fileName}.mxl`) w.close() }), }) btnList.add({ name: i18n('DOWNLOAD')('MIDI'), - action: BtnAction.download(() => getFileUrl('midi'), fallback, 30 * 1000 /* 30s */), + action: BtnAction.download(() => getFileUrl(id, 'midi'), fallback, 30 * 1000 /* 30s */), }) btnList.add({ name: i18n('DOWNLOAD')('MP3'), - action: BtnAction.download(() => getFileUrl('mp3'), fallback, 30 * 1000 /* 30s */), + action: BtnAction.download(() => getFileUrl(id, 'mp3'), fallback, 30 * 1000 /* 30s */), }) indvPartBtn = btnList.add({ @@ -150,7 +151,7 @@ const main = (): void => { const partName = checked.alt const data = new Blob([await d.action(score)]) - saveAs(data, `${filename} - ${partName}.${d.fileExt}`) + saveAs(data, `${fileName} - ${partName}.${d.fileExt}`) // unlock button initBtn() diff --git a/src/mscz.ts b/src/mscz.ts index 1f971b3..999cba2 100644 --- a/src/mscz.ts +++ b/src/mscz.ts @@ -1,10 +1,10 @@ import { saveAs, assertRes } from './utils' -import scoreinfo from './scoreinfo' +import { ScoreInfo } from './scoreinfo' let msczBufferP: Promise | undefined -export const fetchMscz = async (): Promise => { +export const fetchMscz = async (scoreinfo: ScoreInfo): Promise => { if (!msczBufferP) { const url = scoreinfo.msczCidUrl msczBufferP = (async (): Promise => { @@ -21,8 +21,8 @@ export const fetchMscz = async (): Promise => { return msczBufferP } -export const downloadMscz = async (): Promise => { - const data = new Blob([await fetchMscz()]) +export const downloadMscz = async (scoreinfo: ScoreInfo): Promise => { + const data = new Blob([await fetchMscz(scoreinfo)]) const filename = scoreinfo.fileName saveAs(data, `${filename}.mscz`) } diff --git a/src/pdf.ts b/src/pdf.ts index ffa7830..56c86d2 100644 --- a/src/pdf.ts +++ b/src/pdf.ts @@ -2,7 +2,7 @@ import { PDFWorkerHelper } from './worker-helper' import { getFileUrl } from './file' import { saveAs } from './utils' -import scoreinfo from './scoreinfo' +import { ScoreInfo } from './scoreinfo' let pdfBlob: Blob @@ -23,7 +23,7 @@ const _downloadPDF = async (imgURLs: string[], imgType: 'svg' | 'png', name = '' saveAs(pdfBlob, `${name}.pdf`) } -export const downloadPDF = async (): Promise => { +export const downloadPDF = async (scoreinfo: ScoreInfo): Promise => { const imgType = scoreinfo.sheetImgType const pageCount = scoreinfo.pageCount @@ -31,7 +31,7 @@ export const downloadPDF = async (): Promise => { if (i === 0) { // The url to the first page is static. We don't need to use API to obtain it. return scoreinfo.thumbnailUrl } else { // obtain image urls using the API - return getFileUrl('img', i) + return getFileUrl(scoreinfo.id, 'img', i) } }) const sheetImgURLs = await Promise.all(rs) diff --git a/src/scoreinfo.ts b/src/scoreinfo.ts index 16c6b48..81a1c65 100644 --- a/src/scoreinfo.ts +++ b/src/scoreinfo.ts @@ -58,7 +58,3 @@ export class ScoreInfoInPage extends ScoreInfo { return url.split('@')[0] } } - -export const scoreinfo = new ScoreInfoInPage(document) - -export default scoreinfo