Skip to content

Commit 3502139

Browse files
committed
[devtools-snippet] add webpackRuntimeInjection.js
1 parent 4cc29ad commit 3502139

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// webpackRuntimeInjection.js
2+
// https://github.com/0xdevalias/userscripts/tree/main/devtools-snippets
3+
//
4+
// See also:
5+
// - https://gist.github.com/search?q=user:0xdevalias+webpack
6+
// - https://gist.github.com/0xdevalias/8c621c5d09d780b1d321bfdb86d67cdd#runtime-injection--overrides
7+
8+
/**
9+
* Injects wrappers into specified webpack modules to allow manipulation or stubbing.
10+
* @param {Object} overrides - An object where keys are module IDs and values are functions that take the old module function and return a new module function.
11+
*/
12+
function injectWebpackOverrides(overrides) {
13+
window.webpackChunk_N_E.forEach(chunk => {
14+
const [_, modules] = chunk;
15+
Object.keys(modules).forEach(moduleId => {
16+
if (Object.hasOwn(modules, moduleId)) {
17+
const originalModule = modules[moduleId];
18+
// Check if there is an override for the module and apply it
19+
if (overrides.hasOwnProperty(moduleId)) {
20+
// The override function receives the original module and returns a new module function
21+
modules[moduleId] = overrides[moduleId](originalModule);
22+
}
23+
}
24+
});
25+
});
26+
}
27+
28+
/**
29+
* Creates a wrapped require function that can intercept module requests.
30+
* @param {Function} originalRequire - The original require function.
31+
* @param {Object} overrides - The overrides definitions.
32+
* @returns {Function} The wrapped require function.
33+
*/
34+
function createWrappedRequire(originalRequire, overrides) {
35+
return (moduleId) => {
36+
if (Object.hasOwn(overrides, moduleId)) {
37+
return overrides[moduleId](originalRequire(moduleId));
38+
}
39+
return originalRequire(moduleId);
40+
};
41+
}
42+
43+
// Example usage:
44+
// injectWebpackOverrides({
45+
// '51510': (oldModule) => {
46+
// // Create a wrapped require function directly with the specific overrides for modules this module depends on
47+
// const wrappedRequire = createWrappedRequire(oldModule.require, {
48+
// '48779': (originalModule48779) => {
49+
// // Modify or replace the behavior of module 48779
50+
// return {
51+
// ...originalModule48779,
52+
// someFunction: () => 'Modified behavior'
53+
// };
54+
// }
55+
// });
56+
57+
// // Returns a new function that uses the wrapped require
58+
// return (e, t, a) => {
59+
// // Call the old module function using the wrappedRequire
60+
// return oldModule(e, t, wrappedRequire);
61+
// };
62+
// },
63+
// '48779': (oldModule) => {
64+
// // Return a completely new function, replacing the old module
65+
// return (e, t, a) => {
66+
// // Return directly whatever is needed for the shim
67+
// return { /* shimmed module contents */ };
68+
// };
69+
// }
70+
// });

0 commit comments

Comments
 (0)