musescore-downloader/src/file.ts

116 lines
2.9 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-30 14:24:33 +00:00
import { hookNative } from './anti-detection'
2020-11-21 06:22:35 +00:00
import { console } from './utils'
type FileType = 'img' | 'mp3' | 'midi'
2021-06-06 16:47:21 +00:00
const TYPE_REG = /type=(img|mp3|midi)/
2020-11-30 14:24:33 +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-30 19:23:24 +00:00
try {
2020-11-30 19:55:29 +00:00
const p = Object.getPrototypeOf(document.body)
Object.setPrototypeOf(document.body, null)
hookNative(document.body, 'append', () => {
2020-11-30 19:23:24 +00:00
return function (...nodes: Node[]) {
2020-11-30 19:55:29 +00:00
p.append.call(this, ...nodes)
2020-11-30 14:24:33 +00:00
2020-11-30 19:23:24 +00:00
if (nodes[0].nodeName === 'IFRAME') {
const iframe = nodes[0] as HTMLIFrameElement
const w = iframe.contentWindow as Window
2020-11-30 14:24:33 +00:00
2020-12-06 05:51:51 +00:00
hookNative(w, 'fetch', () => {
2020-11-30 19:23:24 +00:00
return function (url, init) {
const token = init?.headers?.Authorization
if (typeof url === 'string' && token) {
const m = url.match(TYPE_REG)
2021-06-06 16:47:21 +00:00
console.debug(url, token, m)
2020-11-30 19:23:24 +00:00
if (m) {
2021-06-06 16:47:21 +00:00
const type = m[1]
2020-11-30 19:23:24 +00:00
// eslint-disable-next-line no-unused-expressions
l[type]?.(token)
}
2020-11-30 14:24:33 +00:00
}
2020-12-06 05:51:51 +00:00
return fetch(url, init)
2020-11-30 14:24:33 +00:00
}
2020-11-30 19:23:24 +00:00
})
}
2020-11-30 14:24:33 +00:00
}
2020-11-30 19:23:24 +00:00
})
2020-11-30 19:55:29 +00:00
Object.setPrototypeOf(document.body, p)
2020-11-30 19:23:24 +00:00
} catch (err) {
console.error(err)
}
2020-11-30 14:24:33 +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': {
2021-01-27 16:09:06 +00:00
const el = document.querySelector('button[hasaccess]') as HTMLButtonElement
2020-11-21 06:22:35 +00:00
el.click()
break
}
case 'mp3': {
2020-11-30 14:24:33 +00:00
const el = document.querySelector('button[title="Toggle Play"]') as HTMLButtonElement
2020-11-21 06:22:35 +00:00
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
}