refactor
This commit is contained in:
parent
6f60cd8b38
commit
4fde5c3948
2 changed files with 49 additions and 43 deletions
44
src/file.ts
44
src/file.ts
|
@ -1,6 +1,6 @@
|
|||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
|
||||
import scoreinfo from './scoreinfo'
|
||||
import { webpackHook } from './webpack-hook'
|
||||
|
||||
const FILE_URL_MODULE_ID = 'iNJA'
|
||||
|
||||
|
@ -11,48 +11,6 @@ const getApiUrl = (id: number, type: FileType, index: number): string => {
|
|||
return `https://musescore.now.sh/api/jmuse?id=${id}&type=${type}&index=${index}`
|
||||
}
|
||||
|
||||
interface Module {
|
||||
(module, exports, __webpack_require__): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve (webpack_require) a module from the page's webpack package
|
||||
*
|
||||
* I know this is super hacky.
|
||||
*/
|
||||
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]
|
||||
}
|
||||
|
||||
const t = Object.assign((id: string, override = true) => {
|
||||
const r: any = {}
|
||||
const m: Module = (override && moduleOverrides[id])
|
||||
? moduleOverrides[id]
|
||||
: moduleLookup(id)
|
||||
m(r, r, t)
|
||||
if (r.exports) return r.exports
|
||||
return r
|
||||
}, {
|
||||
d (exp, name, fn) {
|
||||
return Object.prototype.hasOwnProperty.call(exp, name) ||
|
||||
Object.defineProperty(exp, name, { enumerable: true, get: fn })
|
||||
},
|
||||
n (e) {
|
||||
return e.__esModule ? () => e.default : () => e
|
||||
},
|
||||
r (r) {
|
||||
Object.defineProperty(r, '__esModule', { value: true })
|
||||
},
|
||||
e () {
|
||||
return Promise.resolve()
|
||||
},
|
||||
})
|
||||
|
||||
return t(moduleId)
|
||||
}
|
||||
|
||||
export const getFileUrl = async (type: FileType, index = 0): Promise<string> => {
|
||||
const fileUrlModule = webpackHook(FILE_URL_MODULE_ID, {
|
||||
'6Ulw' (_, r, t) { // override
|
||||
|
|
48
src/webpack-hook.ts
Normal file
48
src/webpack-hook.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
|
||||
interface Module {
|
||||
(module, exports, __webpack_require__): void;
|
||||
}
|
||||
|
||||
type WebpackJson = [number, { [id: string]: Module }][]
|
||||
|
||||
const moduleLookup = (id: string, globalWebpackJson: WebpackJson) => {
|
||||
const pack = globalWebpackJson.find(x => x[1][id])!
|
||||
return pack[1][id]
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve (webpack_require) a module from the page's webpack package
|
||||
*
|
||||
* I know this is super hacky.
|
||||
*/
|
||||
export const webpackHook = (moduleId: string, moduleOverrides: { [id: string]: Module } = {}, globalWebpackJson: WebpackJson = window['webpackJsonpmusescore']) => {
|
||||
const t = Object.assign((id: string, override = true) => {
|
||||
const r: any = {}
|
||||
const m: Module = (override && moduleOverrides[id])
|
||||
? moduleOverrides[id]
|
||||
: moduleLookup(id, globalWebpackJson)
|
||||
m(r, r, t)
|
||||
if (r.exports) return r.exports
|
||||
return r
|
||||
}, {
|
||||
d (exp, name, fn) {
|
||||
return Object.prototype.hasOwnProperty.call(exp, name) ||
|
||||
Object.defineProperty(exp, name, { enumerable: true, get: fn })
|
||||
},
|
||||
n (e) {
|
||||
return e.__esModule ? () => e.default : () => e
|
||||
},
|
||||
r (r) {
|
||||
Object.defineProperty(r, '__esModule', { value: true })
|
||||
},
|
||||
e () {
|
||||
return Promise.resolve()
|
||||
},
|
||||
})
|
||||
|
||||
return t(moduleId)
|
||||
}
|
||||
|
||||
export default webpackHook
|
Loading…
Reference in a new issue