musescore-downloader/src/file.ts

57 lines
1.6 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-12 18:29:10 +00:00
import { webpackHook, webpackGlobalOverride, ALL } from './webpack-hook'
2020-10-21 14:51:40 +00:00
const FILE_URL_MODULE_ID = 'iNJA'
2020-11-12 18:29:10 +00:00
const ARG_NUMBER = 4
2020-11-09 19:09:25 +00:00
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-12 14:46:06 +00:00
// todo: hook module by what it does, not what it is called
2020-11-12 18:29:10 +00:00
webpackGlobalOverride(ALL, (_, r, t) => { // override
2020-11-09 19:09:25 +00:00
const fn = r.a
2020-11-12 18:29:10 +00:00
if (typeof fn === 'function' && fn.length === ARG_NUMBER) {
t.d(r, 'a', () => {
return (...args) => {
if (magic instanceof Promise) {
2020-11-12 18:33:11 +00:00
if (args.every(a => typeof a === 'number' || typeof a === 'string')) {
magic = args[MAGIC_ARG_INDEX]
resolve(magic)
}
2020-11-12 18:29:10 +00:00
}
return fn(...args) as string
2020-11-09 19:09:25 +00:00
}
2020-11-12 18:29:10 +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-11-13 05:26:39 +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-11-13 05:26:39 +00:00
return fn(undefined, scoreinfo.id, index, resolve, magic as string)
})
}