From 624d4e3ac66abe3d55d31ff878c00093d4ee1b78 Mon Sep 17 00:00:00 2001 From: Peter Njeim Date: Tue, 16 Apr 2024 00:14:37 -0300 Subject: [PATCH] feat: update auth token suffix automatically --- src/file.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/file.ts b/src/file.ts index b6fdffa..f2ab0a4 100644 --- a/src/file.ts +++ b/src/file.ts @@ -7,13 +7,28 @@ import { auths } from "./file-magics"; export type FileType = "img" | "mp3" | "midi"; +const getSuffix = async (): Promise => { + const suffixUrl = ( + document.head.querySelector( + "link[href^='https://musescore.com/static/public/build/musescore_es6/20']" + ) as HTMLLinkElement + )?.href; + const suffixJs = await fetch(suffixUrl); + return (await suffixJs.text()).match( + '(?:.*)"(.+)"\\)\\.substr\\(0,4\\)' + )?.[1]!; +}; + const getApiUrl = (id: number, type: FileType, index: number): string => { return `/api/jmuse?id=${id}&type=${type}&index=${index}`; }; -const getApiAuth = (id: number, type: FileType, index: number): string => { - const suffix = "19!@37"; - const code = `${id}${type}${index}${suffix}`; +const getApiAuth = async ( + id: number, + type: FileType, + index: number +): Promise => { + const code = `${id}${type}${index}${await getSuffix()}`; return md5(code).slice(0, 4); }; @@ -123,7 +138,7 @@ export const getFileUrl = async ( _fetch = getFetch() ): Promise => { const url = getApiUrl(id, type, index); - let auth = getApiAuth(id, type, index); + let auth = await getApiAuth(id, type, index); let r = await _fetch(url, { headers: {