-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
autofetcher.ts
345 lines (272 loc) · 8.51 KB
/
autofetcher.ts
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
// AutoFetcher script
// extract and fetch all urls from
// - srcsets, from images as well as audio/video
// - media query stylesheets that have not necessarily been loaded (may not work for cross-origin stylesheets)
// - any data-* attribute
import { querySelectorAllDeep } from "query-selector-shadow-dom";
import { BackgroundBehavior } from "./lib/behavior";
import { sleep, xpathNodes } from "./lib/utils";
const SRC_SET_SELECTOR = "img[srcset], img[data-srcset], img[data-src], noscript > img[src], img[loading='lazy'], " +
"video[srcset], video[data-srcset], video[data-src], audio[srcset], audio[data-srcset], audio[data-src], " +
"picture > source[srcset], picture > source[data-srcset], picture > source[data-src], " +
"video > source[srcset], video > source[data-srcset], video > source[data-src], " +
"audio > source[srcset], audio > source[data-srcset], audio > source[data-src]";
const SRCSET_REGEX = /\s*(\S*\s+[\d.]+[wx]),|(?:\s*,(?:\s+|(?=https?:)))/;
const STYLE_REGEX = /(url\s*\(\s*[\\"']*)([^)'"]+)([\\"']*\s*\))/gi;
const IMPORT_REGEX = /(@import\s*[\\"']*)([^)'";]+)([\\"']*\s*;?)/gi;
const MAX_CONCURRENT = 6;
// ===========================================================================
export class AutoFetcher extends BackgroundBehavior {
urlSet: Set<string> = new Set();
pendingQueue: string[] = [];
waitQueue: string[] = [];
mutationObserver: MutationObserver;
numPending: number = 0;
numDone: number = 0;
headers: object;
_donePromise: Promise<null>;
_markDone: (value: any) => void;
active: boolean;
running = false;
static id = "AutoFetcher";
constructor(active = false, headers = null, startEarly = false) {
super();
this.headers = headers || {};
this._donePromise = new Promise((resolve) => this._markDone = resolve);
this.active = active;
if (this.active && startEarly) {
document.addEventListener("DOMContentLoaded", () => this.initObserver());
}
}
get numFetching() {
return this.numDone + this.numPending + this.pendingQueue.length;
}
async start() {
if (!this.active) {
return;
}
this.initObserver();
this.run();
sleep(500).then(() => {
if (!this.pendingQueue.length && !this.numPending) {
this._markDone(null);
}
});
}
done() {
return this._donePromise;
}
async run() {
this.running = true;
for (const url of this.waitQueue) {
this.doFetch(url);
}
this.waitQueue = [];
this.extractSrcSrcSetAll(document);
this.extractStyleSheets();
this.extractDataAttributes(document);
}
isValidUrl(url: string) {
return url && (url.startsWith("http:") || url.startsWith("https:"));
}
queueUrl(url: string, immediate: boolean = false) {
try {
url = new URL(url, document.baseURI).href;
} catch (e) {
return false;
}
if (!this.isValidUrl(url)) {
return false;
}
if (this.urlSet.has(url)) {
return false;
}
this.urlSet.add(url);
if (this.running || immediate) {
this.doFetch(url);
} else {
this.waitQueue.push(url);
}
return true;
}
// fetch with default CORS mode, read entire stream
async doFetchStream(url: string) {
try {
const resp = await fetch(url, { "credentials": "include", "referrerPolicy": "origin-when-cross-origin" });
this.debug(`Autofetch: started ${url}`);
const reader = resp.body.getReader();
let res = null;
while ((res = await reader.read()) && !res.done);
this.debug(`Autofetch: finished ${url}`);
return true;
} catch (e) {
this.debug(e);
return false;
}
}
// start non-cors fetch, abort immediately (assumes full streaming by backend)
async doFetchNonCors(url: string) {
try {
const abort = new AbortController();
await fetch(url, {
"mode": "no-cors",
"credentials": "include",
"referrerPolicy": "origin-when-cross-origin",
"headers": this.headers,
abort
} as {});
abort.abort();
this.debug(`Autofetch: started non-cors stream for ${url}`);
} catch (e) {
this.debug(`Autofetch: failed non-cors for ${url}`);
}
}
async doFetch(url: string) {
this.pendingQueue.push(url);
if (this.numPending <= MAX_CONCURRENT) {
while (this.pendingQueue.length > 0) {
const url = this.pendingQueue.shift();
this.numPending++;
let success = false;
// todo: option to use cors or non-cors fetch
// success = await this.doFetchNonCors();
if (!success) {
await this.doFetchNonCors(url);
}
this.numPending--;
this.numDone++;
}
if (!this.numPending) {
this._markDone(null);
}
}
}
initObserver() {
if (this.mutationObserver) {
return;
}
this.mutationObserver = new MutationObserver((changes) => this.observeChange(changes));
this.mutationObserver.observe(document.documentElement, {
characterData: false,
characterDataOldValue: false,
attributes: true,
attributeOldValue: true,
subtree: true,
childList: true,
attributeFilter: ["srcset", "loading"]
});
}
processChangedNode(target) {
switch (target.nodeType) {
case Node.ATTRIBUTE_NODE:
if (target.nodeName === "srcset") {
this.extractSrcSetAttr(target.nodeValue);
}
if (target.nodeName === "loading" && target.nodeValue === "lazy") {
const elem = target.parentNode;
if (elem.tagName === "IMG") {
elem.setAttribute("loading", "eager");
}
}
break;
case Node.TEXT_NODE:
if (target.parentNode && target.parentNode.tagName === "STYLE") {
this.extractStyleText(target.nodeValue);
}
break;
case Node.ELEMENT_NODE:
if (target.sheet) {
this.extractStyleSheet(target.sheet);
}
this.extractSrcSrcSet(target);
setTimeout(() => this.extractSrcSrcSetAll(target), 1000);
setTimeout(() => this.extractDataAttributes(target), 1000);
break;
}
}
observeChange(changes) {
for (const change of changes) {
this.processChangedNode(change.target);
if (change.type === "childList") {
for (const node of change.addedNodes) {
this.processChangedNode(node);
}
}
}
}
extractSrcSrcSetAll(root) {
const elems = querySelectorAllDeep(SRC_SET_SELECTOR, root);
for (const elem of elems) {
this.extractSrcSrcSet(elem);
}
}
extractSrcSrcSet(elem) {
if (!elem || elem.nodeType !== Node.ELEMENT_NODE) {
console.warn("No elem to extract from");
return;
}
const data_src = elem.getAttribute("data-src");
if (data_src) {
this.queueUrl(data_src);
}
// force lazy loading to eager
if (elem.getAttribute("loading") === "lazy") {
elem.setAttribute("loading", "eager");
}
const srcset = elem.getAttribute("srcset");
if (srcset) {
this.extractSrcSetAttr(srcset);
}
const data_srcset = elem.getAttribute("data-srcset");
if (data_srcset) {
this.extractSrcSetAttr(data_srcset);
}
// check regular src in case of <noscript> only to avoid duplicate loading
const src = elem.getAttribute("src");
if (src && (srcset || data_srcset || elem.parentElement.tagName === "NOSCRIPT")) {
this.queueUrl(src);
}
}
extractSrcSetAttr(srcset) {
for (const v of srcset.split(SRCSET_REGEX)) {
if (v) {
const parts = v.trim().split(" ");
this.queueUrl(parts[0]);
}
}
}
extractStyleSheets(root?) {
root = root || document;
for (const sheet of root.styleSheets) {
this.extractStyleSheet(sheet);
}
}
extractStyleSheet(sheet) {
let rules;
try {
rules = sheet.cssRules || sheet.rules;
} catch (e) {
this.debug("Can't access stylesheet");
return;
}
for (const rule of rules) {
if (rule.type === CSSRule.MEDIA_RULE) {
this.extractStyleText(rule.cssText);
}
}
}
extractStyleText(text) {
const urlExtractor = (m, n1, n2, n3) => {
this.queueUrl(n2);
return n1 + n2 + n3;
};
text.replace(STYLE_REGEX, urlExtractor).replace(IMPORT_REGEX, urlExtractor);
}
extractDataAttributes(root) {
const QUERY = "//@*[starts-with(name(), 'data-') and " +
"(starts-with(., 'http') or starts-with(., '/') or starts-with(., './') or starts-with(., '../'))]";
for (const attr of xpathNodes(QUERY, root)) {
this.queueUrl(attr.value);
}
}
}