-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.js
419 lines (417 loc) · 15.1 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
const LimitPromise = function (max) {
this._max = max;
this._count = 0;
this._taskQueue = [];
};
LimitPromise.prototype.call = function (caller, ...args) {
return new Promise((resolve, reject) => {
const task = this._createTask(caller, args, resolve, reject);
if (this._count >= this._max) {
this._taskQueue.push(task);
} else {
task();
}
});
};
LimitPromise.prototype._createTask = function (caller, args, resolve, reject) {
return () => {
caller(...args)
.then(resolve)
.catch(reject)
.finally(() => {
this._count--;
if (this._taskQueue.length) {
// console.log('a task run over, pop a task to run')
let task = this._taskQueue.shift();
task();
} else {
// console.log('task count = ', count)
}
});
this._count++;
// console.log('task run , task count = ', count)
};
};
const limitP = new LimitPromise(128);
function extname(url) {
if (url.indexOf('data:') === 0) {
const mime = url.match(/data:([^;]+)/)[1];
return mime.split('/')[1];
}
return url.split('.').pop();
}
function basename(url) {
if (url.indexOf('data:') === 0) {
return '';
}
return url.split('/').pop();
}
function createWebpackRequire(modules, base = '') {
const installedModules = {};
function __webpack_require__(moduleId) {
if (installedModules[moduleId]) return installedModules[moduleId].exports;
var module = (installedModules[moduleId] = {
exports: {},
id: moduleId,
loaded: false,
});
if (!modules[moduleId]) return '';
modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
module.loaded = true;
return module.exports;
}
__webpack_require__.r = function (exports) {
if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
}
Object.defineProperty(exports, '__esModule', { value: true });
};
__webpack_require__.o = function (object, property) {
return Object.prototype.hasOwnProperty.call(object, property);
};
__webpack_require__.d = function (exports, name, getter) {
if (!getter) {
const definition = name;
for (var key in definition) {
if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
Object.defineProperty(exports, key, {
enumerable: true,
get: definition[key],
});
}
}
return;
}
if (!__webpack_require__.o(exports, name)) {
Object.defineProperty(exports, name, { enumerable: true, get: getter });
}
};
__webpack_require__.c = installedModules;
__webpack_require__.p = base;
return __webpack_require__;
}
function extractSpine(modules, url = '', w = window) {
const maybeFuncs = [];
Object.keys(modules).forEach((k) => {
const e = modules[k];
const et = e.toString();
if (et.includes('atlas:') && et.includes('json:')) maybeFuncs.push(k);
});
console.log('[extractSpine] Detected Top-Level Modules:', maybeFuncs);
console.log(modules);
const webpackRequire = createWebpackRequire(modules, url);
const insideModules = [];
w.Object._defineProperty = Object.defineProperty;
w.Object.defineProperty = (module, __esmodule, value) => {
if (__esmodule === '__esModule') {
insideModules.push(module);
}
return w.Object._defineProperty(module, __esmodule, value);
};
const globalThis = window;
const maybeModules = maybeFuncs.map((e) => webpackRequire(e));
w.Object.defineProperty = Object._defineProperty;
const spines = [];
const mains = [];
console.log('Detected Sub-Level Modules:', insideModules);
const checkva = (va, name) => {
console.log(va, name);
const vk = Array.isArray(va) ? 0 : Object.keys(va)[0];
let v = va[vk];
if (!v) return;
if (typeof v !== 'object') {
v = va;
}
if (v.atlas && v.json) {
spines.push(va);
Object.values(va).forEach((v) => {
v.module = name || v.module || '';
});
return;
}
if (v.id && v.src && v.type) {
mains.push(va);
va.forEach((v) => {
v.module = name || v.module || '';
});
}
};
insideModules.forEach((e) => {
const ek = Object.keys(e);
ek.forEach((k) => {
if (k.includes && k.includes('_MANIFEST')) {
const obj = e[k];
const name = k.replace('_MANIFEST', '');
if (Array.isArray(obj)) {
checkva(obj, '_' + name);
} else if (Object.values(obj)[0].atlas) {
checkva(obj, name);
} else {
Object.values(obj).forEach((e) => checkva(e, name));
}
}
});
});
let mains_arr = mains.reduce((b, a) => a.concat(b), []);
mains_arr = mains_arr.filter((e) => {
if (mains_arr.find((p) => e.src === p.src) && e.module.startsWith('_')) {
return false;
}
return true;
});
return {
SPINE_MANIFEST: spines.reduce((b, a) => Object.assign(a, b), {}),
MAIN_MANIFEST: mains_arr,
};
}
function extractStaticFiles(modules, base) {
const matches = [];
Object.keys(modules).forEach((k) => {
const e = modules[k];
const et = e.toString();
const match = et.match(/[a-zA-Z0-9]\.exports\s?=\s?([a-zA-Z0-9]\.[a-zA-Z0-9]\s?\+)?\s?"(.*?)"/);
if (match) {
const url = match[2];
if (!url.startsWith('data:') && !match[1]) {
return;
}
let bname = basename(url);
if (bname) {
const a = bname.split('.');
a.pop(); // remove extension
if (a.length >= 2) {
// remove webpack hash
a.pop();
}
bname = a.join('.');
} else {
bname = k.replace(/\//g, '_').replace(/\./g, '_').replace(/\:/g, '_').replace(/\+/g, '_');
}
matches.push({
id: bname,
src: url.includes('data:') ? url : new URL(url, base).toString(),
_module: k,
});
}
});
return matches;
}
async function fetchToZip_(name, url) {
const res = await fetch(url);
const stream = () => res.body;
return {
name,
stream,
};
}
async function fetchToZip(name, url) {
return limitP.call(fetchToZip_, name, url);
}
async function loadPageInIframe(url) {
// fetch url and load by srcdoc
const response = await fetch(url);
let html = await response.text();
if (html.includes('webpackJsonp')) {
html = html.replace(new RegExp(`<script type="text/javascript">`, 'g'), `<script type="text/dontexecute">`);
} else {
let entrName = '';
if (html.includes('Symbol.toStringTag') && html.includes('Object.defineProperty')) {
// modified webpackjsonp name in html
// parse to dom
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const script = doc.querySelectorAll('script');
script.forEach((s) => {
if (s.src.includes('sentry') || s.textContent.includes('Sentry') || s.textContent.includes('firebase'))
s.type = 'text/dontexecute';
if (s.textContent.includes('Symbol.toStringTag') && s.textContent.includes('Object.defineProperty')) {
s.type = 'text/dontexecute';
const matches = [...s.textContent.matchAll(/self\.(.*?)=self.(.*?)\|\|\[\]/g)];
for (const match of matches) {
if (match[1] == match[2]) {
if (entrName != '') {
alert(`Warning: Multiple entry points found: ${entrName} and ${match[1]}`);
}
entrName = match[1];
}
}
}
});
html = doc.documentElement.outerHTML;
}
html = html.replace(
`</head>`,
`\<script\>
window.webpackJsonp_ = [];
window.cachedModules = [];
window.loadedModules = [];
window.webpackJsonpProxy = new Proxy(webpackJsonp_, {
get: (target, prop) => {
if (prop === 'push') {
return (...args) => {
console.log(args);
cachedModules.push(...args);
};
}
if (prop in target) {
return target[prop];
}
return undefined;
},
set: (target, prop, value) => {
if (prop === 'push') {
value(['inject',{
inject(module, exports, __webpack_require__){
loadedModules = __webpack_require__.m
}
},[['inject']]])
console.log('set', prop, value);
return true;
}
target[prop] = value;
return true;
},
});
Object.defineProperty(window, '${entrName || 'webpackJsonp'}', {
value: webpackJsonpProxy,
writable: true,
enumerable: false,
configurable: false,
});\</script\>`,
);
}
let base = url;
const matchVendors = html.match(/src="([^"]*?\/)vendors([^"]*?)js"/);
console.log(matchVendors);
if (matchVendors) {
base = matchVendors[1];
}
if (!base.includes('://')) {
base = new URL(base, url).toString();
}
html = html.replace('<head>', `<head><base href="${base}">`);
const iframe = document.createElement('iframe');
iframe.srcdoc = html;
iframe.style.display = 'none';
document.body.appendChild(iframe);
return new Promise((resolve) => {
iframe.onload = () => {
resolve({ iframe, base });
};
});
}
const dirname = (path) => {
const a = path.split('/');
a.pop();
return a.join('/');
};
async function extract(url) {
btn.innerText = 'Fetching Page...';
const { iframe: frame, base } = await loadPageInIframe(url);
frame.contentWindow.regeneratorRuntime = regeneratorRuntime;
btn.innerText = 'Extracting Data...';
let modules = {};
if (frame.contentWindow.cachedModules) {
modules = {
...frame.contentWindow.loadedModules,
};
for (const i of frame.contentWindow.cachedModules) {
modules = {
...modules,
...i[1],
};
}
} else {
const webpackJsonp = frame.contentWindow.webpackJsonp;
console.log('found WebpackJsonp', webpackJsonp);
const vendors = webpackJsonp.find((e) => e[0].includes('vendors'));
if (!vendors) {
btn.innerText = 'Load vendors.js faild!';
return;
}
const Index = webpackJsonp.find((e) => e[0].includes('index'));
if (!Index) {
btn.innerText = 'Load index.js faild!';
return;
}
const Runtime = webpackJsonp.find((e) => e[0].includes('runtime'));
modules = { ...vendors[1], ...Index[1], ...(Runtime ? Runtime[1] : {}) };
}
const spineres = extractSpine(modules, new URL('.', base).toString(), frame.contentWindow);
console.log('Got Spine Data', spineres);
const staticres = extractStaticFiles(modules, new URL('.', base).toString());
console.log('Got Static Files', staticres);
btn.innerText = 'Preparing resources...';
const fn = (url.match(/event\/(.*?)\//) || ['', ''])[1].split('-')[0] || Date.now().toString();
const fileStream = streamSaver.createWriteStream(fn + '.zip');
const readableZipStream = new ZIP({
async start(ctrl) {
btn.innerText = 'Download started...';
const savedIds = [];
// save spine json & atlas
for (const i of Object.keys(spineres.SPINE_MANIFEST)) {
const dir = spineres.SPINE_MANIFEST[i].module || '';
const atlas = new File([spineres.SPINE_MANIFEST[i].atlas], dir + '/' + i + '.atlas', {
type: 'text/plain',
});
ctrl.enqueue(atlas);
const j = spineres.SPINE_MANIFEST[i].json;
if (typeof j === 'string' && j.indexOf('http') === 0) {
savedIds.push(j);
ctrl.enqueue(await fetchToZip(dir + '/' + i + '.json', j));
} else {
const json = new File([JSON.stringify(j, null, 4)], dir + '/' + i + '.json', {
type: 'application/json',
});
ctrl.enqueue(json);
}
}
// save images
const promises = Object.values(spineres.MAIN_MANIFEST).map((e) => {
//skip things in savedIds
if (savedIds.includes(e.src)) {
return Promise.resolve();
}
const dir = e.module || '';
const fn = dir + '/' + e.id + '.' + extname(e.src);
savedIds.push(e.src);
return fetchToZip(fn, e.src).then((res) => ctrl.enqueue(res));
});
// save other static
let otherlen = 0;
const staticPromises = staticres.map((e) => {
//skip things in savedIds
if (savedIds.includes(e.src)) {
return Promise.resolve();
}
const dir = '_other_resources';
const fn = dir + '/' + e.id + '.' + extname(e.src);
savedIds.push(e.src);
otherlen++;
return fetchToZip(fn, e.src).then((res) => ctrl.enqueue(res));
});
desc.innerText =
`Extracted ${Object.keys(spineres.SPINE_MANIFEST).length} spine(s), ` +
`${Object.keys(spineres.MAIN_MANIFEST).length} render-related image(s), ` +
`${otherlen} other resource(s)`;
await Promise.all(promises.concat(staticPromises));
ctrl.close();
},
});
if (window.WritableStream && readableZipStream.pipeTo) {
await readableZipStream.pipeTo(fileStream);
btn.innerText = 'Done';
} else {
btn.innerText = 'FileWriter Unsupported!';
}
}
async function clk() {
btn.disabled = true;
try {
await extract(url.value);
} catch (e) {
console.error(e);
btn.innerText = 'Error!';
}
btn.disabled = false;
}