feat: webpack global override hook

This commit is contained in:
Xmader 2020-11-09 14:07:57 -05:00
parent 6a767ea7ae
commit b594dd9a1c
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
1 changed files with 45 additions and 1 deletions

View File

@ -1,11 +1,13 @@
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { hookNative } from './anti-detection'
interface Module {
(module, exports, __webpack_require__): void;
}
type WebpackJson = [number, { [id: string]: Module }][]
type WebpackJson = [number[], { [id: string]: Module }][]
const moduleLookup = (id: string, globalWebpackJson: WebpackJson) => {
const pack = globalWebpackJson.find(x => x[1][id])!
@ -47,4 +49,46 @@ export const webpackHook = (moduleId: string, moduleOverrides: { [id: string]: M
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