2020-10-19 09:57:09 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
2020-10-18 11:21:25 +00:00
|
|
|
|
|
|
|
import scoreinfo from './scoreinfo'
|
|
|
|
|
2020-10-19 09:57:09 +00:00
|
|
|
const AUTH_MODULE_ID = 'FNf8'
|
2020-10-18 11:21:25 +00:00
|
|
|
|
|
|
|
type FileType = 'img' | 'mp3' | 'midi'
|
|
|
|
|
|
|
|
const getApiUrl = (type: FileType, index: number): string => {
|
|
|
|
// proxy
|
|
|
|
return `https://musescore.now.sh/api/jmuse?id=${scoreinfo.id}&type=${type}&index=${index}`
|
|
|
|
}
|
|
|
|
|
2020-10-19 09:57:09 +00:00
|
|
|
/**
|
|
|
|
* Retrieve (webpack_require) a module from the page's webpack package
|
|
|
|
*
|
2020-10-19 12:00:20 +00:00
|
|
|
* I know this is super hacky.
|
2020-10-19 09:57:09 +00:00
|
|
|
*/
|
|
|
|
const webpackHook = (moduleId: string, globalWebpackJson = window['webpackJsonpmusescore']) => {
|
|
|
|
const pack = globalWebpackJson.find(x => x[1][moduleId])
|
|
|
|
|
|
|
|
const t = Object.assign((id: string) => {
|
|
|
|
const r: any = {}
|
|
|
|
pack[1][id](r, r, t)
|
|
|
|
if (r.exports) return r.exports
|
|
|
|
return r
|
|
|
|
}, {
|
|
|
|
d (exp, name, fn) {
|
|
|
|
return Object.defineProperty(exp, name, { value: fn })
|
|
|
|
},
|
|
|
|
n (e) {
|
|
|
|
return () => e
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return t(moduleId)
|
|
|
|
}
|
|
|
|
|
2020-10-18 11:21:25 +00:00
|
|
|
const getApiAuth = (type: FileType, index: number): string => {
|
2020-10-19 09:57:09 +00:00
|
|
|
const authModule = webpackHook(AUTH_MODULE_ID)
|
|
|
|
const fn: (id: number, type: string, index: number) => string = authModule.a()
|
|
|
|
return fn(scoreinfo.id, type, index)
|
2020-10-18 11:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getFileUrl = async (type: FileType, index = 0): Promise<string> => {
|
|
|
|
const url = getApiUrl(type, index)
|
|
|
|
const auth = getApiAuth(type, index)
|
|
|
|
|
|
|
|
const r = await fetch(url, {
|
|
|
|
headers: {
|
|
|
|
Authorization: auth,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const { info } = await r.json()
|
|
|
|
return info.url as string
|
|
|
|
}
|