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-21 14:39:28 +00:00
|
|
|
interface Module {
|
|
|
|
(module, exports, __webpack_require__): void;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2020-10-21 14:39:28 +00:00
|
|
|
const webpackHook = (moduleId: string, moduleOverrides: { [id: string]: Module } = {}, globalWebpackJson = window['webpackJsonpmusescore']) => {
|
|
|
|
const moduleLookup = (id: string) => {
|
|
|
|
const pack = globalWebpackJson.find(x => x[1][id])
|
|
|
|
return pack[1][id]
|
|
|
|
}
|
2020-10-19 09:57:09 +00:00
|
|
|
|
2020-10-21 14:39:28 +00:00
|
|
|
const t = Object.assign((id: string, override = true) => {
|
2020-10-19 09:57:09 +00:00
|
|
|
const r: any = {}
|
2020-10-21 14:39:28 +00:00
|
|
|
const m: Module = (override && moduleOverrides[id])
|
|
|
|
? moduleOverrides[id]
|
|
|
|
: moduleLookup(id)
|
|
|
|
m(r, r, t)
|
2020-10-19 09:57:09 +00:00
|
|
|
if (r.exports) return r.exports
|
|
|
|
return r
|
|
|
|
}, {
|
|
|
|
d (exp, name, fn) {
|
2020-10-21 14:39:28 +00:00
|
|
|
return Object.prototype.hasOwnProperty.call(exp, name) ||
|
|
|
|
Object.defineProperty(exp, name, { enumerable: true, get: fn })
|
2020-10-19 09:57:09 +00:00
|
|
|
},
|
|
|
|
n (e) {
|
2020-10-21 14:39:28 +00:00
|
|
|
return e.__esModule ? () => e.default : () => e
|
|
|
|
},
|
|
|
|
r (r) {
|
|
|
|
Object.defineProperty(r, '__esModule', { value: true })
|
|
|
|
},
|
|
|
|
e () {
|
|
|
|
return Promise.resolve()
|
2020-10-19 09:57:09 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return t(moduleId)
|
|
|
|
}
|
|
|
|
|
2020-10-18 11:21:25 +00:00
|
|
|
const getApiAuth = (type: FileType, index: number): string => {
|
2020-10-21 14:39:28 +00:00
|
|
|
const authModule = webpackHook(AUTH_MODULE_ID, {
|
|
|
|
'6Ulw' (_, r, t) { // override
|
|
|
|
t.d(r, 'a', () => {
|
|
|
|
return type
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
2020-10-19 09:57:09 +00:00
|
|
|
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
|
|
|
|
}
|