musescore-downloader/src/file.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-10-26 18:53:55 +00:00
/* eslint-disable no-extend-native */
import scoreinfo from './scoreinfo'
2020-11-09 19:09:25 +00:00
import { webpackHook, webpackGlobalOverride } from './webpack-hook'
2020-10-21 14:51:40 +00:00
const FILE_URL_MODULE_ID = 'iNJA'
2020-11-09 19:09:25 +00:00
const AUTH_MODULE_ID = 'FNf8'
const MAGIC_ARG_INDEX = 3
type FileType = 'img' | 'mp3' | 'midi'
2020-10-22 20:52:33 +00:00
/**
* I know this is super hacky.
*/
let magic: Promise<string> | string = new Promise((resolve) => {
2020-11-09 19:09:25 +00:00
webpackGlobalOverride(AUTH_MODULE_ID, (_, r, t) => { // override
const fn = r.a
t.d(r, 'a', () => {
return (...args) => {
2020-11-09 19:10:01 +00:00
if (magic instanceof Promise) {
2020-11-09 19:09:25 +00:00
magic = args[MAGIC_ARG_INDEX]
resolve(magic)
}
return fn(...args) as string
2020-11-05 21:47:50 +00:00
}
2020-11-09 19:09:25 +00:00
})
2020-11-05 21:47:50 +00:00
})
2020-10-22 20:52:33 +00:00
})
2020-10-21 14:51:40 +00:00
export const getFileUrl = async (type: FileType, index = 0): Promise<string> => {
const fileUrlModule = webpackHook(FILE_URL_MODULE_ID, {
2020-10-21 14:39:28 +00:00
'6Ulw' (_, r, t) { // override
t.d(r, 'a', () => {
return type
})
},
})
2020-10-26 18:53:55 +00:00
const fn: (id: number, index: number, cb: (url: string) => any, magic: string) => string = fileUrlModule.a
2020-10-22 20:52:33 +00:00
2020-11-09 19:10:01 +00:00
if (magic instanceof Promise) {
2020-10-22 20:52:33 +00:00
// force to retrieve the MAGIC
const el = document.querySelectorAll('.SD7H- > button')[3] as HTMLButtonElement
2020-10-22 20:52:33 +00:00
el.click()
magic = await magic
}
2020-10-21 14:51:40 +00:00
return new Promise((resolve) => {
2020-10-22 20:52:33 +00:00
return fn(scoreinfo.id, index, resolve, magic as string)
})
}