musescore-downloader/src/file.ts

102 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-10-26 18:53:55 +00:00
/* eslint-disable no-extend-native */
2020-11-21 06:22:35 +00:00
/* eslint-disable @typescript-eslint/no-unsafe-return */
2020-11-21 06:22:35 +00:00
import { ALL, webpackGlobalOverride } from './webpack-hook'
import { console } from './utils'
type FileType = 'img' | 'mp3' | 'midi'
2020-11-21 06:22:35 +00:00
const TYPE_REG = /id=(\d+)&type=(img|mp3|midi)/
2020-11-17 17:09:49 +00:00
2020-10-22 20:52:33 +00:00
/**
* I know this is super hacky.
*/
2020-11-21 06:22:35 +00:00
const magicHookConstr = (() => {
const l = {}
2020-11-19 22:54:41 +00:00
2020-11-21 06:22:35 +00:00
webpackGlobalOverride(ALL, (n, r, t) => { // override
const e = n.exports
if (typeof e === 'object' && e.fetch) {
const fn = e.fetch
t.d(e, 'fetch', () => {
return function (...args) {
const [url, init] = args
const token = init?.headers?.Authorization
if (typeof url === 'string' && token) {
const m = url.match(TYPE_REG)
if (m) {
const type = m[2]
// eslint-disable-next-line no-unused-expressions
l[type]?.(token)
2020-11-19 22:54:41 +00:00
}
2020-11-18 18:57:56 +00:00
}
2020-11-21 06:22:35 +00:00
return fn(...args)
2020-11-19 05:51:18 +00:00
}
})
2020-11-21 06:22:35 +00:00
}
2020-11-05 21:47:50 +00:00
})
2020-11-21 06:22:35 +00:00
return async (type: FileType) => {
return new Promise<string>((resolve) => {
l[type] = (token) => {
resolve(token)
magics[type] = token
}
})
}
})()
2020-11-17 17:09:49 +00:00
const magics: Record<FileType, Promise<string>> = {
2020-11-18 18:57:56 +00:00
img: magicHookConstr('img'),
midi: magicHookConstr('midi'),
mp3: magicHookConstr('mp3'),
2020-11-17 17:09:49 +00:00
}
2020-10-22 20:52:33 +00:00
2020-11-24 07:51:43 +00:00
const getApiUrl = (id: number, type: FileType, index: number): string => {
return `/api/jmuse?id=${id}&type=${type}&index=${index}&v2=1`
2020-11-13 23:27:28 +00:00
}
2020-10-22 20:52:33 +00:00
2020-11-13 23:27:28 +00:00
const getApiAuth = async (type: FileType, index: number): Promise<string> => {
2020-11-17 17:09:49 +00:00
// eslint-disable-next-line no-void
void index
2020-11-21 06:22:35 +00:00
const magic = magics[type]
if (magic instanceof Promise) {
// force to retrieve the MAGIC
switch (type) {
case 'midi': {
const el = document.querySelectorAll('.SD7H- > button')[3] as HTMLButtonElement
el.click()
break
}
case 'mp3': {
const el = document.querySelector('#playerBtnExprt') as HTMLButtonElement
el.click()
break
}
case 'img': {
const imgE = document.querySelector('img[src*=score_]')
const nextE = imgE?.parentElement?.nextElementSibling
if (nextE) nextE.scrollIntoView()
break
}
}
}
return magic
2020-11-13 23:27:28 +00:00
}
2020-11-24 07:51:43 +00:00
export const getFileUrl = async (id: number, type: FileType, index = 0): Promise<string> => {
const url = getApiUrl(id, type, index)
2020-11-13 23:27:28 +00:00
const auth = await getApiAuth(type, index)
const r = await fetch(url, {
headers: {
Authorization: auth,
},
})
2020-11-13 23:27:28 +00:00
const { info } = await r.json()
return info.url as string
}