From 21d5002575065cfbb286245e2f78f3a3957d4a3c Mon Sep 17 00:00:00 2001 From: Xmader Date: Thu, 22 Oct 2020 16:52:33 -0400 Subject: [PATCH] fix: auth magic --- src/file.ts | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/file.ts b/src/file.ts index 83a710b..10d6a5d 100644 --- a/src/file.ts +++ b/src/file.ts @@ -11,6 +11,27 @@ const getApiUrl = (id: number, type: FileType, index: number): string => { return `https://musescore.now.sh/api/jmuse?id=${id}&type=${type}&index=${index}` } +/** + * I know this is super hacky. + */ +let magic: Promise | string = new Promise((resolve) => { + const reg = '^\\d+(img|mp3|midi)\\d(\\w+)$' + + const _encode = encodeURIComponent + + window.encodeURIComponent = (s) => { + const m = s.toString().match(reg) + if (m) { + // the auth string will be encoded using `encodeURIComponent` before `md5`, + // so hook here + resolve(m[2]) + magic = m[2] + window.encodeURIComponent = _encode // detach + } + return _encode(s) + } +}) + export const getFileUrl = async (type: FileType, index = 0): Promise => { const fileUrlModule = webpackHook(FILE_URL_MODULE_ID, { '6Ulw' (_, r, t) { // override @@ -25,9 +46,16 @@ export const getFileUrl = async (type: FileType, index = 0): Promise => }, }) - const fn: (id: number, index: number, cb: (url: string) => any) => string = fileUrlModule.default + const fn: (id: number, index: number, cb: (url: string) => any, magic: string) => string = fileUrlModule.default + + if (typeof magic !== 'string') { + // force to retrieve the MAGIC + const el = document.querySelectorAll('._13vRI')[6] as HTMLButtonElement + el.click() + magic = await magic + } return new Promise((resolve) => { - return fn(scoreinfo.id, index, resolve) + return fn(scoreinfo.id, index, resolve, magic as string) }) }