@whitesev/utils 2.9.12 → 2.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -176
- package/dist/index.amd.js +206 -117
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +1 -1
- package/dist/index.amd.min.js.map +1 -1
- package/dist/index.cjs.js +206 -117
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +206 -117
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +206 -117
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.system.js +206 -117
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/index.system.min.js.map +1 -1
- package/dist/index.umd.js +206 -117
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/Dictionary.d.ts +2 -0
- package/dist/types/src/Utils.d.ts +9 -2
- package/dist/types/src/types/Httpx.d.ts +1344 -1344
- package/dist/types/src/types/Log.d.ts +19 -19
- package/dist/types/src/types/Progress.d.ts +20 -20
- package/dist/types/src/types/React.d.ts +119 -119
- package/dist/types/src/types/TryCatch.d.ts +9 -9
- package/dist/types/src/types/UtilsGMCookie.d.ts +93 -93
- package/dist/types/src/types/UtilsGMMenu.d.ts +77 -77
- package/dist/types/src/types/Vue2.d.ts +166 -166
- package/dist/types/src/types/WindowApi.d.ts +14 -14
- package/dist/types/src/types/ajaxHooker.d.ts +155 -155
- package/dist/types/src/types/env.d.ts +7 -7
- package/dist/types/src/types/global.d.ts +31 -31
- package/package.json +26 -24
- package/src/ColorConversion.ts +118 -118
- package/src/CommonUtil.ts +301 -301
- package/src/DOMUtils.ts +251 -251
- package/src/Dictionary.ts +205 -199
- package/src/GBKEncoder.ts +108 -108
- package/src/Hooks.ts +73 -73
- package/src/Httpx.ts +1457 -1457
- package/src/LockFunction.ts +65 -62
- package/src/Log.ts +233 -233
- package/src/ModuleRaid.js +378 -360
- package/src/Progress.ts +108 -108
- package/src/TryCatch.ts +86 -86
- package/src/Utils.ts +3907 -3852
- package/src/UtilsCommon.ts +14 -14
- package/src/UtilsGMCookie.ts +273 -273
- package/src/UtilsGMMenu.ts +460 -460
- package/src/Vue.ts +233 -233
- package/src/WindowApi.ts +59 -59
- package/src/ajaxHooker/ajaxHooker.js +606 -538
- package/src/ajaxHooker/ajaxHooker1.2.4.js +440 -438
- package/src/indexedDB.ts +497 -497
- package/src/types/Httpx.d.ts +1344 -1344
- package/src/types/Log.d.ts +19 -19
- package/src/types/Progress.d.ts +20 -20
- package/src/types/React.d.ts +119 -119
- package/src/types/TryCatch.d.ts +9 -9
- package/src/types/UtilsGMCookie.d.ts +93 -93
- package/src/types/UtilsGMMenu.d.ts +77 -77
- package/src/types/Vue2.d.ts +166 -166
- package/src/types/WindowApi.d.ts +14 -14
- package/src/types/ajaxHooker.d.ts +155 -155
- package/src/types/env.d.ts +7 -7
- package/src/types/global.d.ts +31 -31
package/src/ModuleRaid.js
CHANGED
|
@@ -9,390 +9,408 @@
|
|
|
9
9
|
// @original-script https://github.com/pixeldesu/moduleRaid
|
|
10
10
|
// ==/UserScript==
|
|
11
11
|
|
|
12
|
-
|
|
13
12
|
/**
|
|
14
13
|
* Main moduleRaid class
|
|
15
14
|
* @link https://scriptcat.org/zh-CN/script-show-page/2628
|
|
16
15
|
*/
|
|
17
16
|
export class ModuleRaid {
|
|
17
|
+
/**
|
|
18
|
+
* moduleRaid constructor
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* Constructing an instance without any arguments:
|
|
22
|
+
* ```ts
|
|
23
|
+
* const mR = new ModuleRaid()
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Constructing an instance with the optional `opts` object:
|
|
27
|
+
* ```ts
|
|
28
|
+
* const mR = new ModuleRaid({ entrypoint: 'webpackChunk_custom_name' })
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param opts a object containing options to initialize moduleRaid with
|
|
32
|
+
* - **opts:**
|
|
33
|
+
* - _target_: the window object being searched for
|
|
34
|
+
* - _entrypoint_: the Webpack entrypoint present on the global window object
|
|
35
|
+
* - _debug_: whether debug mode is enabled or not
|
|
36
|
+
* - _strict_: whether strict mode is enabled or not
|
|
37
|
+
*/
|
|
38
|
+
constructor(opts) {
|
|
18
39
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* Constructing an instance without any arguments:
|
|
23
|
-
* ```ts
|
|
24
|
-
* const mR = new ModuleRaid()
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* Constructing an instance with the optional `opts` object:
|
|
28
|
-
* ```ts
|
|
29
|
-
* const mR = new ModuleRaid({ entrypoint: 'webpackChunk_custom_name' })
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* @param opts a object containing options to initialize moduleRaid with
|
|
33
|
-
* - **opts:**
|
|
34
|
-
* - _target_: the window object being searched for
|
|
35
|
-
* - _entrypoint_: the Webpack entrypoint present on the global window object
|
|
36
|
-
* - _debug_: whether debug mode is enabled or not
|
|
37
|
-
* - _strict_: whether strict mode is enabled or not
|
|
40
|
+
* A random generated module ID we use for injecting into Webpack
|
|
38
41
|
*/
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* A random generated module ID we use for injecting into Webpack
|
|
42
|
-
*/
|
|
43
|
-
this.moduleID = Math.random().toString(36).substring(7);
|
|
44
|
-
/**
|
|
45
|
-
* An array containing different argument injection methods for
|
|
46
|
-
* Webpack (before version 4), and subsequently pulling out methods and modules
|
|
47
|
-
* @internal
|
|
48
|
-
*/
|
|
49
|
-
this.functionArguments = [
|
|
50
|
-
[
|
|
51
|
-
[0],
|
|
52
|
-
[
|
|
53
|
-
(_e, _t, i) => {
|
|
54
|
-
this.modules = i.c;
|
|
55
|
-
this.constructors = i.m;
|
|
56
|
-
this.get = i;
|
|
57
|
-
},
|
|
58
|
-
],
|
|
59
|
-
],
|
|
60
|
-
[
|
|
61
|
-
[1e3],
|
|
62
|
-
{
|
|
63
|
-
[this.moduleID]: (_e, _t, i) => {
|
|
64
|
-
this.modules = i.c;
|
|
65
|
-
this.constructors = i.m;
|
|
66
|
-
this.get = i;
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
[[this.moduleID]],
|
|
70
|
-
],
|
|
71
|
-
];
|
|
72
|
-
/**
|
|
73
|
-
* An array containing different argument injection methods for
|
|
74
|
-
* Webpack (after version 4), and subsequently pulling out methods and modules
|
|
75
|
-
* @internal
|
|
76
|
-
*/
|
|
77
|
-
this.arrayArguments = [
|
|
78
|
-
[
|
|
79
|
-
[this.moduleID],
|
|
80
|
-
{},
|
|
81
|
-
(e) => {
|
|
82
|
-
const mCac = e.m;
|
|
83
|
-
Object.keys(mCac).forEach((mod) => {
|
|
84
|
-
try {
|
|
85
|
-
this.modules[mod] = e(mod);
|
|
86
|
-
}
|
|
87
|
-
catch (err) {
|
|
88
|
-
this.log(`[arrayArguments/1] Failed to require(${mod}) with error:\n${err}\n${err.stack}`);
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
this.get = e;
|
|
92
|
-
},
|
|
93
|
-
],
|
|
94
|
-
this.functionArguments[1],
|
|
95
|
-
];
|
|
96
|
-
/**
|
|
97
|
-
* Storage for the modules we extracted from Webpack
|
|
98
|
-
*/
|
|
99
|
-
this.modules = {};
|
|
100
|
-
/**
|
|
101
|
-
* Storage for the constructors we extracted from Webpack
|
|
102
|
-
*/
|
|
103
|
-
this.constructors = [];
|
|
104
|
-
let options = {
|
|
105
|
-
target: window,
|
|
106
|
-
entrypoint: 'webpackJsonp',
|
|
107
|
-
debug: false,
|
|
108
|
-
strict: false,
|
|
109
|
-
};
|
|
110
|
-
if (typeof opts === 'object') {
|
|
111
|
-
options = Object.assign(Object.assign({}, options), opts);
|
|
112
|
-
}
|
|
113
|
-
this.target = options.target
|
|
114
|
-
this.entrypoint = options.entrypoint;
|
|
115
|
-
this.debug = options.debug;
|
|
116
|
-
this.strict = options.strict;
|
|
117
|
-
this.detectEntrypoint();
|
|
118
|
-
this.fillModules();
|
|
119
|
-
this.replaceGet();
|
|
120
|
-
this.setupPushEvent();
|
|
121
|
-
}
|
|
42
|
+
this.moduleID = Math.random().toString(36).substring(7);
|
|
122
43
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* @param {*} message The message to be logged
|
|
44
|
+
* An array containing different argument injection methods for
|
|
45
|
+
* Webpack (before version 4), and subsequently pulling out methods and modules
|
|
126
46
|
* @internal
|
|
127
47
|
*/
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
48
|
+
this.functionArguments = [
|
|
49
|
+
[
|
|
50
|
+
[0],
|
|
51
|
+
[
|
|
52
|
+
(_e, _t, i) => {
|
|
53
|
+
this.modules = i.c;
|
|
54
|
+
this.constructors = i.m;
|
|
55
|
+
this.get = i;
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
],
|
|
59
|
+
[
|
|
60
|
+
[1e3],
|
|
61
|
+
{
|
|
62
|
+
[this.moduleID]: (_e, _t, i) => {
|
|
63
|
+
this.modules = i.c;
|
|
64
|
+
this.constructors = i.m;
|
|
65
|
+
this.get = i;
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
[[this.moduleID]],
|
|
69
|
+
],
|
|
70
|
+
];
|
|
133
71
|
/**
|
|
134
|
-
*
|
|
135
|
-
*
|
|
72
|
+
* An array containing different argument injection methods for
|
|
73
|
+
* Webpack (after version 4), and subsequently pulling out methods and modules
|
|
136
74
|
* @internal
|
|
137
75
|
*/
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
try {
|
|
152
|
-
if (this.modules && Object.keys(this.modules).length > 0)
|
|
153
|
-
return;
|
|
154
|
-
this.target[this.entrypoint](...argument);
|
|
155
|
-
}
|
|
156
|
-
catch (err) {
|
|
157
|
-
this.log(`moduleRaid.functionArguments[${index}] failed:\n${err}\n${err.stack}`);
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
this.arrayArguments.forEach((argument, index) => {
|
|
163
|
-
try {
|
|
164
|
-
if (this.modules && Object.keys(this.modules).length > 0)
|
|
165
|
-
return;
|
|
166
|
-
this.target[this.entrypoint].push(argument);
|
|
167
|
-
}
|
|
168
|
-
catch (err) {
|
|
169
|
-
this.log(`Pushing moduleRaid.arrayArguments[${index}] into ${this.entrypoint} failed:\n${err}\n${err.stack}`);
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
if (this.modules && Object.keys(this.modules).length == 0) {
|
|
174
|
-
let moduleEnd = false;
|
|
175
|
-
let moduleIterator = 0;
|
|
176
|
-
if (typeof this.target[this.entrypoint] != 'function' || !this.target[this.entrypoint]([], [], [moduleIterator])) {
|
|
177
|
-
throw Error('Unknown Webpack structure');
|
|
178
|
-
}
|
|
179
|
-
while (!moduleEnd) {
|
|
180
|
-
try {
|
|
181
|
-
this.modules[moduleIterator] = this.target[this.entrypoint]([], [], [moduleIterator]);
|
|
182
|
-
moduleIterator++;
|
|
183
|
-
}
|
|
184
|
-
catch (err) {
|
|
185
|
-
moduleEnd = true;
|
|
186
|
-
}
|
|
76
|
+
this.arrayArguments = [
|
|
77
|
+
[
|
|
78
|
+
[this.moduleID],
|
|
79
|
+
{},
|
|
80
|
+
(e) => {
|
|
81
|
+
const mCac = e.m;
|
|
82
|
+
Object.keys(mCac).forEach((mod) => {
|
|
83
|
+
try {
|
|
84
|
+
this.modules[mod] = e(mod);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
this.log(
|
|
87
|
+
`[arrayArguments/1] Failed to require(${mod}) with error:\n${err}\n${err.stack}`,
|
|
88
|
+
);
|
|
187
89
|
}
|
|
188
|
-
|
|
189
|
-
|
|
90
|
+
});
|
|
91
|
+
this.get = e;
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
this.functionArguments[1],
|
|
95
|
+
];
|
|
190
96
|
/**
|
|
191
|
-
*
|
|
192
|
-
* chunks being pushed into Webpack
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* You can listen for newly pushed packages using the `moduleraid:webpack-push` event
|
|
196
|
-
* on `document`
|
|
197
|
-
*
|
|
198
|
-
* ```ts
|
|
199
|
-
* document.addEventListener('moduleraid:webpack-push', (e) => {
|
|
200
|
-
* // e.detail contains the arguments push() was called with
|
|
201
|
-
* console.log(e.detail)
|
|
202
|
-
* })
|
|
203
|
-
* ```
|
|
204
|
-
* @internal
|
|
97
|
+
* Storage for the modules we extracted from Webpack
|
|
205
98
|
*/
|
|
206
|
-
|
|
207
|
-
const originalPush = this.target[this.entrypoint].push;
|
|
208
|
-
this.target[this.entrypoint].push = (...args) => {
|
|
209
|
-
const result = Reflect.apply(originalPush, this.target[this.entrypoint], args);
|
|
210
|
-
document.dispatchEvent(new CustomEvent('moduleraid:webpack-push', { detail: args }));
|
|
211
|
-
return result;
|
|
212
|
-
};
|
|
213
|
-
}
|
|
99
|
+
this.modules = {};
|
|
214
100
|
/**
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* If the default entrypoint, or the entrypoint that's passed to the moduleRaid constructor
|
|
218
|
-
* already matches, the method exits early
|
|
219
|
-
*
|
|
220
|
-
* If `options.strict` has been set in the constructor and the initial entrypoint cannot
|
|
221
|
-
* be found, this method will error, demanding a strictly set entrypoint
|
|
222
|
-
* @internal
|
|
101
|
+
* Storage for the constructors we extracted from Webpack
|
|
223
102
|
*/
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
103
|
+
this.constructors = [];
|
|
104
|
+
let options = {
|
|
105
|
+
target: window,
|
|
106
|
+
entrypoint: "webpackJsonp",
|
|
107
|
+
debug: false,
|
|
108
|
+
strict: false,
|
|
109
|
+
};
|
|
110
|
+
if (typeof opts === "object") {
|
|
111
|
+
options = Object.assign(Object.assign({}, options), opts);
|
|
112
|
+
}
|
|
113
|
+
this.target = options.target;
|
|
114
|
+
this.entrypoint = options.entrypoint;
|
|
115
|
+
this.debug = options.debug;
|
|
116
|
+
this.strict = options.strict;
|
|
117
|
+
this.detectEntrypoint();
|
|
118
|
+
this.fillModules();
|
|
119
|
+
this.replaceGet();
|
|
120
|
+
this.setupPushEvent();
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Debug logging method, outputs to the console when {@link ModuleRaid.debug} is true
|
|
124
|
+
*
|
|
125
|
+
* @param {*} message The message to be logged
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
128
|
+
log(message) {
|
|
129
|
+
if (this.debug) {
|
|
130
|
+
console.warn(`[moduleRaid] ${message}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Method to set an alternative getter if we weren't able to extract __webpack_require__
|
|
135
|
+
* from Webpack
|
|
136
|
+
* @internal
|
|
137
|
+
*/
|
|
138
|
+
replaceGet() {
|
|
139
|
+
if (this.get === null) {
|
|
140
|
+
this.get = (key) => this.modules[key];
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Method that will try to inject a module into Webpack or get modules
|
|
145
|
+
* depending on it's success it might be more or less brute about it
|
|
146
|
+
* @internal
|
|
147
|
+
*/
|
|
148
|
+
fillModules() {
|
|
149
|
+
if (typeof this.target[this.entrypoint] === "function") {
|
|
150
|
+
this.functionArguments.forEach((argument, index) => {
|
|
151
|
+
try {
|
|
152
|
+
if (this.modules && Object.keys(this.modules).length > 0) return;
|
|
153
|
+
this.target[this.entrypoint](...argument);
|
|
154
|
+
} catch (err) {
|
|
155
|
+
this.log(
|
|
156
|
+
`moduleRaid.functionArguments[${index}] failed:\n${err}\n${err.stack}`,
|
|
157
|
+
);
|
|
237
158
|
}
|
|
238
|
-
|
|
239
|
-
|
|
159
|
+
});
|
|
160
|
+
} else {
|
|
161
|
+
this.arrayArguments.forEach((argument, index) => {
|
|
162
|
+
try {
|
|
163
|
+
if (this.modules && Object.keys(this.modules).length > 0) return;
|
|
164
|
+
this.target[this.entrypoint].push(argument);
|
|
165
|
+
} catch (err) {
|
|
166
|
+
this.log(
|
|
167
|
+
`Pushing moduleRaid.arrayArguments[${index}] into ${this.entrypoint} failed:\n${err}\n${err.stack}`,
|
|
168
|
+
);
|
|
240
169
|
}
|
|
241
|
-
|
|
242
|
-
this.entrypoint = windowObjects[0];
|
|
170
|
+
});
|
|
243
171
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
return true;
|
|
264
|
-
}
|
|
265
|
-
else {
|
|
266
|
-
if (this.searchObject(value, query))
|
|
267
|
-
return true;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
172
|
+
if (this.modules && Object.keys(this.modules).length == 0) {
|
|
173
|
+
let moduleEnd = false;
|
|
174
|
+
let moduleIterator = 0;
|
|
175
|
+
if (
|
|
176
|
+
typeof this.target[this.entrypoint] != "function" ||
|
|
177
|
+
!this.target[this.entrypoint]([], [], [moduleIterator])
|
|
178
|
+
) {
|
|
179
|
+
throw Error("Unknown Webpack structure");
|
|
180
|
+
}
|
|
181
|
+
while (!moduleEnd) {
|
|
182
|
+
try {
|
|
183
|
+
this.modules[moduleIterator] = this.target[this.entrypoint](
|
|
184
|
+
[],
|
|
185
|
+
[],
|
|
186
|
+
[moduleIterator],
|
|
187
|
+
);
|
|
188
|
+
moduleIterator++;
|
|
189
|
+
} catch (err) {
|
|
190
|
+
moduleEnd = true;
|
|
270
191
|
}
|
|
271
|
-
|
|
192
|
+
}
|
|
272
193
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Method to hook into `window[this.entrypoint].push` adding a listener for new
|
|
197
|
+
* chunks being pushed into Webpack
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* You can listen for newly pushed packages using the `moduleraid:webpack-push` event
|
|
201
|
+
* on `document`
|
|
202
|
+
*
|
|
203
|
+
* ```ts
|
|
204
|
+
* document.addEventListener('moduleraid:webpack-push', (e) => {
|
|
205
|
+
* // e.detail contains the arguments push() was called with
|
|
206
|
+
* console.log(e.detail)
|
|
207
|
+
* })
|
|
208
|
+
* ```
|
|
209
|
+
* @internal
|
|
210
|
+
*/
|
|
211
|
+
setupPushEvent() {
|
|
212
|
+
const originalPush = this.target[this.entrypoint].push;
|
|
213
|
+
this.target[this.entrypoint].push = (...args) => {
|
|
214
|
+
const result = Reflect.apply(
|
|
215
|
+
originalPush,
|
|
216
|
+
this.target[this.entrypoint],
|
|
217
|
+
args,
|
|
218
|
+
);
|
|
219
|
+
document.dispatchEvent(
|
|
220
|
+
new CustomEvent("moduleraid:webpack-push", { detail: args }),
|
|
221
|
+
);
|
|
222
|
+
return result;
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Method to try autodetecting a Webpack JSONP entrypoint based on common naming
|
|
227
|
+
*
|
|
228
|
+
* If the default entrypoint, or the entrypoint that's passed to the moduleRaid constructor
|
|
229
|
+
* already matches, the method exits early
|
|
230
|
+
*
|
|
231
|
+
* If `options.strict` has been set in the constructor and the initial entrypoint cannot
|
|
232
|
+
* be found, this method will error, demanding a strictly set entrypoint
|
|
233
|
+
* @internal
|
|
234
|
+
*/
|
|
235
|
+
detectEntrypoint() {
|
|
236
|
+
if (this.target[this.entrypoint] != undefined) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (this.strict) {
|
|
240
|
+
throw Error(
|
|
241
|
+
`Strict mode is enabled and entrypoint at window.${this.entrypoint} couldn't be found. Please specify the correct one!`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
let windowObjects = Object.keys(this.target);
|
|
245
|
+
windowObjects = windowObjects
|
|
246
|
+
.filter(
|
|
247
|
+
(object) =>
|
|
248
|
+
object.toLowerCase().includes("chunk") ||
|
|
249
|
+
object.toLowerCase().includes("webpack"),
|
|
250
|
+
)
|
|
251
|
+
.filter(
|
|
252
|
+
(object) =>
|
|
253
|
+
typeof this.target[object] === "function" ||
|
|
254
|
+
Array.isArray(this.target[object]),
|
|
255
|
+
);
|
|
256
|
+
if (windowObjects.length > 1) {
|
|
257
|
+
throw Error(
|
|
258
|
+
`Multiple possible endpoints have been detected, please create a new moduleRaid instance with a specific one:\n${windowObjects.join(", ")}`,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
if (windowObjects.length === 0) {
|
|
262
|
+
throw Error("No Webpack JSONP entrypoints could be detected");
|
|
263
|
+
}
|
|
264
|
+
this.log(
|
|
265
|
+
`Entrypoint has been detected at window.${windowObjects[0]} and set for injection`,
|
|
266
|
+
);
|
|
267
|
+
this.entrypoint = windowObjects[0];
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Recursive object-search function for modules
|
|
271
|
+
*
|
|
272
|
+
* @param object the object to search through
|
|
273
|
+
* @param query the query the object keys/values are searched for
|
|
274
|
+
* @returns boolean state of `object` containing `query` somewhere in it
|
|
275
|
+
* @internal
|
|
276
|
+
*/
|
|
277
|
+
searchObject(object, query) {
|
|
278
|
+
for (const key in object) {
|
|
279
|
+
const value = object[key];
|
|
280
|
+
const lowerCaseQuery = query.toLowerCase();
|
|
281
|
+
if (typeof value != "object") {
|
|
282
|
+
const lowerCaseKey = key.toString().toLowerCase();
|
|
283
|
+
if (lowerCaseKey.includes(lowerCaseQuery)) return true;
|
|
284
|
+
if (typeof value != "object") {
|
|
285
|
+
const lowerCaseValue = value.toString().toLowerCase();
|
|
286
|
+
if (lowerCaseValue.includes(lowerCaseQuery)) return true;
|
|
287
|
+
} else {
|
|
288
|
+
if (this.searchObject(value, query)) return true;
|
|
301
289
|
}
|
|
302
|
-
|
|
303
|
-
const module = this.modules[key.toString()];
|
|
304
|
-
if (module === undefined)
|
|
305
|
-
return;
|
|
306
|
-
try {
|
|
307
|
-
if (typeof query === 'string') {
|
|
308
|
-
query = query.toLowerCase();
|
|
309
|
-
switch (typeof module) {
|
|
310
|
-
case 'string':
|
|
311
|
-
if (module.toLowerCase().includes(query))
|
|
312
|
-
results.push(module);
|
|
313
|
-
break;
|
|
314
|
-
case 'function':
|
|
315
|
-
if (module.toString().toLowerCase().includes(query))
|
|
316
|
-
results.push(module);
|
|
317
|
-
break;
|
|
318
|
-
case 'object':
|
|
319
|
-
if (this.searchObject(module, query))
|
|
320
|
-
results.push(module);
|
|
321
|
-
break;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
else if (typeof query === 'function') {
|
|
325
|
-
if (query(module))
|
|
326
|
-
results.push(module);
|
|
327
|
-
}
|
|
328
|
-
else {
|
|
329
|
-
throw new TypeError(`findModule can only find via string and function, ${typeof query} was passed`);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
catch (err) {
|
|
333
|
-
this.log(`There was an error while searching through module '${key}':\n${err}\n${err.stack}`);
|
|
334
|
-
}
|
|
335
|
-
});
|
|
336
|
-
return results;
|
|
290
|
+
}
|
|
337
291
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Method to search through the module object, searching for the fitting content
|
|
296
|
+
* if a string is supplied
|
|
297
|
+
*
|
|
298
|
+
* If query is supplied as a function, everything that returns true when passed
|
|
299
|
+
* to the query function will be returned
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* With a string as query argument:
|
|
303
|
+
* ```ts
|
|
304
|
+
* const results = mR.findModule('feature')
|
|
305
|
+
* // => Array of module results
|
|
306
|
+
* ```
|
|
307
|
+
*
|
|
308
|
+
* With a function as query argument:
|
|
309
|
+
* ```ts
|
|
310
|
+
* const results = mR.findModule((module) => { typeof module === 'function' })
|
|
311
|
+
* // => Array of module results
|
|
312
|
+
* ```
|
|
313
|
+
*
|
|
314
|
+
* @param query query to search the module list for
|
|
315
|
+
* @return a list of modules fitting the query
|
|
316
|
+
*/
|
|
317
|
+
findModule(query) {
|
|
318
|
+
const results = [];
|
|
319
|
+
const modules = Object.keys(this.modules);
|
|
320
|
+
if (modules.length === 0) {
|
|
321
|
+
throw new Error("There are no modules to search through!");
|
|
322
|
+
}
|
|
323
|
+
modules.forEach((key) => {
|
|
324
|
+
const module = this.modules[key.toString()];
|
|
325
|
+
if (module === undefined) return;
|
|
326
|
+
try {
|
|
327
|
+
if (typeof query === "string") {
|
|
328
|
+
query = query.toLowerCase();
|
|
329
|
+
switch (typeof module) {
|
|
330
|
+
case "string":
|
|
331
|
+
if (module.toLowerCase().includes(query)) results.push(module);
|
|
332
|
+
break;
|
|
333
|
+
case "function":
|
|
334
|
+
if (module.toString().toLowerCase().includes(query))
|
|
335
|
+
results.push(module);
|
|
336
|
+
break;
|
|
337
|
+
case "object":
|
|
338
|
+
if (this.searchObject(module, query)) results.push(module);
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
} else if (typeof query === "function") {
|
|
342
|
+
if (query(module)) results.push(module);
|
|
343
|
+
} else {
|
|
344
|
+
throw new TypeError(
|
|
345
|
+
`findModule can only find via string and function, ${typeof query} was passed`,
|
|
346
|
+
);
|
|
378
347
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
348
|
+
} catch (err) {
|
|
349
|
+
this.log(
|
|
350
|
+
`There was an error while searching through module '${key}':\n${err}\n${err.stack}`,
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
return results;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Method to search through the constructor array, searching for the fitting content
|
|
358
|
+
* if a string is supplied
|
|
359
|
+
*
|
|
360
|
+
* If query is supplied as a function, everything that returns true when passed
|
|
361
|
+
* to the query function will be returned
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* With a string as query argument:
|
|
365
|
+
* ```ts
|
|
366
|
+
* const results = mR.findConstructor('feature')
|
|
367
|
+
* // => Array of constructor/module tuples
|
|
368
|
+
* ```
|
|
369
|
+
*
|
|
370
|
+
* With a function as query argument:
|
|
371
|
+
* ```ts
|
|
372
|
+
* const results = mR.findConstructor((constructor) => { constructor.prototype.value !== undefined })
|
|
373
|
+
* // => Array of constructor/module tuples
|
|
374
|
+
* ```
|
|
375
|
+
*
|
|
376
|
+
* Accessing the resulting data:
|
|
377
|
+
* ```ts
|
|
378
|
+
* // With array destructuring (ES6)
|
|
379
|
+
* const [constructor, module] = results[0]
|
|
380
|
+
*
|
|
381
|
+
* // ...or...
|
|
382
|
+
*
|
|
383
|
+
* // regular access
|
|
384
|
+
* const constructor = results[0][0]
|
|
385
|
+
* const module = results[0][1]
|
|
386
|
+
* ```
|
|
387
|
+
*
|
|
388
|
+
* @param query query to search the constructor list for
|
|
389
|
+
* @returns a list of constructor/module tuples fitting the query
|
|
390
|
+
*/
|
|
391
|
+
findConstructor(query) {
|
|
392
|
+
const results = [];
|
|
393
|
+
const constructors = Object.keys(this.constructors);
|
|
394
|
+
if (constructors.length === 0) {
|
|
395
|
+
throw new Error("There are no constructors to search through!");
|
|
397
396
|
}
|
|
398
|
-
|
|
397
|
+
constructors.forEach((key) => {
|
|
398
|
+
const constructor = this.constructors[key];
|
|
399
|
+
try {
|
|
400
|
+
if (typeof query === "string") {
|
|
401
|
+
query = query.toLowerCase();
|
|
402
|
+
if (constructor.toString().toLowerCase().includes(query))
|
|
403
|
+
results.push([this.constructors[key], this.modules[key]]);
|
|
404
|
+
} else if (typeof query === "function") {
|
|
405
|
+
if (query(constructor))
|
|
406
|
+
results.push([this.constructors[key], this.modules[key]]);
|
|
407
|
+
}
|
|
408
|
+
} catch (err) {
|
|
409
|
+
this.log(
|
|
410
|
+
`There was an error while searching through constructor '${key}':\n${err}\n${err.stack}`,
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
return results;
|
|
415
|
+
}
|
|
416
|
+
}
|