feat: webpack global override hook
This commit is contained in:
parent
6a767ea7ae
commit
b594dd9a1c
|
@ -1,11 +1,13 @@
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||||
|
|
||||||
|
import { hookNative } from './anti-detection'
|
||||||
|
|
||||||
interface Module {
|
interface Module {
|
||||||
(module, exports, __webpack_require__): void;
|
(module, exports, __webpack_require__): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebpackJson = [number, { [id: string]: Module }][]
|
type WebpackJson = [number[], { [id: string]: Module }][]
|
||||||
|
|
||||||
const moduleLookup = (id: string, globalWebpackJson: WebpackJson) => {
|
const moduleLookup = (id: string, globalWebpackJson: WebpackJson) => {
|
||||||
const pack = globalWebpackJson.find(x => x[1][id])!
|
const pack = globalWebpackJson.find(x => x[1][id])!
|
||||||
|
@ -47,4 +49,46 @@ export const webpackHook = (moduleId: string, moduleOverrides: { [id: string]: M
|
||||||
return t(moduleId)
|
return t(moduleId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const webpackGlobalOverride = (() => {
|
||||||
|
const moduleOverrides: { [id: string]: Module } = {}
|
||||||
|
|
||||||
|
function applyOverride (pack: WebpackJson[0]) {
|
||||||
|
Object.entries(moduleOverrides).forEach(([id, override]) => {
|
||||||
|
const mod = pack[1][id]
|
||||||
|
if (mod) {
|
||||||
|
pack[1][id] = function (n, r, t) {
|
||||||
|
// make exports configurable
|
||||||
|
t = Object.assign(t, {
|
||||||
|
d (exp, name, fn) {
|
||||||
|
return Object.defineProperty(exp, name, { enumerable: true, get: fn, configurable: true })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
mod(n, r, t)
|
||||||
|
override(n, r, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// hook `webpackJsonpmusescore.push` as soon as `webpackJsonpmusescore` is available
|
||||||
|
let jsonp
|
||||||
|
Object.defineProperty(window, 'webpackJsonpmusescore', {
|
||||||
|
get () { return jsonp },
|
||||||
|
set (v: WebpackJson) {
|
||||||
|
jsonp = v
|
||||||
|
hookNative(v, 'push', (_fn) => {
|
||||||
|
return function (pack) {
|
||||||
|
applyOverride(pack)
|
||||||
|
return _fn.call(this, pack)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// set overrides
|
||||||
|
return (moduleId: string, override: Module) => {
|
||||||
|
moduleOverrides[moduleId] = override
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|
||||||
export default webpackHook
|
export default webpackHook
|
||||||
|
|
Loading…
Reference in New Issue