single-file-core 1.1.76 → 1.1.78
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/.eslintrc.js +0 -0
- package/LICENSE +0 -0
- package/core/constants.js +13 -0
- package/core/helper.js +604 -596
- package/core/index.js +2234 -2231
- package/core/infobar.js +276 -272
- package/core/util.js +408 -407
- package/modules/css-fonts-alt-minifier.js +342 -342
- package/modules/css-fonts-minifier.js +376 -376
- package/modules/css-matched-rules.js +0 -0
- package/modules/css-medias-alt-minifier.js +90 -90
- package/modules/css-rules-minifier.js +0 -0
- package/modules/html-images-alt-minifier.js +0 -0
- package/modules/html-minifier.js +0 -0
- package/modules/html-serializer.js +0 -0
- package/modules/index.js +0 -0
- package/modules/template-formatter.js +0 -0
- package/package.json +18 -18
- package/processors/frame-tree/content/content-frame-tree.js +426 -424
- package/processors/hooks/content/content-hooks-frames-web.js +0 -0
- package/processors/hooks/content/content-hooks-frames.js +0 -0
- package/processors/index.js +0 -0
- package/processors/lazy/content/content-lazy-loader.js +233 -233
- package/single-file-bootstrap.js +55 -55
- package/single-file-editor-helper.js +49 -49
- package/single-file-frames.js +0 -0
- package/single-file-hooks-frames.js +0 -0
- package/single-file-infobar.js +62 -62
- package/single-file.js +101 -101
- package/vendor/css-font-property-parser.js +0 -0
- package/vendor/css-media-query-parser.js +0 -0
- package/vendor/css-minifier.js +0 -0
- package/vendor/css-tree.js +0 -0
- package/vendor/css-unescape.js +0 -0
- package/vendor/html-srcset-parser.js +0 -0
- package/vendor/index.js +0 -0
- package/vendor/mime-type-parser.js +0 -0
package/core/index.js
CHANGED
|
@@ -1,2232 +1,2235 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2010-2022 Gildas Lormeau
|
|
3
|
-
* contact : gildas.lormeau <at> gmail.com
|
|
4
|
-
*
|
|
5
|
-
* This file is part of SingleFile.
|
|
6
|
-
*
|
|
7
|
-
* The code in this file is free software: you can redistribute it and/or
|
|
8
|
-
* modify it under the terms of the GNU Affero General Public License
|
|
9
|
-
* (GNU AGPL) as published by the Free Software Foundation, either version 3
|
|
10
|
-
* of the License, or (at your option) any later version.
|
|
11
|
-
*
|
|
12
|
-
* The code in this file is distributed in the hope that it will be useful,
|
|
13
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
|
|
15
|
-
* General Public License for more details.
|
|
16
|
-
*
|
|
17
|
-
* As additional permission under GNU AGPL version 3 section 7, you may
|
|
18
|
-
* distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
|
|
19
|
-
* AGPL normally required by section 4, provided you include this license
|
|
20
|
-
* notice and a URL through which recipients can access the Corresponding
|
|
21
|
-
* Source.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/* global globalThis */
|
|
25
|
-
|
|
26
|
-
const DEBUG = false;
|
|
27
|
-
|
|
28
|
-
const Set = globalThis.Set;
|
|
29
|
-
const Map = globalThis.Map;
|
|
30
|
-
const JSON = globalThis.JSON;
|
|
31
|
-
const setTimeout = globalThis.setTimeout;
|
|
32
|
-
const clearTimeout = globalThis.clearTimeout;
|
|
33
|
-
const Image = globalThis.Image;
|
|
34
|
-
|
|
35
|
-
let util, cssTree;
|
|
36
|
-
|
|
37
|
-
export {
|
|
38
|
-
getClass
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
function getClass(...args) {
|
|
42
|
-
[util, cssTree] = args;
|
|
43
|
-
return SingleFileClass;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
class SingleFileClass {
|
|
47
|
-
constructor(options) {
|
|
48
|
-
this.options = options;
|
|
49
|
-
}
|
|
50
|
-
async run() {
|
|
51
|
-
const waitForUserScript = globalThis.
|
|
52
|
-
if (this.options.userScriptEnabled && waitForUserScript) {
|
|
53
|
-
await waitForUserScript(util.ON_BEFORE_CAPTURE_EVENT_NAME);
|
|
54
|
-
}
|
|
55
|
-
this.runner = new Runner(this.options, true);
|
|
56
|
-
await this.runner.loadPage();
|
|
57
|
-
await this.runner.initialize();
|
|
58
|
-
if (this.options.userScriptEnabled && waitForUserScript) {
|
|
59
|
-
await waitForUserScript(util.ON_AFTER_CAPTURE_EVENT_NAME);
|
|
60
|
-
}
|
|
61
|
-
await this.runner.run();
|
|
62
|
-
}
|
|
63
|
-
cancel() {
|
|
64
|
-
this.cancelled = true;
|
|
65
|
-
if (this.runner) {
|
|
66
|
-
this.runner.cancel();
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
getPageData() {
|
|
70
|
-
return this.runner.getPageData();
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// -------------
|
|
75
|
-
// ProgressEvent
|
|
76
|
-
// -------------
|
|
77
|
-
const PAGE_LOADING = "page-loading";
|
|
78
|
-
const PAGE_LOADED = "page-loaded";
|
|
79
|
-
const RESOURCES_INITIALIZING = "resource-initializing";
|
|
80
|
-
const RESOURCES_INITIALIZED = "resources-initialized";
|
|
81
|
-
const RESOURCE_LOADED = "resource-loaded";
|
|
82
|
-
const PAGE_ENDED = "page-ended";
|
|
83
|
-
const STAGE_STARTED = "stage-started";
|
|
84
|
-
const STAGE_ENDED = "stage-ended";
|
|
85
|
-
const STAGE_TASK_STARTED = "stage-task-started";
|
|
86
|
-
const STAGE_TASK_ENDED = "stage-task-ended";
|
|
87
|
-
|
|
88
|
-
class ProgressEvent {
|
|
89
|
-
constructor(type, detail) {
|
|
90
|
-
return { type, detail, PAGE_LOADING, PAGE_LOADED, RESOURCES_INITIALIZING, RESOURCES_INITIALIZED, RESOURCE_LOADED, PAGE_ENDED, STAGE_STARTED, STAGE_ENDED, STAGE_TASK_STARTED, STAGE_TASK_ENDED };
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// ------
|
|
95
|
-
// Runner
|
|
96
|
-
// ------
|
|
97
|
-
const RESOLVE_URLS_STAGE = 0;
|
|
98
|
-
const REPLACE_DATA_STAGE = 1;
|
|
99
|
-
const REPLACE_DOCS_STAGE = 2;
|
|
100
|
-
const POST_PROCESS_STAGE = 3;
|
|
101
|
-
const STAGES = [{
|
|
102
|
-
sequential: [
|
|
103
|
-
{ action: "preProcessPage" },
|
|
104
|
-
{ option: "loadDeferredImagesKeepZoomLevel", action: "resetZoomLevel" },
|
|
105
|
-
{ action: "replaceStyleContents" },
|
|
106
|
-
{ action: "replaceInvalidElements" },
|
|
107
|
-
{ action: "resetCharsetMeta" },
|
|
108
|
-
{ option: "saveFavicon", action: "saveFavicon" },
|
|
109
|
-
{ action: "insertFonts" },
|
|
110
|
-
{ action: "insertShadowRootContents" },
|
|
111
|
-
{ action: "replaceCanvasElements" },
|
|
112
|
-
{ action: "setInputValues" },
|
|
113
|
-
{ option: "moveStylesInHead", action: "moveStylesInHead" },
|
|
114
|
-
{ option: "blockScripts", action: "removeEmbedScripts" },
|
|
115
|
-
{ option: "selected", action: "removeUnselectedElements" },
|
|
116
|
-
{ option: "blockVideos", action: "insertVideoPosters" },
|
|
117
|
-
{ option: "blockVideos", action: "insertVideoLinks" },
|
|
118
|
-
{ option: "removeFrames", action: "removeFrames" },
|
|
119
|
-
{ action: "removeDiscardedResources" },
|
|
120
|
-
{ option: "removeHiddenElements", action: "removeHiddenElements" },
|
|
121
|
-
{ action: "saveScrollPosition" },
|
|
122
|
-
{ action: "resolveHrefs" },
|
|
123
|
-
{ action: "resolveStyleAttributeURLs" }
|
|
124
|
-
],
|
|
125
|
-
parallel: [
|
|
126
|
-
{ option: "blockVideos", action: "insertMissingVideoPosters" },
|
|
127
|
-
{ action: "resolveStylesheetURLs" },
|
|
128
|
-
{ option: "!removeFrames", action: "resolveFrameURLs" }
|
|
129
|
-
]
|
|
130
|
-
}, {
|
|
131
|
-
sequential: [
|
|
132
|
-
{ option: "removeUnusedStyles", action: "removeUnusedStyles" },
|
|
133
|
-
{ option: "removeAlternativeMedias", action: "removeAlternativeMedias" },
|
|
134
|
-
{ option: "removeUnusedFonts", action: "removeUnusedFonts" }
|
|
135
|
-
],
|
|
136
|
-
parallel: [
|
|
137
|
-
{ action: "processStylesheets" },
|
|
138
|
-
{ action: "processStyleAttributes" },
|
|
139
|
-
{ action: "processPageResources" },
|
|
140
|
-
{ action: "processScripts" }
|
|
141
|
-
]
|
|
142
|
-
}, {
|
|
143
|
-
sequential: [
|
|
144
|
-
{ option: "removeAlternativeImages", action: "removeAlternativeImages" }
|
|
145
|
-
],
|
|
146
|
-
parallel: [
|
|
147
|
-
{ option: "removeAlternativeFonts", action: "removeAlternativeFonts" },
|
|
148
|
-
{ option: "!removeFrames", action: "processFrames" }
|
|
149
|
-
]
|
|
150
|
-
}, {
|
|
151
|
-
sequential: [
|
|
152
|
-
{ action: "replaceStylesheets" },
|
|
153
|
-
{ action: "replaceStyleAttributes" },
|
|
154
|
-
{ action: "insertVariables" },
|
|
155
|
-
{ option: "compressHTML", action: "compressHTML" },
|
|
156
|
-
{ action: "cleanupPage" }
|
|
157
|
-
],
|
|
158
|
-
parallel: [
|
|
159
|
-
{ option: "enableMaff", action: "insertMAFFMetaData" },
|
|
160
|
-
{ action: "setDocInfo" }
|
|
161
|
-
]
|
|
162
|
-
}];
|
|
163
|
-
|
|
164
|
-
class Runner {
|
|
165
|
-
constructor(options, root) {
|
|
166
|
-
const rootDocDefined = root && options.doc;
|
|
167
|
-
this.root = root;
|
|
168
|
-
this.options = options;
|
|
169
|
-
this.options.url = this.options.url || (rootDocDefined && this.options.doc.location.href);
|
|
170
|
-
const matchResourceReferrer = this.options.url.match(/^.*\//);
|
|
171
|
-
this.options.resourceReferrer = this.options.passReferrerOnError && matchResourceReferrer && matchResourceReferrer[0];
|
|
172
|
-
this.options.baseURI = rootDocDefined && this.options.doc.baseURI;
|
|
173
|
-
this.options.rootDocument = root;
|
|
174
|
-
this.options.updatedResources = this.options.updatedResources || {};
|
|
175
|
-
this.options.fontTests = new Map();
|
|
176
|
-
this.batchRequest = new BatchRequest();
|
|
177
|
-
this.processor = new Processor(options, this.batchRequest);
|
|
178
|
-
if (rootDocDefined) {
|
|
179
|
-
const docData = util.preProcessDoc(this.options.doc, this.options.win, this.options);
|
|
180
|
-
this.options.canvases = docData.canvases;
|
|
181
|
-
this.options.fonts = docData.fonts;
|
|
182
|
-
this.options.stylesheets = docData.stylesheets;
|
|
183
|
-
this.options.images = docData.images;
|
|
184
|
-
this.options.posters = docData.posters;
|
|
185
|
-
this.options.videos = docData.videos;
|
|
186
|
-
this.options.usedFonts = docData.usedFonts;
|
|
187
|
-
this.options.shadowRoots = docData.shadowRoots;
|
|
188
|
-
this.options.referrer = docData.referrer;
|
|
189
|
-
this.options.adoptedStyleSheets = docData.adoptedStyleSheets;
|
|
190
|
-
this.markedElements = docData.markedElements;
|
|
191
|
-
this.invalidElements = docData.invalidElements;
|
|
192
|
-
}
|
|
193
|
-
if (this.options.saveRawPage) {
|
|
194
|
-
this.options.removeFrames = true;
|
|
195
|
-
}
|
|
196
|
-
this.options.content = this.options.content || (rootDocDefined ? util.serialize(this.options.doc) : null);
|
|
197
|
-
this.onprogress = options.onprogress || (() => { });
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
async loadPage() {
|
|
201
|
-
this.onprogress(new ProgressEvent(PAGE_LOADING, { pageURL: this.options.url, frame: !this.root }));
|
|
202
|
-
await this.processor.loadPage(this.options.content);
|
|
203
|
-
this.onprogress(new ProgressEvent(PAGE_LOADED, { pageURL: this.options.url, frame: !this.root }));
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async initialize() {
|
|
207
|
-
this.onprogress(new ProgressEvent(RESOURCES_INITIALIZING, { pageURL: this.options.url }));
|
|
208
|
-
await this.executeStage(RESOLVE_URLS_STAGE);
|
|
209
|
-
this.pendingPromises = this.executeStage(REPLACE_DATA_STAGE);
|
|
210
|
-
if (this.root && this.options.doc) {
|
|
211
|
-
util.postProcessDoc(this.options.doc, this.markedElements, this.invalidElements);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
cancel() {
|
|
216
|
-
this.cancelled = true;
|
|
217
|
-
this.batchRequest.cancel();
|
|
218
|
-
if (this.root) {
|
|
219
|
-
if (this.options.frames) {
|
|
220
|
-
this.options.frames.forEach(cancelRunner);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
function cancelRunner(resourceData) {
|
|
225
|
-
if (resourceData.runner) {
|
|
226
|
-
resourceData.runner.cancel();
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
async run() {
|
|
232
|
-
if (this.root) {
|
|
233
|
-
this.processor.initialize(this.batchRequest);
|
|
234
|
-
this.onprogress(new ProgressEvent(RESOURCES_INITIALIZED, { pageURL: this.options.url, max: this.processor.maxResources }));
|
|
235
|
-
}
|
|
236
|
-
await this.batchRequest.run(detail => {
|
|
237
|
-
detail.pageURL = this.options.url;
|
|
238
|
-
this.onprogress(new ProgressEvent(RESOURCE_LOADED, detail));
|
|
239
|
-
}, this.options);
|
|
240
|
-
await this.pendingPromises;
|
|
241
|
-
this.options.doc = null;
|
|
242
|
-
this.options.win = null;
|
|
243
|
-
await this.executeStage(REPLACE_DOCS_STAGE);
|
|
244
|
-
await this.executeStage(POST_PROCESS_STAGE);
|
|
245
|
-
this.processor.finalize();
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
getDocument() {
|
|
249
|
-
return this.processor.doc;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
getStyleSheets() {
|
|
253
|
-
return this.processor.stylesheets;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
getPageData() {
|
|
257
|
-
if (this.root) {
|
|
258
|
-
this.onprogress(new ProgressEvent(PAGE_ENDED, { pageURL: this.options.url }));
|
|
259
|
-
}
|
|
260
|
-
return this.processor.getPageData();
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
async executeStage(step) {
|
|
264
|
-
if (DEBUG) {
|
|
265
|
-
log("**** STARTED STAGE", step, "****");
|
|
266
|
-
}
|
|
267
|
-
const frame = !this.root;
|
|
268
|
-
this.onprogress(new ProgressEvent(STAGE_STARTED, { pageURL: this.options.url, step, frame }));
|
|
269
|
-
STAGES[step].sequential.forEach(task => {
|
|
270
|
-
let startTime;
|
|
271
|
-
if (DEBUG) {
|
|
272
|
-
startTime = Date.now();
|
|
273
|
-
log(" -- STARTED task =", task.action);
|
|
274
|
-
}
|
|
275
|
-
this.onprogress(new ProgressEvent(STAGE_TASK_STARTED, { pageURL: this.options.url, step, task: task.action, frame }));
|
|
276
|
-
if (!this.cancelled) {
|
|
277
|
-
this.executeTask(task);
|
|
278
|
-
}
|
|
279
|
-
this.onprogress(new ProgressEvent(STAGE_TASK_ENDED, { pageURL: this.options.url, step, task: task.action, frame }));
|
|
280
|
-
if (DEBUG) {
|
|
281
|
-
log(" -- ENDED task =", task.action, "delay =", Date.now() - startTime);
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
let parallelTasksPromise;
|
|
285
|
-
if (STAGES[step].parallel) {
|
|
286
|
-
parallelTasksPromise = await Promise.all(STAGES[step].parallel.map(async task => {
|
|
287
|
-
let startTime;
|
|
288
|
-
if (DEBUG) {
|
|
289
|
-
startTime = Date.now();
|
|
290
|
-
log(" // STARTED task =", task.action);
|
|
291
|
-
}
|
|
292
|
-
this.onprogress(new ProgressEvent(STAGE_TASK_STARTED, { pageURL: this.options.url, step, task: task.action, frame }));
|
|
293
|
-
if (!this.cancelled) {
|
|
294
|
-
await this.executeTask(task);
|
|
295
|
-
}
|
|
296
|
-
this.onprogress(new ProgressEvent(STAGE_TASK_ENDED, { pageURL: this.options.url, step, task: task.action, frame }));
|
|
297
|
-
if (DEBUG) {
|
|
298
|
-
log(" // ENDED task =", task.action, "delay =", Date.now() - startTime);
|
|
299
|
-
}
|
|
300
|
-
}));
|
|
301
|
-
} else {
|
|
302
|
-
parallelTasksPromise = Promise.resolve();
|
|
303
|
-
}
|
|
304
|
-
this.onprogress(new ProgressEvent(STAGE_ENDED, { pageURL: this.options.url, step, frame }));
|
|
305
|
-
if (DEBUG) {
|
|
306
|
-
log("**** ENDED STAGE", step, "****");
|
|
307
|
-
}
|
|
308
|
-
return parallelTasksPromise;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
executeTask(task) {
|
|
312
|
-
if (!task.option || ((task.option.startsWith("!") && !this.options[task.option]) || this.options[task.option])) {
|
|
313
|
-
return this.processor[task.action]();
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
// ------------
|
|
319
|
-
// BatchRequest
|
|
320
|
-
// ------------
|
|
321
|
-
class BatchRequest {
|
|
322
|
-
constructor() {
|
|
323
|
-
this.requests = new Map();
|
|
324
|
-
this.duplicates = new Map();
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
addURL(resourceURL, { asBinary, expectedType, groupDuplicates, baseURI, blockMixedContent } = {}) {
|
|
328
|
-
return new Promise((resolve, reject) => {
|
|
329
|
-
const requestKey = JSON.stringify([resourceURL, asBinary, expectedType, baseURI, blockMixedContent]);
|
|
330
|
-
let resourceRequests = this.requests.get(requestKey);
|
|
331
|
-
if (!resourceRequests) {
|
|
332
|
-
resourceRequests = [];
|
|
333
|
-
this.requests.set(requestKey, resourceRequests);
|
|
334
|
-
}
|
|
335
|
-
const callbacks = { resolve, reject };
|
|
336
|
-
resourceRequests.push(callbacks);
|
|
337
|
-
if (groupDuplicates) {
|
|
338
|
-
let duplicateRequests = this.duplicates.get(requestKey);
|
|
339
|
-
if (!duplicateRequests) {
|
|
340
|
-
duplicateRequests = [];
|
|
341
|
-
this.duplicates.set(requestKey, duplicateRequests);
|
|
342
|
-
}
|
|
343
|
-
duplicateRequests.push(callbacks);
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
getMaxResources() {
|
|
349
|
-
return this.requests.size;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
run(onloadListener, options) {
|
|
353
|
-
const resourceURLs = [...this.requests.keys()];
|
|
354
|
-
let indexResource = 0;
|
|
355
|
-
return Promise.all(resourceURLs.map(async requestKey => {
|
|
356
|
-
const [resourceURL, asBinary, expectedType, baseURI, blockMixedContent] = JSON.parse(requestKey);
|
|
357
|
-
const resourceRequests = this.requests.get(requestKey);
|
|
358
|
-
try {
|
|
359
|
-
const currentIndexResource = indexResource;
|
|
360
|
-
indexResource = indexResource + 1;
|
|
361
|
-
const content = await util.getContent(resourceURL, {
|
|
362
|
-
asBinary,
|
|
363
|
-
expectedType,
|
|
364
|
-
maxResourceSize: options.maxResourceSize,
|
|
365
|
-
maxResourceSizeEnabled: options.maxResourceSizeEnabled,
|
|
366
|
-
frameId: options.windowId,
|
|
367
|
-
resourceReferrer: options.resourceReferrer,
|
|
368
|
-
baseURI,
|
|
369
|
-
blockMixedContent,
|
|
370
|
-
acceptHeaders: options.acceptHeaders,
|
|
371
|
-
networkTimeout: options.networkTimeout
|
|
372
|
-
});
|
|
373
|
-
onloadListener({ url: resourceURL });
|
|
374
|
-
if (!this.cancelled) {
|
|
375
|
-
resourceRequests.forEach(callbacks => {
|
|
376
|
-
const duplicateCallbacks = this.duplicates.get(requestKey);
|
|
377
|
-
const duplicate = duplicateCallbacks && duplicateCallbacks.length > 1 && duplicateCallbacks.includes(callbacks);
|
|
378
|
-
callbacks.resolve({ content: content.data, indexResource: currentIndexResource, duplicate });
|
|
379
|
-
});
|
|
380
|
-
}
|
|
381
|
-
} catch (error) {
|
|
382
|
-
indexResource = indexResource + 1;
|
|
383
|
-
onloadListener({ url: resourceURL });
|
|
384
|
-
resourceRequests.forEach(resourceRequest => resourceRequest.reject(error));
|
|
385
|
-
}
|
|
386
|
-
this.requests.delete(requestKey);
|
|
387
|
-
}));
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
cancel() {
|
|
391
|
-
this.cancelled = true;
|
|
392
|
-
const resourceURLs = [...this.requests.keys()];
|
|
393
|
-
resourceURLs.forEach(requestKey => {
|
|
394
|
-
const resourceRequests = this.requests.get(requestKey);
|
|
395
|
-
resourceRequests.forEach(callbacks => callbacks.reject());
|
|
396
|
-
this.requests.delete(requestKey);
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
// ---------
|
|
402
|
-
// Processor
|
|
403
|
-
// ---------
|
|
404
|
-
const PREFIXES_FORBIDDEN_DATA_URI = ["data:text/"];
|
|
405
|
-
const PREFIX_DATA_URI_IMAGE_SVG = "data:image/svg+xml";
|
|
406
|
-
const SCRIPT_TAG_FOUND = /<script/gi;
|
|
407
|
-
const NOSCRIPT_TAG_FOUND = /<noscript/gi;
|
|
408
|
-
const CANVAS_TAG_FOUND = /<canvas/gi;
|
|
409
|
-
const SHADOWROOT_ATTRIBUTE_NAME = "shadowroot";
|
|
410
|
-
const SCRIPT_TEMPLATE_SHADOW_ROOT = "data-template-shadow-root";
|
|
411
|
-
const UTF8_CHARSET = "utf-8";
|
|
412
|
-
|
|
413
|
-
class Processor {
|
|
414
|
-
constructor(options, batchRequest) {
|
|
415
|
-
this.options = options;
|
|
416
|
-
this.stats = new Stats(options);
|
|
417
|
-
this.baseURI = normalizeURL(options.baseURI || options.url);
|
|
418
|
-
this.batchRequest = batchRequest;
|
|
419
|
-
this.stylesheets = new Map();
|
|
420
|
-
this.styles = new Map();
|
|
421
|
-
this.cssVariables = new Map();
|
|
422
|
-
this.fontTests = options.fontTests;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
initialize() {
|
|
426
|
-
this.options.saveDate = new Date();
|
|
427
|
-
this.options.saveUrl = this.options.url;
|
|
428
|
-
if (this.options.enableMaff) {
|
|
429
|
-
this.maffMetaDataPromise = this.batchRequest.addURL(util.resolveURL("index.rdf", this.options.baseURI || this.options.url), { expectedType: "document" });
|
|
430
|
-
}
|
|
431
|
-
this.maxResources = this.batchRequest.getMaxResources();
|
|
432
|
-
if (!this.options.saveRawPage && !this.options.removeFrames && this.options.frames) {
|
|
433
|
-
this.options.frames.forEach(frameData =>
|
|
434
|
-
}
|
|
435
|
-
this.stats.set("processed", "resources", this.maxResources);
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
async loadPage(pageContent, charset) {
|
|
439
|
-
let content;
|
|
440
|
-
if (!pageContent || this.options.saveRawPage) {
|
|
441
|
-
content = await util.getContent(this.baseURI, {
|
|
442
|
-
maxResourceSize: this.options.maxResourceSize,
|
|
443
|
-
maxResourceSizeEnabled: this.options.maxResourceSizeEnabled,
|
|
444
|
-
charset,
|
|
445
|
-
frameId: this.options.windowId,
|
|
446
|
-
resourceReferrer: this.options.resourceReferrer,
|
|
447
|
-
expectedType: "document",
|
|
448
|
-
acceptHeaders: this.options.acceptHeaders,
|
|
449
|
-
networkTimeout: this.options.networkTimeout
|
|
450
|
-
});
|
|
451
|
-
pageContent = content.data;
|
|
452
|
-
}
|
|
453
|
-
this.doc = util.parseDocContent(pageContent, this.baseURI);
|
|
454
|
-
if (this.options.saveRawPage) {
|
|
455
|
-
let charset;
|
|
456
|
-
this.doc.querySelectorAll("meta[charset], meta[http-equiv=\"content-type\"]").forEach(element => {
|
|
457
|
-
const charsetDeclaration = element.content.split(";")[1];
|
|
458
|
-
if (charsetDeclaration && !charset) {
|
|
459
|
-
charset = charsetDeclaration.split("=")[1].trim().toLowerCase();
|
|
460
|
-
}
|
|
461
|
-
});
|
|
462
|
-
if (charset && content.charset && charset.toLowerCase() != content.charset.toLowerCase()) {
|
|
463
|
-
return this.loadPage(pageContent, charset);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
this.workStyleElement = this.doc.createElement("style");
|
|
467
|
-
this.doc.body.appendChild(this.workStyleElement);
|
|
468
|
-
this.onEventAttributeNames = getOnEventAttributeNames(this.doc);
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
finalize() {
|
|
472
|
-
if (this.workStyleElement.parentNode) {
|
|
473
|
-
this.workStyleElement.remove();
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
async getPageData() {
|
|
478
|
-
util.postProcessDoc(this.doc);
|
|
479
|
-
const url = util.parseURL(this.baseURI);
|
|
480
|
-
if (this.options.insertSingleFileComment) {
|
|
481
|
-
const firstComment = this.doc.documentElement.firstChild;
|
|
482
|
-
let infobarURL = this.options.saveUrl, infobarSaveDate = this.options.saveDate;
|
|
483
|
-
if (firstComment.nodeType == 8 && (firstComment.textContent.includes(util.COMMENT_HEADER_LEGACY) || firstComment.textContent.includes(util.COMMENT_HEADER))) {
|
|
484
|
-
const info = this.doc.documentElement.firstChild.textContent.split("\n");
|
|
485
|
-
try {
|
|
486
|
-
const [, , url, saveDate] = info;
|
|
487
|
-
infobarURL = url.split("url: ")[1];
|
|
488
|
-
infobarSaveDate = saveDate.split("saved date: ")[1];
|
|
489
|
-
firstComment.remove();
|
|
490
|
-
} catch (error) {
|
|
491
|
-
// ignored
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
const infobarContent = (this.options.infobarContent || "").replace(/\\n/g, "\n").replace(/\\t/g, "\t");
|
|
495
|
-
const commentNode = this.doc.createComment(
|
|
496
|
-
"\n " + (this.options.useLegacyCommentHeader ? util.COMMENT_HEADER_LEGACY : util.COMMENT_HEADER) +
|
|
497
|
-
" \n url: " + infobarURL +
|
|
498
|
-
(this.options.removeSavedDate ? " " : " \n saved date: " + infobarSaveDate) +
|
|
499
|
-
(infobarContent ? " \n info: " + infobarContent : "") + "\n"
|
|
500
|
-
);
|
|
501
|
-
this.doc.documentElement.insertBefore(commentNode, this.doc.documentElement.firstChild);
|
|
502
|
-
}
|
|
503
|
-
const legacyInfobarElement = this.doc.querySelector("singlefile-infobar");
|
|
504
|
-
if (legacyInfobarElement) {
|
|
505
|
-
legacyInfobarElement.remove();
|
|
506
|
-
}
|
|
507
|
-
const infobarElement = this.doc.querySelector(util.INFOBAR_TAGNAME);
|
|
508
|
-
if (infobarElement) {
|
|
509
|
-
infobarElement.remove();
|
|
510
|
-
}
|
|
511
|
-
if (this.options.includeInfobar) {
|
|
512
|
-
util.appendInfobar(this.doc, this.options);
|
|
513
|
-
}
|
|
514
|
-
if (this.doc.querySelector("template[" + SHADOWROOT_ATTRIBUTE_NAME + "]") || (this.options.shadowRoots && this.options.shadowRoots.length)) {
|
|
515
|
-
if (this.options.blockScripts) {
|
|
516
|
-
this.doc.querySelectorAll("script[" + SCRIPT_TEMPLATE_SHADOW_ROOT + "]").forEach(element => element.remove());
|
|
517
|
-
}
|
|
518
|
-
const scriptElement = this.doc.createElement("script");
|
|
519
|
-
scriptElement.setAttribute(SCRIPT_TEMPLATE_SHADOW_ROOT, "");
|
|
520
|
-
scriptElement.textContent = `(()=>{document.currentScript.remove();processNode(document);function processNode(node){node.querySelectorAll("template[${SHADOWROOT_ATTRIBUTE_NAME}]").forEach(element=>{let shadowRoot = element.parentElement.shadowRoot;if (!shadowRoot) {try {shadowRoot=element.parentElement.attachShadow({mode:element.getAttribute("${SHADOWROOT_ATTRIBUTE_NAME}")});shadowRoot.innerHTML=element.innerHTML;element.remove()} catch (error) {} if (shadowRoot) {processNode(shadowRoot)}}})}})()`;
|
|
521
|
-
this.doc.body.appendChild(scriptElement);
|
|
522
|
-
}
|
|
523
|
-
if (this.options.insertCanonicalLink && this.options.saveUrl.match(HTTP_URI_PREFIX)) {
|
|
524
|
-
let canonicalLink = this.doc.querySelector("link[rel=canonical]");
|
|
525
|
-
if (!canonicalLink) {
|
|
526
|
-
canonicalLink = this.doc.createElement("link");
|
|
527
|
-
canonicalLink.setAttribute("rel", "canonical");
|
|
528
|
-
this.doc.head.appendChild(canonicalLink);
|
|
529
|
-
}
|
|
530
|
-
if (canonicalLink && !canonicalLink.href) {
|
|
531
|
-
canonicalLink.href = this.options.saveUrl;
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
if (this.options.insertMetaCSP) {
|
|
535
|
-
const metaTag = this.doc.createElement("meta");
|
|
536
|
-
metaTag.httpEquiv = "content-security-policy";
|
|
537
|
-
metaTag.content = "default-src 'none'; font-src 'self' data:; img-src 'self' data:; style-src 'unsafe-inline'; media-src 'self' data:; script-src 'unsafe-inline' data:; object-src 'self' data:; frame-src 'self' data:;";
|
|
538
|
-
this.doc.head.appendChild(metaTag);
|
|
539
|
-
}
|
|
540
|
-
if (this.options.insertMetaNoIndex) {
|
|
541
|
-
let metaElement = this.doc.querySelector("meta[name=robots][content*=noindex]");
|
|
542
|
-
if (!metaElement) {
|
|
543
|
-
metaElement = this.doc.createElement("meta");
|
|
544
|
-
metaElement.setAttribute("name", "robots");
|
|
545
|
-
metaElement.setAttribute("content", "noindex");
|
|
546
|
-
this.doc.head.appendChild(metaElement);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
const styleElement = this.doc.createElement("style");
|
|
550
|
-
styleElement.textContent = "img[src=\"data:,\"],source[src=\"data:,\"]{display:none!important}";
|
|
551
|
-
this.doc.head.appendChild(styleElement);
|
|
552
|
-
let size;
|
|
553
|
-
if (this.options.displayStats) {
|
|
554
|
-
size = util.getContentSize(this.doc.documentElement.outerHTML);
|
|
555
|
-
}
|
|
556
|
-
const content = util.serialize(this.doc, this.options.compressHTML);
|
|
557
|
-
if (this.options.displayStats) {
|
|
558
|
-
const contentSize = util.getContentSize(content);
|
|
559
|
-
this.stats.set("processed", "HTML bytes", contentSize);
|
|
560
|
-
this.stats.add("discarded", "HTML bytes", size - contentSize);
|
|
561
|
-
}
|
|
562
|
-
const filename = await util.formatFilename(content, this.options);
|
|
563
|
-
const matchTitle = this.baseURI.match(/([^/]*)\/?(\.html?.*)$/) || this.baseURI.match(/\/\/([^/]*)\/?$/);
|
|
564
|
-
const pageData = {
|
|
565
|
-
stats: this.stats.data,
|
|
566
|
-
title: this.options.title || (this.baseURI && matchTitle ? matchTitle[1] : url.hostname ? url.hostname : ""),
|
|
567
|
-
filename,
|
|
568
|
-
content
|
|
569
|
-
};
|
|
570
|
-
if (this.options.addProof) {
|
|
571
|
-
pageData.hash = await util.digest("SHA-256", content);
|
|
572
|
-
}
|
|
573
|
-
if (this.options.retrieveLinks) {
|
|
574
|
-
pageData.links = Array.from(new Set(Array.from(this.doc.links).map(linkElement => linkElement.href)));
|
|
575
|
-
}
|
|
576
|
-
return pageData;
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
preProcessPage() {
|
|
580
|
-
if (this.options.win) {
|
|
581
|
-
this.doc.body.querySelectorAll(":not(svg) title, meta, link[href][rel*=\"icon\"]").forEach(element => element instanceof this.options.win.HTMLElement && this.doc.head.appendChild(element));
|
|
582
|
-
}
|
|
583
|
-
if (this.options.images && !this.options.saveRawPage) {
|
|
584
|
-
this.doc.querySelectorAll("img[" + util.IMAGE_ATTRIBUTE_NAME + "]").forEach(imgElement => {
|
|
585
|
-
const attributeValue = imgElement.getAttribute(util.IMAGE_ATTRIBUTE_NAME);
|
|
586
|
-
if (attributeValue) {
|
|
587
|
-
const imageData = this.options.images[Number(attributeValue)];
|
|
588
|
-
if (imageData) {
|
|
589
|
-
if (this.options.removeHiddenElements && (
|
|
590
|
-
(imageData.size && !imageData.size.pxWidth && !imageData.size.pxHeight) ||
|
|
591
|
-
imgElement.getAttribute(util.HIDDEN_CONTENT_ATTRIBUTE_NAME) == "")
|
|
592
|
-
) {
|
|
593
|
-
imgElement.setAttribute("src", util.EMPTY_RESOURCE);
|
|
594
|
-
} else {
|
|
595
|
-
if (imageData.currentSrc) {
|
|
596
|
-
imgElement.dataset.singleFileOriginURL = imgElement.getAttribute("src");
|
|
597
|
-
imgElement.setAttribute("src", imageData.currentSrc);
|
|
598
|
-
}
|
|
599
|
-
if (this.options.loadDeferredImages) {
|
|
600
|
-
if ((!imgElement.getAttribute("src") || imgElement.getAttribute("src") == util.EMPTY_RESOURCE) && imgElement.getAttribute("data-src")) {
|
|
601
|
-
imageData.src = imgElement.dataset.src;
|
|
602
|
-
imgElement.setAttribute("src", imgElement.dataset.src);
|
|
603
|
-
imgElement.removeAttribute("data-src");
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
});
|
|
610
|
-
if (this.options.loadDeferredImages) {
|
|
611
|
-
this.doc.querySelectorAll("img[data-srcset]").forEach(imgElement => {
|
|
612
|
-
if (!imgElement.getAttribute("srcset") && imgElement.getAttribute("data-srcset")) {
|
|
613
|
-
imgElement.setAttribute("srcset", imgElement.dataset.srcset);
|
|
614
|
-
imgElement.removeAttribute("data-srcset");
|
|
615
|
-
}
|
|
616
|
-
});
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
replaceStyleContents() {
|
|
622
|
-
if (this.options.stylesheets) {
|
|
623
|
-
this.doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
|
|
624
|
-
const attributeValue = styleElement.getAttribute(util.STYLESHEET_ATTRIBUTE_NAME);
|
|
625
|
-
if (attributeValue) {
|
|
626
|
-
const stylesheetContent = this.options.stylesheets[Number(styleIndex)];
|
|
627
|
-
if (stylesheetContent) {
|
|
628
|
-
styleElement.textContent = stylesheetContent;
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
});
|
|
632
|
-
}
|
|
633
|
-
if (this.options.adoptedStyleSheets && this.options.adoptedStyleSheets.length) {
|
|
634
|
-
const styleElement = this.doc.createElement("style");
|
|
635
|
-
styleElement.textContent = this.options.adoptedStyleSheets.join("\n");
|
|
636
|
-
this.doc.body.appendChild(styleElement);
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
removeUnselectedElements() {
|
|
641
|
-
removeUnmarkedElements(this.doc.body);
|
|
642
|
-
this.doc.body.removeAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME);
|
|
643
|
-
|
|
644
|
-
function removeUnmarkedElements(element) {
|
|
645
|
-
let selectedElementFound = false;
|
|
646
|
-
Array.from(element.childNodes).forEach(node => {
|
|
647
|
-
if (node.nodeType == 1) {
|
|
648
|
-
const isSelectedElement = node.getAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME) == "";
|
|
649
|
-
selectedElementFound = selectedElementFound || isSelectedElement;
|
|
650
|
-
if (isSelectedElement) {
|
|
651
|
-
node.removeAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME);
|
|
652
|
-
removeUnmarkedElements(node);
|
|
653
|
-
} else if (selectedElementFound) {
|
|
654
|
-
removeNode(node);
|
|
655
|
-
} else {
|
|
656
|
-
hideNode(node);
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
});
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
function removeNode(node) {
|
|
663
|
-
if ((node.nodeType != 1 || !node.querySelector("svg,style,link")) && canHideNode(node)) {
|
|
664
|
-
node.remove();
|
|
665
|
-
} else {
|
|
666
|
-
hideNode(node);
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
function hideNode(node) {
|
|
671
|
-
if (canHideNode(node)) {
|
|
672
|
-
node.style.setProperty("display", "none", "important");
|
|
673
|
-
node.removeAttribute("src");
|
|
674
|
-
node.removeAttribute("srcset");
|
|
675
|
-
node.removeAttribute("srcdoc");
|
|
676
|
-
Array.from(node.childNodes).forEach(removeNode);
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
function canHideNode(node) {
|
|
681
|
-
if (node.nodeType == 1) {
|
|
682
|
-
const tagName = node.tagName && node.tagName.toUpperCase();
|
|
683
|
-
return tagName != "SVG" && tagName != "STYLE" && tagName != "LINK";
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
insertVideoPosters() {
|
|
689
|
-
if (this.options.posters) {
|
|
690
|
-
this.doc.querySelectorAll("video, video > source").forEach(element => {
|
|
691
|
-
let videoElement;
|
|
692
|
-
if (element.tagName.toUpperCase() == "VIDEO") {
|
|
693
|
-
videoElement = element;
|
|
694
|
-
} else {
|
|
695
|
-
videoElement = element.parentElement;
|
|
696
|
-
}
|
|
697
|
-
const attributeValue = element.getAttribute(util.POSTER_ATTRIBUTE_NAME);
|
|
698
|
-
if (attributeValue) {
|
|
699
|
-
const posterURL = this.options.posters[Number(attributeValue)];
|
|
700
|
-
if (!videoElement.getAttribute("poster") && posterURL) {
|
|
701
|
-
videoElement.setAttribute("poster", posterURL);
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
});
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
insertVideoLinks() {
|
|
709
|
-
const LINK_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAgMAAADXB5lNAAABhmlDQ1BJQ0MgcHJvZmlsZQAAKJF9kj1Iw0AYht+mSkUrDnYQcchQnSyIijqWKhbBQmkrtOpgcukfNGlIUlwcBdeCgz+LVQcXZ10dXAVB8AfEydFJ0UVK/C4ptIjx4LiH9+59+e67A4RGhalm1wSgapaRisfEbG5VDLyiDwEAvZiVmKkn0osZeI6ve/j4ehfhWd7n/hz9St5kgE8kjjLdsIg3iGc2LZ3zPnGIlSSF+Jx43KACiR+5Lrv8xrnosMAzQ0YmNU8cIhaLHSx3MCsZKvE0cVhRNcoXsi4rnLc4q5Uaa9XJbxjMaytprtMcQRxLSCAJETJqKKMCCxFaNVJMpGg/5uEfdvxJcsnkKoORYwFVqJAcP/gb/O6tWZiadJOCMaD7xbY/RoHALtCs2/b3sW03TwD/M3Cltf3VBjD3SXq9rYWPgIFt4OK6rcl7wOUOMPSkS4bkSH6aQqEAvJ/RM+WAwVv6EGtu31r7OH0AMtSr5Rvg4BAYK1L2use9ezr79u+ZVv9+AFlNcp0UUpiqAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH5AsHAB8H+DhhoQAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAJUExURQAAAICHi4qKioTuJAkAAAABdFJOUwBA5thmAAAAAWJLR0QCZgt8ZAAAAJJJREFUOI3t070NRCEMA2CnYAOyDyPwpHj/Va7hJ3FzV7zy3ET5JIwoAF6Jk4wzAJAkzxAYG9YRTgB+24wBgKmfrGAKTcEfAY4KRlRoIeBTgKOCERVaCPgU4Khge2GqKOBTgKOCERVaAEC/4PNcnyoSWHpjqkhwKxbcig0Q6AorXYF/+A6eIYD1lVbwG/jdA6/kA2THRAURVubcAAAAAElFTkSuQmCC";
|
|
710
|
-
const ICON_SIZE = "16px";
|
|
711
|
-
this.doc.querySelectorAll("video").forEach(videoElement => {
|
|
712
|
-
const attributeValue = videoElement.getAttribute(util.VIDEO_ATTRIBUTE_NAME);
|
|
713
|
-
if (attributeValue) {
|
|
714
|
-
const videoData = this.options.videos[Number(attributeValue)];
|
|
715
|
-
const src = videoData.src || videoElement.src;
|
|
716
|
-
if (videoElement && src) {
|
|
717
|
-
const linkElement = this.doc.createElement("a");
|
|
718
|
-
const imgElement = this.doc.createElement("img");
|
|
719
|
-
linkElement.href = src;
|
|
720
|
-
linkElement.target = "_blank";
|
|
721
|
-
linkElement.style.setProperty("z-index", 2147483647, "important");
|
|
722
|
-
linkElement.style.setProperty("position", "absolute", "important");
|
|
723
|
-
linkElement.style.setProperty("top", "8px", "important");
|
|
724
|
-
linkElement.style.setProperty("left", "8px", "important");
|
|
725
|
-
linkElement.style.setProperty("width", ICON_SIZE, "important");
|
|
726
|
-
linkElement.style.setProperty("height", ICON_SIZE, "important");
|
|
727
|
-
linkElement.style.setProperty("min-width", ICON_SIZE, "important");
|
|
728
|
-
linkElement.style.setProperty("min-height", ICON_SIZE, "important");
|
|
729
|
-
linkElement.style.setProperty("max-width", ICON_SIZE, "important");
|
|
730
|
-
linkElement.style.setProperty("max-height", ICON_SIZE, "important");
|
|
731
|
-
imgElement.src = LINK_ICON;
|
|
732
|
-
imgElement.style.setProperty("width", ICON_SIZE, "important");
|
|
733
|
-
imgElement.style.setProperty("height", ICON_SIZE, "important");
|
|
734
|
-
imgElement.style.setProperty("min-width", ICON_SIZE, "important");
|
|
735
|
-
imgElement.style.setProperty("min-height", ICON_SIZE, "important");
|
|
736
|
-
imgElement.style.setProperty("max-width", ICON_SIZE, "important");
|
|
737
|
-
imgElement.style.setProperty("max-height", ICON_SIZE, "important");
|
|
738
|
-
linkElement.appendChild(imgElement);
|
|
739
|
-
videoElement.insertAdjacentElement("afterend", linkElement);
|
|
740
|
-
const positionInlineParent = videoElement.parentNode.style.getPropertyValue("position");
|
|
741
|
-
if ((!videoData.positionParent && (!positionInlineParent || positionInlineParent != "static")) || videoData.positionParent == "static") {
|
|
742
|
-
videoElement.parentNode.style.setProperty("position", "relative", "important");
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
});
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
removeFrames() {
|
|
750
|
-
const frameElements = this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]");
|
|
751
|
-
this.stats.set("discarded", "frames", frameElements.length);
|
|
752
|
-
this.stats.set("processed", "frames", frameElements.length);
|
|
753
|
-
this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]").forEach(element => element.remove());
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
removeEmbedScripts() {
|
|
757
|
-
const JAVASCRIPT_URI_PREFIX = "javascript:";
|
|
758
|
-
const DISABLED_SCRIPT = "javascript:void(0)";
|
|
759
|
-
this.onEventAttributeNames.forEach(attributeName => this.doc.querySelectorAll("[" + attributeName + "]").forEach(element => element.removeAttribute(attributeName)));
|
|
760
|
-
this.doc.querySelectorAll("[href]").forEach(element => {
|
|
761
|
-
if (element.href && element.href.match && element.href.trim().startsWith(JAVASCRIPT_URI_PREFIX)) {
|
|
762
|
-
element.setAttribute("href", DISABLED_SCRIPT);
|
|
763
|
-
}
|
|
764
|
-
});
|
|
765
|
-
this.doc.querySelectorAll("[src]").forEach(element => {
|
|
766
|
-
if (element.src && element.src.trim().startsWith(JAVASCRIPT_URI_PREFIX)) {
|
|
767
|
-
element.setAttribute("src", DISABLED_SCRIPT);
|
|
768
|
-
}
|
|
769
|
-
});
|
|
770
|
-
const scriptElements = this.doc.querySelectorAll("script:not([type=\"application/ld+json\"]):not([" + SCRIPT_TEMPLATE_SHADOW_ROOT + "])");
|
|
771
|
-
this.stats.set("discarded", "scripts", scriptElements.length);
|
|
772
|
-
this.stats.set("processed", "scripts", scriptElements.length);
|
|
773
|
-
scriptElements.forEach(element => element.remove());
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
removeDiscardedResources() {
|
|
777
|
-
this.doc.querySelectorAll("." + util.SINGLE_FILE_UI_ELEMENT_CLASS).forEach(element => element.remove());
|
|
778
|
-
const noscriptPlaceholders = new Map();
|
|
779
|
-
this.doc.querySelectorAll("noscript").forEach(noscriptElement => {
|
|
780
|
-
const placeholderElement = this.doc.createElement("div");
|
|
781
|
-
placeholderElement.innerHTML = noscriptElement.dataset.singleFileDisabledNoscript;
|
|
782
|
-
noscriptElement.replaceWith(placeholderElement);
|
|
783
|
-
noscriptPlaceholders.set(placeholderElement, noscriptElement);
|
|
784
|
-
});
|
|
785
|
-
this.doc.querySelectorAll("meta[http-equiv=refresh], meta[disabled-http-equiv]").forEach(element => element.remove());
|
|
786
|
-
noscriptPlaceholders.forEach((noscriptElement, placeholderElement) => {
|
|
787
|
-
noscriptElement.dataset.singleFileDisabledNoscript = placeholderElement.innerHTML;
|
|
788
|
-
placeholderElement.replaceWith(noscriptElement);
|
|
789
|
-
});
|
|
790
|
-
this.doc.querySelectorAll("meta[http-equiv=\"content-security-policy\"]").forEach(element => element.remove());
|
|
791
|
-
const objectElements = this.doc.querySelectorAll("applet, object[data]:not([type=\"image/svg+xml\"]):not([type=\"image/svg-xml\"]):not([type=\"text/html\"]):not([data*=\".svg\"]):not([data*=\".pdf\"]), embed[src]:not([src*=\".svg\"]):not([src*=\".pdf\"])");
|
|
792
|
-
this.stats.set("discarded", "objects", objectElements.length);
|
|
793
|
-
this.stats.set("processed", "objects", objectElements.length);
|
|
794
|
-
objectElements.forEach(element => element.remove());
|
|
795
|
-
const replacedAttributeValue = this.doc.querySelectorAll("link[rel~=preconnect], link[rel~=prerender], link[rel~=dns-prefetch], link[rel~=preload], link[rel~=manifest], link[rel~=prefetch]");
|
|
796
|
-
replacedAttributeValue.forEach(element => {
|
|
797
|
-
const relValue = element
|
|
798
|
-
.getAttribute("rel")
|
|
799
|
-
.replace(/(preconnect|prerender|dns-prefetch|preload|prefetch|manifest)/g, "")
|
|
800
|
-
.trim();
|
|
801
|
-
if (relValue.length) {
|
|
802
|
-
element.setAttribute("rel", relValue);
|
|
803
|
-
} else {
|
|
804
|
-
element.remove();
|
|
805
|
-
}
|
|
806
|
-
});
|
|
807
|
-
this.doc.querySelectorAll("link[rel*=stylesheet][rel*=alternate][title],link[rel*=stylesheet]:not([href]),link[rel*=stylesheet][href=\"\"]").forEach(element => element.remove());
|
|
808
|
-
if (this.options.removeHiddenElements) {
|
|
809
|
-
this.doc.querySelectorAll("input[type=hidden]").forEach(element => element.remove());
|
|
810
|
-
}
|
|
811
|
-
if (!this.options.saveFavicon) {
|
|
812
|
-
this.doc.querySelectorAll("link[rel*=\"icon\"]").forEach(element => element.remove());
|
|
813
|
-
}
|
|
814
|
-
this.doc.querySelectorAll("a[ping]").forEach(element => element.removeAttribute("ping"));
|
|
815
|
-
this.doc.querySelectorAll("link[rel=import][href]").forEach(element => element.remove());
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
replaceInvalidElements() {
|
|
819
|
-
this.doc.querySelectorAll("template[" + util.INVALID_ELEMENT_ATTRIBUTE_NAME + "]").forEach(templateElement => {
|
|
820
|
-
const placeHolderElement = this.doc.createElement("span");
|
|
821
|
-
if (templateElement.content) {
|
|
822
|
-
const originalElement = templateElement.content.firstChild;
|
|
823
|
-
if (originalElement) {
|
|
824
|
-
if (originalElement.hasAttributes()) {
|
|
825
|
-
Array.from(originalElement.attributes).forEach(attribute => placeHolderElement.setAttribute(attribute.name, attribute.value));
|
|
826
|
-
}
|
|
827
|
-
originalElement.childNodes.forEach(childNode => placeHolderElement.appendChild(childNode.cloneNode(true)));
|
|
828
|
-
}
|
|
829
|
-
templateElement.replaceWith(placeHolderElement);
|
|
830
|
-
}
|
|
831
|
-
});
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
resetCharsetMeta() {
|
|
835
|
-
let charset;
|
|
836
|
-
this.doc.querySelectorAll("meta[charset], meta[http-equiv=\"content-type\"]").forEach(element => {
|
|
837
|
-
const charsetDeclaration = element.content.split(";")[1];
|
|
838
|
-
if (charsetDeclaration && !charset) {
|
|
839
|
-
charset = charsetDeclaration.split("=")[1];
|
|
840
|
-
if (charset) {
|
|
841
|
-
this.charset = charset.trim().toLowerCase();
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
element.remove();
|
|
845
|
-
});
|
|
846
|
-
const metaElement = this.doc.createElement("meta");
|
|
847
|
-
metaElement.setAttribute("charset", UTF8_CHARSET);
|
|
848
|
-
if (this.doc.head.firstChild) {
|
|
849
|
-
this.doc.head.insertBefore(metaElement, this.doc.head.firstChild);
|
|
850
|
-
} else {
|
|
851
|
-
this.doc.head.appendChild(metaElement);
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
setInputValues() {
|
|
856
|
-
this.doc.querySelectorAll("input:not([type=radio]):not([type=checkbox])").forEach(input => {
|
|
857
|
-
const value = input.getAttribute(util.INPUT_VALUE_ATTRIBUTE_NAME);
|
|
858
|
-
input.setAttribute("value", value || "");
|
|
859
|
-
});
|
|
860
|
-
this.doc.querySelectorAll("input[type=radio], input[type=checkbox]").forEach(input => {
|
|
861
|
-
const value = input.getAttribute(util.INPUT_VALUE_ATTRIBUTE_NAME);
|
|
862
|
-
if (value == "true") {
|
|
863
|
-
input.setAttribute("checked", "");
|
|
864
|
-
}
|
|
865
|
-
});
|
|
866
|
-
this.doc.querySelectorAll("textarea").forEach(textarea => {
|
|
867
|
-
const value = textarea.getAttribute(util.INPUT_VALUE_ATTRIBUTE_NAME);
|
|
868
|
-
textarea.textContent = value || "";
|
|
869
|
-
});
|
|
870
|
-
this.doc.querySelectorAll("select").forEach(select => {
|
|
871
|
-
select.querySelectorAll("option").forEach(option => {
|
|
872
|
-
const selected = option.getAttribute(util.INPUT_VALUE_ATTRIBUTE_NAME) != null;
|
|
873
|
-
if (selected) {
|
|
874
|
-
option.setAttribute("selected", "");
|
|
875
|
-
}
|
|
876
|
-
});
|
|
877
|
-
});
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
moveStylesInHead() {
|
|
881
|
-
this.doc.querySelectorAll("style").forEach(stylesheet => {
|
|
882
|
-
if (stylesheet.getAttribute(util.STYLE_ATTRIBUTE_NAME) == "") {
|
|
883
|
-
this.doc.head.appendChild(stylesheet);
|
|
884
|
-
}
|
|
885
|
-
});
|
|
886
|
-
}
|
|
887
|
-
|
|
888
|
-
saveFavicon() {
|
|
889
|
-
let faviconElement = this.doc.querySelector("link[href][rel=\"shortcut icon\"]");
|
|
890
|
-
if (!faviconElement) {
|
|
891
|
-
faviconElement = this.doc.querySelector("link[href][rel=\"icon\"]");
|
|
892
|
-
}
|
|
893
|
-
if (!faviconElement) {
|
|
894
|
-
faviconElement = this.doc.createElement("link");
|
|
895
|
-
faviconElement.setAttribute("type", "image/x-icon");
|
|
896
|
-
faviconElement.setAttribute("rel", "shortcut icon");
|
|
897
|
-
faviconElement.setAttribute("href", "/favicon.ico");
|
|
898
|
-
}
|
|
899
|
-
this.doc.head.appendChild(faviconElement);
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
saveScrollPosition() {
|
|
903
|
-
if (this.options.scrollPosition && this.options.scrolling == "no" && (this.options.scrollPosition.x || this.options.scrollPosition.y)) {
|
|
904
|
-
const scriptElement = this.doc.createElement("script");
|
|
905
|
-
scriptElement.textContent = "document.currentScript.remove();addEventListener(\"load\",()=>scrollTo(" + this.options.scrollPosition.x + "," + this.options.scrollPosition.y + "))";
|
|
906
|
-
this.doc.body.appendChild(scriptElement);
|
|
907
|
-
}
|
|
908
|
-
}
|
|
909
|
-
|
|
910
|
-
replaceCanvasElements() {
|
|
911
|
-
if (this.options.canvases) {
|
|
912
|
-
this.doc.querySelectorAll("canvas").forEach(canvasElement => {
|
|
913
|
-
const attributeValue = canvasElement.getAttribute(util.CANVAS_ATTRIBUTE_NAME);
|
|
914
|
-
if (attributeValue) {
|
|
915
|
-
const canvasData = this.options.canvases[Number(attributeValue)];
|
|
916
|
-
if (canvasData) {
|
|
917
|
-
const backgroundStyle = {};
|
|
918
|
-
if (canvasData.backgroundColor) {
|
|
919
|
-
backgroundStyle["background-color"] = canvasData.backgroundColor;
|
|
920
|
-
}
|
|
921
|
-
ProcessorHelper.setBackgroundImage(canvasElement, "url(" + canvasData.dataURI + ")", backgroundStyle);
|
|
922
|
-
this.stats.add("processed", "canvas", 1);
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
});
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
insertFonts() {
|
|
930
|
-
if (this.options.fonts && this.options.fonts.length) {
|
|
931
|
-
let firstStylesheet = this.doc.querySelector("style, link[rel=stylesheet]"), previousStyleElement;
|
|
932
|
-
this.options.fonts.forEach(fontData => {
|
|
933
|
-
if (fontData["font-family"] && fontData.src) {
|
|
934
|
-
let stylesheetContent = "@font-face{";
|
|
935
|
-
let stylesContent = "";
|
|
936
|
-
Object.keys(fontData).forEach(fontStyle => {
|
|
937
|
-
if (stylesContent) {
|
|
938
|
-
stylesContent += ";";
|
|
939
|
-
}
|
|
940
|
-
stylesContent += fontStyle + ":" + fontData[fontStyle];
|
|
941
|
-
});
|
|
942
|
-
stylesheetContent += stylesContent + "}";
|
|
943
|
-
const styleElement = this.doc.createElement("style");
|
|
944
|
-
styleElement.textContent = stylesheetContent;
|
|
945
|
-
if (previousStyleElement) {
|
|
946
|
-
previousStyleElement.insertAdjacentElement("afterend", styleElement);
|
|
947
|
-
} else if (firstStylesheet) {
|
|
948
|
-
firstStylesheet.parentElement.insertBefore(styleElement, firstStylesheet);
|
|
949
|
-
} else {
|
|
950
|
-
this.doc.head.appendChild(styleElement);
|
|
951
|
-
}
|
|
952
|
-
previousStyleElement = styleElement;
|
|
953
|
-
}
|
|
954
|
-
});
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
removeHiddenElements() {
|
|
959
|
-
const hiddenElements = this.doc.querySelectorAll("[" + util.HIDDEN_CONTENT_ATTRIBUTE_NAME + "]");
|
|
960
|
-
const removedElements = this.doc.querySelectorAll("[" + util.REMOVED_CONTENT_ATTRIBUTE_NAME + "]");
|
|
961
|
-
this.stats.set("discarded", "hidden elements", removedElements.length);
|
|
962
|
-
this.stats.set("processed", "hidden elements", removedElements.length);
|
|
963
|
-
if (hiddenElements.length) {
|
|
964
|
-
const styleElement = this.doc.createElement("style");
|
|
965
|
-
styleElement.textContent = ".sf-hidden{display:none!important;}";
|
|
966
|
-
this.doc.head.appendChild(styleElement);
|
|
967
|
-
hiddenElements.forEach(element => {
|
|
968
|
-
if (element.style.getPropertyValue("display") != "none") {
|
|
969
|
-
if (element.style.getPropertyPriority("display") == "important") {
|
|
970
|
-
element.style.setProperty("display", "none", "important");
|
|
971
|
-
} else {
|
|
972
|
-
element.classList.add("sf-hidden");
|
|
973
|
-
}
|
|
974
|
-
}
|
|
975
|
-
});
|
|
976
|
-
}
|
|
977
|
-
removedElements.forEach(element => element.remove());
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
resolveHrefs() {
|
|
981
|
-
this.doc.querySelectorAll("a[href], area[href], link[href]").forEach(element => {
|
|
982
|
-
const href = element.getAttribute("href").trim();
|
|
983
|
-
if (element.tagName.toUpperCase() == "LINK" && element.rel.includes("stylesheet")) {
|
|
984
|
-
if (this.options.saveOriginalURLs && !isDataURL(href)) {
|
|
985
|
-
element.setAttribute("data-sf-original-href", href);
|
|
986
|
-
}
|
|
987
|
-
}
|
|
988
|
-
if (!testIgnoredPath(href)) {
|
|
989
|
-
let resolvedURL;
|
|
990
|
-
try {
|
|
991
|
-
resolvedURL = util.resolveURL(href, this.options.baseURI || this.options.url);
|
|
992
|
-
} catch (error) {
|
|
993
|
-
// ignored
|
|
994
|
-
}
|
|
995
|
-
if (resolvedURL) {
|
|
996
|
-
const url = normalizeURL(this.options.url);
|
|
997
|
-
if (resolvedURL.startsWith(url + "#") && !resolvedURL.startsWith(url + "#!") && !this.options.resolveFragmentIdentifierURLs) {
|
|
998
|
-
resolvedURL = resolvedURL.substring(url.length);
|
|
999
|
-
}
|
|
1000
|
-
try {
|
|
1001
|
-
element.setAttribute("href", resolvedURL);
|
|
1002
|
-
} catch (error) {
|
|
1003
|
-
// ignored
|
|
1004
|
-
}
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
});
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
async insertMissingVideoPosters() {
|
|
1011
|
-
await Promise.all(Array.from(this.doc.querySelectorAll("video[src], video > source[src]")).map(async element => {
|
|
1012
|
-
let videoElement;
|
|
1013
|
-
if (element.tagName.toUpperCase() == "VIDEO") {
|
|
1014
|
-
videoElement = element;
|
|
1015
|
-
} else {
|
|
1016
|
-
videoElement = element.parentElement;
|
|
1017
|
-
}
|
|
1018
|
-
if (!videoElement.poster) {
|
|
1019
|
-
const attributeValue = videoElement.getAttribute(util.VIDEO_ATTRIBUTE_NAME);
|
|
1020
|
-
if (attributeValue) {
|
|
1021
|
-
const videoData = this.options.videos[Number(attributeValue)];
|
|
1022
|
-
const src = videoData.src || videoElement.src;
|
|
1023
|
-
if (src) {
|
|
1024
|
-
const temporaryVideoElement = this.doc.createElement("video");
|
|
1025
|
-
temporaryVideoElement.src = src;
|
|
1026
|
-
temporaryVideoElement.style.setProperty("width", videoData.size.pxWidth + "px", "important");
|
|
1027
|
-
temporaryVideoElement.style.setProperty("height", videoData.size.pxHeight + "px", "important");
|
|
1028
|
-
temporaryVideoElement.style.setProperty("display", "none", "important");
|
|
1029
|
-
temporaryVideoElement.crossOrigin = "anonymous";
|
|
1030
|
-
const canvasElement = this.doc.createElement("canvas");
|
|
1031
|
-
const context = canvasElement.getContext("2d");
|
|
1032
|
-
this.options.doc.body.appendChild(temporaryVideoElement);
|
|
1033
|
-
return new Promise(resolve => {
|
|
1034
|
-
temporaryVideoElement.currentTime = videoData.currentTime;
|
|
1035
|
-
temporaryVideoElement.oncanplay = () => {
|
|
1036
|
-
canvasElement.width = videoData.size.pxWidth;
|
|
1037
|
-
canvasElement.height = videoData.size.pxHeight;
|
|
1038
|
-
context.drawImage(temporaryVideoElement, 0, 0, canvasElement.width, canvasElement.height);
|
|
1039
|
-
try {
|
|
1040
|
-
videoElement.poster = canvasElement.toDataURL("image/png", "");
|
|
1041
|
-
} catch (error) {
|
|
1042
|
-
// ignored
|
|
1043
|
-
}
|
|
1044
|
-
temporaryVideoElement.remove();
|
|
1045
|
-
resolve();
|
|
1046
|
-
};
|
|
1047
|
-
temporaryVideoElement.onerror = () => {
|
|
1048
|
-
temporaryVideoElement.remove();
|
|
1049
|
-
resolve();
|
|
1050
|
-
};
|
|
1051
|
-
});
|
|
1052
|
-
}
|
|
1053
|
-
}
|
|
1054
|
-
}
|
|
1055
|
-
}));
|
|
1056
|
-
}
|
|
1057
|
-
|
|
1058
|
-
resolveStyleAttributeURLs() {
|
|
1059
|
-
this.doc.querySelectorAll("[style]").forEach(element => {
|
|
1060
|
-
if (this.options.blockStylesheets) {
|
|
1061
|
-
element.removeAttribute("style");
|
|
1062
|
-
} else {
|
|
1063
|
-
const styleContent = element.getAttribute("style");
|
|
1064
|
-
const declarationList = cssTree.parse(styleContent, { context: "declarationList", parseCustomProperty: true });
|
|
1065
|
-
ProcessorHelper.resolveStylesheetURLs(declarationList, this.baseURI, this.workStyleElement);
|
|
1066
|
-
this.styles.set(element, declarationList);
|
|
1067
|
-
}
|
|
1068
|
-
});
|
|
1069
|
-
}
|
|
1070
|
-
|
|
1071
|
-
async resolveStylesheetURLs() {
|
|
1072
|
-
await Promise.all(Array.from(this.doc.querySelectorAll("style, link[rel*=stylesheet]")).map(async element => {
|
|
1073
|
-
const options = Object.assign({}, this.options, { charset: this.charset });
|
|
1074
|
-
let mediaText;
|
|
1075
|
-
if (element.media) {
|
|
1076
|
-
mediaText = element.media.toLowerCase();
|
|
1077
|
-
}
|
|
1078
|
-
const scoped = Boolean(element.closest("[" + SHADOWROOT_ATTRIBUTE_NAME + "]"));
|
|
1079
|
-
const stylesheetInfo = {
|
|
1080
|
-
mediaText,
|
|
1081
|
-
scoped
|
|
1082
|
-
};
|
|
1083
|
-
if (element.tagName.toUpperCase() == "LINK" && element.charset) {
|
|
1084
|
-
options.charset = element.charset;
|
|
1085
|
-
}
|
|
1086
|
-
await processElement(element, stylesheetInfo, this.stylesheets, this.baseURI, options, this.workStyleElement);
|
|
1087
|
-
}));
|
|
1088
|
-
if (this.options.rootDocument) {
|
|
1089
|
-
const newResources = Object.keys(this.options.updatedResources)
|
|
1090
|
-
.filter(url => this.options.updatedResources[url].type == "stylesheet" && !this.options.updatedResources[url].retrieved)
|
|
1091
|
-
.map(url => this.options.updatedResources[url]);
|
|
1092
|
-
await Promise.all(newResources.map(async resource => {
|
|
1093
|
-
resource.retrieved = true;
|
|
1094
|
-
if (!this.options.blockStylesheets) {
|
|
1095
|
-
const stylesheetInfo = {};
|
|
1096
|
-
const element = this.doc.createElement("style");
|
|
1097
|
-
this.doc.body.appendChild(element);
|
|
1098
|
-
element.textContent = resource.content;
|
|
1099
|
-
await processElement(element, stylesheetInfo, this.stylesheets, this.baseURI, this.options, this.workStyleElement);
|
|
1100
|
-
}
|
|
1101
|
-
}));
|
|
1102
|
-
}
|
|
1103
|
-
|
|
1104
|
-
async function processElement(element, stylesheetInfo, stylesheets, baseURI, options, workStyleElement) {
|
|
1105
|
-
let stylesheet;
|
|
1106
|
-
stylesheets.set(element, stylesheetInfo);
|
|
1107
|
-
if (!options.blockStylesheets) {
|
|
1108
|
-
if (element.tagName.toUpperCase() == "LINK") {
|
|
1109
|
-
stylesheet = await ProcessorHelper.resolveLinkStylesheetURLs(element.href, baseURI, options, workStyleElement);
|
|
1110
|
-
} else {
|
|
1111
|
-
stylesheet = cssTree.parse(element.textContent, { context: "stylesheet", parseCustomProperty: true });
|
|
1112
|
-
const importFound = await ProcessorHelper.resolveImportURLs(stylesheet, baseURI, options, workStyleElement);
|
|
1113
|
-
if (importFound) {
|
|
1114
|
-
stylesheet = cssTree.parse(cssTree.generate(stylesheet), { context: "stylesheet", parseCustomProperty: true });
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
if (stylesheet && stylesheet.children) {
|
|
1119
|
-
if (options.compressCSS) {
|
|
1120
|
-
ProcessorHelper.removeSingleLineCssComments(stylesheet);
|
|
1121
|
-
}
|
|
1122
|
-
ProcessorHelper.replacePseudoClassDefined(stylesheet);
|
|
1123
|
-
stylesheetInfo.stylesheet = stylesheet;
|
|
1124
|
-
} else {
|
|
1125
|
-
stylesheets.delete(element);
|
|
1126
|
-
}
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
|
|
1130
|
-
async resolveFrameURLs() {
|
|
1131
|
-
if (!this.options.saveRawPage) {
|
|
1132
|
-
const frameElements = Array.from(this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"));
|
|
1133
|
-
await Promise.all(frameElements.map(async frameElement => {
|
|
1134
|
-
if (frameElement.tagName.toUpperCase() == "OBJECT") {
|
|
1135
|
-
frameElement.setAttribute("data", "data:text/html,");
|
|
1136
|
-
} else {
|
|
1137
|
-
const src = frameElement.getAttribute("src");
|
|
1138
|
-
if (this.options.saveOriginalURLs && src && !isDataURL(src)) {
|
|
1139
|
-
frameElement.setAttribute("data-sf-original-src", src);
|
|
1140
|
-
}
|
|
1141
|
-
frameElement.removeAttribute("src");
|
|
1142
|
-
frameElement.removeAttribute("srcdoc");
|
|
1143
|
-
}
|
|
1144
|
-
Array.from(frameElement.childNodes).forEach(node => node.remove());
|
|
1145
|
-
const frameWindowId = frameElement.getAttribute(util.WIN_ID_ATTRIBUTE_NAME);
|
|
1146
|
-
if (this.options.frames && frameWindowId) {
|
|
1147
|
-
const frameData = this.options.frames.find(frame => frame.windowId == frameWindowId);
|
|
1148
|
-
if (frameData) {
|
|
1149
|
-
await initializeProcessor(frameData, frameElement, frameWindowId, this.batchRequest, Object.create(this.options));
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
}));
|
|
1153
|
-
}
|
|
1154
|
-
|
|
1155
|
-
async function initializeProcessor(frameData, frameElement, frameWindowId, batchRequest, options) {
|
|
1156
|
-
options.insertSingleFileComment = false;
|
|
1157
|
-
options.insertCanonicalLink = false;
|
|
1158
|
-
options.insertMetaNoIndex = false;
|
|
1159
|
-
options.saveFavicon = false;
|
|
1160
|
-
options.includeInfobar = false;
|
|
1161
|
-
options.url = frameData.baseURI;
|
|
1162
|
-
options.windowId = frameWindowId;
|
|
1163
|
-
if (frameData.content) {
|
|
1164
|
-
options.content = frameData.content;
|
|
1165
|
-
options.canvases = frameData.canvases;
|
|
1166
|
-
options.fonts = frameData.fonts;
|
|
1167
|
-
options.stylesheets = frameData.stylesheets;
|
|
1168
|
-
options.images = frameData.images;
|
|
1169
|
-
options.posters = frameData.posters;
|
|
1170
|
-
options.videos = frameData.videos;
|
|
1171
|
-
options.usedFonts = frameData.usedFonts;
|
|
1172
|
-
options.shadowRoots = frameData.shadowRoots;
|
|
1173
|
-
options.scrollPosition = frameData.scrollPosition;
|
|
1174
|
-
options.scrolling = frameData.scrolling;
|
|
1175
|
-
options.adoptedStyleSheets = frameData.adoptedStyleSheets;
|
|
1176
|
-
frameData.runner = new Runner(options);
|
|
1177
|
-
frameData.frameElement = frameElement;
|
|
1178
|
-
await frameData.runner.loadPage();
|
|
1179
|
-
await frameData.runner.initialize();
|
|
1180
|
-
frameData.maxResources = batchRequest.getMaxResources();
|
|
1181
|
-
}
|
|
1182
|
-
}
|
|
1183
|
-
}
|
|
1184
|
-
|
|
1185
|
-
insertShadowRootContents() {
|
|
1186
|
-
const doc = this.doc;
|
|
1187
|
-
const options = this.options;
|
|
1188
|
-
if (options.shadowRoots && options.shadowRoots.length) {
|
|
1189
|
-
processElement(this.doc);
|
|
1190
|
-
}
|
|
1191
|
-
|
|
1192
|
-
function processElement(element) {
|
|
1193
|
-
const shadowRootElements = Array.from(element.querySelectorAll("[" + util.SHADOW_ROOT_ATTRIBUTE_NAME + "]"));
|
|
1194
|
-
shadowRootElements.forEach(element => {
|
|
1195
|
-
const attributeValue = element.getAttribute(util.SHADOW_ROOT_ATTRIBUTE_NAME);
|
|
1196
|
-
if (attributeValue) {
|
|
1197
|
-
const shadowRootData = options.shadowRoots[Number(attributeValue)];
|
|
1198
|
-
if (shadowRootData) {
|
|
1199
|
-
const templateElement = doc.createElement("template");
|
|
1200
|
-
templateElement.setAttribute(SHADOWROOT_ATTRIBUTE_NAME, shadowRootData.mode);
|
|
1201
|
-
if (shadowRootData.adoptedStyleSheets && shadowRootData.adoptedStyleSheets.length) {
|
|
1202
|
-
shadowRootData.adoptedStyleSheets.forEach(stylesheetContent => {
|
|
1203
|
-
const styleElement = doc.createElement("style");
|
|
1204
|
-
styleElement.textContent = stylesheetContent;
|
|
1205
|
-
templateElement.appendChild(styleElement);
|
|
1206
|
-
});
|
|
1207
|
-
}
|
|
1208
|
-
const shadowDoc = util.parseDocContent(shadowRootData.content);
|
|
1209
|
-
if (shadowDoc.head) {
|
|
1210
|
-
const metaCharset = shadowDoc.head.querySelector("meta[charset]");
|
|
1211
|
-
if (metaCharset) {
|
|
1212
|
-
metaCharset.remove();
|
|
1213
|
-
}
|
|
1214
|
-
shadowDoc.head.childNodes.forEach(node => templateElement.appendChild(shadowDoc.importNode(node, true)));
|
|
1215
|
-
}
|
|
1216
|
-
if (shadowDoc.body) {
|
|
1217
|
-
shadowDoc.body.childNodes.forEach(node => templateElement.appendChild(shadowDoc.importNode(node, true)));
|
|
1218
|
-
}
|
|
1219
|
-
processElement(templateElement);
|
|
1220
|
-
if (element.firstChild) {
|
|
1221
|
-
element.insertBefore(templateElement, element.firstChild);
|
|
1222
|
-
} else {
|
|
1223
|
-
element.appendChild(templateElement);
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
});
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
|
|
1231
|
-
removeUnusedStyles() {
|
|
1232
|
-
if (!this.mediaAllInfo) {
|
|
1233
|
-
this.mediaAllInfo = util.getMediaAllInfo(this.doc, this.stylesheets, this.styles);
|
|
1234
|
-
}
|
|
1235
|
-
const stats = util.minifyCSSRules(this.stylesheets, this.styles, this.mediaAllInfo);
|
|
1236
|
-
this.stats.set("processed", "CSS rules", stats.processed);
|
|
1237
|
-
this.stats.set("discarded", "CSS rules", stats.discarded);
|
|
1238
|
-
}
|
|
1239
|
-
|
|
1240
|
-
removeUnusedFonts() {
|
|
1241
|
-
util.removeUnusedFonts(this.doc, this.stylesheets, this.styles, this.options);
|
|
1242
|
-
}
|
|
1243
|
-
|
|
1244
|
-
removeAlternativeMedias() {
|
|
1245
|
-
const stats = util.minifyMedias(this.stylesheets);
|
|
1246
|
-
this.stats.set("processed", "medias", stats.processed);
|
|
1247
|
-
this.stats.set("discarded", "medias", stats.discarded);
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
async processStylesheets() {
|
|
1251
|
-
this.options.fontDeclarations = new Map();
|
|
1252
|
-
await Promise.all([...this.stylesheets].map(([, stylesheetInfo]) =>
|
|
1253
|
-
ProcessorHelper.processStylesheet(stylesheetInfo.stylesheet.children, this.baseURI, this.options, this.cssVariables, this.batchRequest)
|
|
1254
|
-
));
|
|
1255
|
-
}
|
|
1256
|
-
|
|
1257
|
-
async processStyleAttributes() {
|
|
1258
|
-
return Promise.all([...this.styles].map(([, stylesheet]) =>
|
|
1259
|
-
ProcessorHelper.processStyle(stylesheet, this.baseURI, this.options, this.cssVariables, this.batchRequest)
|
|
1260
|
-
));
|
|
1261
|
-
}
|
|
1262
|
-
|
|
1263
|
-
async processPageResources() {
|
|
1264
|
-
const processAttributeArgs = [
|
|
1265
|
-
["link[href][rel*=\"icon\"]", "href", false, true],
|
|
1266
|
-
["object[type=\"image/svg+xml\"], object[type=\"image/svg-xml\"], object[data*=\".svg\"]", "data"],
|
|
1267
|
-
["img[src], input[src][type=image]", "src", true],
|
|
1268
|
-
["embed[src*=\".svg\"]", "src"],
|
|
1269
|
-
["video[poster]", "poster"],
|
|
1270
|
-
["*[background]", "background"],
|
|
1271
|
-
["image", "xlink:href"],
|
|
1272
|
-
["image", "href"]
|
|
1273
|
-
];
|
|
1274
|
-
if (this.options.blockImages) {
|
|
1275
|
-
this.doc.querySelectorAll("svg").forEach(element => element.remove());
|
|
1276
|
-
}
|
|
1277
|
-
let resourcePromises = processAttributeArgs.map(([selector, attributeName, processDuplicates, removeElementIfMissing]) =>
|
|
1278
|
-
ProcessorHelper.processAttribute(this.doc.querySelectorAll(selector), attributeName, this.baseURI, this.options, "image", this.cssVariables, this.styles, this.batchRequest, processDuplicates, removeElementIfMissing)
|
|
1279
|
-
);
|
|
1280
|
-
resourcePromises.
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
resourcePromises.push(ProcessorHelper.processAttribute(this.doc.querySelectorAll("
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
frameElement.
|
|
1370
|
-
}
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
this.stats.
|
|
1376
|
-
}
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
stylesheetContent
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
}
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
}
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
const
|
|
1476
|
-
const
|
|
1477
|
-
const
|
|
1478
|
-
this.doc.documentElement.style.
|
|
1479
|
-
this.doc.documentElement.style.
|
|
1480
|
-
this.doc.documentElement.style.
|
|
1481
|
-
this.doc.documentElement.style.
|
|
1482
|
-
this.doc.documentElement.style.
|
|
1483
|
-
this.doc.documentElement.style.
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
const
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
if (
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
const
|
|
1513
|
-
const
|
|
1514
|
-
const
|
|
1515
|
-
|
|
1516
|
-
this.
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
}
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
const
|
|
1535
|
-
const
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
element.style.setProperty("background-
|
|
1543
|
-
element.style.setProperty("background-
|
|
1544
|
-
element.style.setProperty("background-
|
|
1545
|
-
element.style.setProperty("background-
|
|
1546
|
-
element.style.setProperty("background-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
if (!shortcutIcon) {
|
|
1555
|
-
shortcutIcon = findShortcutIcon(Array.from(doc.querySelectorAll("link[href][rel
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
}
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
}
|
|
1704
|
-
if (
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
}
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
}
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
}
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
image
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
resourceElement.
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
resourceElement.
|
|
1955
|
-
}
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
}
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
if (
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
if (
|
|
2051
|
-
backgroundStyle["background-
|
|
2052
|
-
}
|
|
2053
|
-
if (imageData.
|
|
2054
|
-
backgroundStyle["background-
|
|
2055
|
-
}
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
}
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
const
|
|
2071
|
-
const
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
}
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
}
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
}
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
}
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
if (options.
|
|
2132
|
-
stylesheetContent =
|
|
2133
|
-
}
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
}
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
//
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
"
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
"
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
"
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
"
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2010-2022 Gildas Lormeau
|
|
3
|
+
* contact : gildas.lormeau <at> gmail.com
|
|
4
|
+
*
|
|
5
|
+
* This file is part of SingleFile.
|
|
6
|
+
*
|
|
7
|
+
* The code in this file is free software: you can redistribute it and/or
|
|
8
|
+
* modify it under the terms of the GNU Affero General Public License
|
|
9
|
+
* (GNU AGPL) as published by the Free Software Foundation, either version 3
|
|
10
|
+
* of the License, or (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* The code in this file is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
|
|
15
|
+
* General Public License for more details.
|
|
16
|
+
*
|
|
17
|
+
* As additional permission under GNU AGPL version 3 section 7, you may
|
|
18
|
+
* distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
|
|
19
|
+
* AGPL normally required by section 4, provided you include this license
|
|
20
|
+
* notice and a URL through which recipients can access the Corresponding
|
|
21
|
+
* Source.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/* global globalThis */
|
|
25
|
+
|
|
26
|
+
const DEBUG = false;
|
|
27
|
+
|
|
28
|
+
const Set = globalThis.Set;
|
|
29
|
+
const Map = globalThis.Map;
|
|
30
|
+
const JSON = globalThis.JSON;
|
|
31
|
+
const setTimeout = globalThis.setTimeout;
|
|
32
|
+
const clearTimeout = globalThis.clearTimeout;
|
|
33
|
+
const Image = globalThis.Image;
|
|
34
|
+
|
|
35
|
+
let util, cssTree;
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
getClass
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function getClass(...args) {
|
|
42
|
+
[util, cssTree] = args;
|
|
43
|
+
return SingleFileClass;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
class SingleFileClass {
|
|
47
|
+
constructor(options) {
|
|
48
|
+
this.options = options;
|
|
49
|
+
}
|
|
50
|
+
async run() {
|
|
51
|
+
const waitForUserScript = globalThis[util.WAIT_FOR_USERSCRIPT_PROPERTY_NAME];
|
|
52
|
+
if (this.options.userScriptEnabled && waitForUserScript) {
|
|
53
|
+
await waitForUserScript(util.ON_BEFORE_CAPTURE_EVENT_NAME);
|
|
54
|
+
}
|
|
55
|
+
this.runner = new Runner(this.options, true);
|
|
56
|
+
await this.runner.loadPage();
|
|
57
|
+
await this.runner.initialize();
|
|
58
|
+
if (this.options.userScriptEnabled && waitForUserScript) {
|
|
59
|
+
await waitForUserScript(util.ON_AFTER_CAPTURE_EVENT_NAME);
|
|
60
|
+
}
|
|
61
|
+
await this.runner.run();
|
|
62
|
+
}
|
|
63
|
+
cancel() {
|
|
64
|
+
this.cancelled = true;
|
|
65
|
+
if (this.runner) {
|
|
66
|
+
this.runner.cancel();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
getPageData() {
|
|
70
|
+
return this.runner.getPageData();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// -------------
|
|
75
|
+
// ProgressEvent
|
|
76
|
+
// -------------
|
|
77
|
+
const PAGE_LOADING = "page-loading";
|
|
78
|
+
const PAGE_LOADED = "page-loaded";
|
|
79
|
+
const RESOURCES_INITIALIZING = "resource-initializing";
|
|
80
|
+
const RESOURCES_INITIALIZED = "resources-initialized";
|
|
81
|
+
const RESOURCE_LOADED = "resource-loaded";
|
|
82
|
+
const PAGE_ENDED = "page-ended";
|
|
83
|
+
const STAGE_STARTED = "stage-started";
|
|
84
|
+
const STAGE_ENDED = "stage-ended";
|
|
85
|
+
const STAGE_TASK_STARTED = "stage-task-started";
|
|
86
|
+
const STAGE_TASK_ENDED = "stage-task-ended";
|
|
87
|
+
|
|
88
|
+
class ProgressEvent {
|
|
89
|
+
constructor(type, detail) {
|
|
90
|
+
return { type, detail, PAGE_LOADING, PAGE_LOADED, RESOURCES_INITIALIZING, RESOURCES_INITIALIZED, RESOURCE_LOADED, PAGE_ENDED, STAGE_STARTED, STAGE_ENDED, STAGE_TASK_STARTED, STAGE_TASK_ENDED };
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ------
|
|
95
|
+
// Runner
|
|
96
|
+
// ------
|
|
97
|
+
const RESOLVE_URLS_STAGE = 0;
|
|
98
|
+
const REPLACE_DATA_STAGE = 1;
|
|
99
|
+
const REPLACE_DOCS_STAGE = 2;
|
|
100
|
+
const POST_PROCESS_STAGE = 3;
|
|
101
|
+
const STAGES = [{
|
|
102
|
+
sequential: [
|
|
103
|
+
{ action: "preProcessPage" },
|
|
104
|
+
{ option: "loadDeferredImagesKeepZoomLevel", action: "resetZoomLevel" },
|
|
105
|
+
{ action: "replaceStyleContents" },
|
|
106
|
+
{ action: "replaceInvalidElements" },
|
|
107
|
+
{ action: "resetCharsetMeta" },
|
|
108
|
+
{ option: "saveFavicon", action: "saveFavicon" },
|
|
109
|
+
{ action: "insertFonts" },
|
|
110
|
+
{ action: "insertShadowRootContents" },
|
|
111
|
+
{ action: "replaceCanvasElements" },
|
|
112
|
+
{ action: "setInputValues" },
|
|
113
|
+
{ option: "moveStylesInHead", action: "moveStylesInHead" },
|
|
114
|
+
{ option: "blockScripts", action: "removeEmbedScripts" },
|
|
115
|
+
{ option: "selected", action: "removeUnselectedElements" },
|
|
116
|
+
{ option: "blockVideos", action: "insertVideoPosters" },
|
|
117
|
+
{ option: "blockVideos", action: "insertVideoLinks" },
|
|
118
|
+
{ option: "removeFrames", action: "removeFrames" },
|
|
119
|
+
{ action: "removeDiscardedResources" },
|
|
120
|
+
{ option: "removeHiddenElements", action: "removeHiddenElements" },
|
|
121
|
+
{ action: "saveScrollPosition" },
|
|
122
|
+
{ action: "resolveHrefs" },
|
|
123
|
+
{ action: "resolveStyleAttributeURLs" }
|
|
124
|
+
],
|
|
125
|
+
parallel: [
|
|
126
|
+
{ option: "blockVideos", action: "insertMissingVideoPosters" },
|
|
127
|
+
{ action: "resolveStylesheetURLs" },
|
|
128
|
+
{ option: "!removeFrames", action: "resolveFrameURLs" }
|
|
129
|
+
]
|
|
130
|
+
}, {
|
|
131
|
+
sequential: [
|
|
132
|
+
{ option: "removeUnusedStyles", action: "removeUnusedStyles" },
|
|
133
|
+
{ option: "removeAlternativeMedias", action: "removeAlternativeMedias" },
|
|
134
|
+
{ option: "removeUnusedFonts", action: "removeUnusedFonts" }
|
|
135
|
+
],
|
|
136
|
+
parallel: [
|
|
137
|
+
{ action: "processStylesheets" },
|
|
138
|
+
{ action: "processStyleAttributes" },
|
|
139
|
+
{ action: "processPageResources" },
|
|
140
|
+
{ action: "processScripts" }
|
|
141
|
+
]
|
|
142
|
+
}, {
|
|
143
|
+
sequential: [
|
|
144
|
+
{ option: "removeAlternativeImages", action: "removeAlternativeImages" }
|
|
145
|
+
],
|
|
146
|
+
parallel: [
|
|
147
|
+
{ option: "removeAlternativeFonts", action: "removeAlternativeFonts" },
|
|
148
|
+
{ option: "!removeFrames", action: "processFrames" }
|
|
149
|
+
]
|
|
150
|
+
}, {
|
|
151
|
+
sequential: [
|
|
152
|
+
{ action: "replaceStylesheets" },
|
|
153
|
+
{ action: "replaceStyleAttributes" },
|
|
154
|
+
{ action: "insertVariables" },
|
|
155
|
+
{ option: "compressHTML", action: "compressHTML" },
|
|
156
|
+
{ action: "cleanupPage" }
|
|
157
|
+
],
|
|
158
|
+
parallel: [
|
|
159
|
+
{ option: "enableMaff", action: "insertMAFFMetaData" },
|
|
160
|
+
{ action: "setDocInfo" }
|
|
161
|
+
]
|
|
162
|
+
}];
|
|
163
|
+
|
|
164
|
+
class Runner {
|
|
165
|
+
constructor(options, root) {
|
|
166
|
+
const rootDocDefined = root && options.doc;
|
|
167
|
+
this.root = root;
|
|
168
|
+
this.options = options;
|
|
169
|
+
this.options.url = this.options.url || (rootDocDefined && this.options.doc.location.href);
|
|
170
|
+
const matchResourceReferrer = this.options.url.match(/^.*\//);
|
|
171
|
+
this.options.resourceReferrer = this.options.passReferrerOnError && matchResourceReferrer && matchResourceReferrer[0];
|
|
172
|
+
this.options.baseURI = rootDocDefined && this.options.doc.baseURI;
|
|
173
|
+
this.options.rootDocument = root;
|
|
174
|
+
this.options.updatedResources = this.options.updatedResources || {};
|
|
175
|
+
this.options.fontTests = new Map();
|
|
176
|
+
this.batchRequest = new BatchRequest();
|
|
177
|
+
this.processor = new Processor(options, this.batchRequest);
|
|
178
|
+
if (rootDocDefined) {
|
|
179
|
+
const docData = util.preProcessDoc(this.options.doc, this.options.win, this.options);
|
|
180
|
+
this.options.canvases = docData.canvases;
|
|
181
|
+
this.options.fonts = docData.fonts;
|
|
182
|
+
this.options.stylesheets = docData.stylesheets;
|
|
183
|
+
this.options.images = docData.images;
|
|
184
|
+
this.options.posters = docData.posters;
|
|
185
|
+
this.options.videos = docData.videos;
|
|
186
|
+
this.options.usedFonts = docData.usedFonts;
|
|
187
|
+
this.options.shadowRoots = docData.shadowRoots;
|
|
188
|
+
this.options.referrer = docData.referrer;
|
|
189
|
+
this.options.adoptedStyleSheets = docData.adoptedStyleSheets;
|
|
190
|
+
this.markedElements = docData.markedElements;
|
|
191
|
+
this.invalidElements = docData.invalidElements;
|
|
192
|
+
}
|
|
193
|
+
if (this.options.saveRawPage) {
|
|
194
|
+
this.options.removeFrames = true;
|
|
195
|
+
}
|
|
196
|
+
this.options.content = this.options.content || (rootDocDefined ? util.serialize(this.options.doc) : null);
|
|
197
|
+
this.onprogress = options.onprogress || (() => { });
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async loadPage() {
|
|
201
|
+
this.onprogress(new ProgressEvent(PAGE_LOADING, { pageURL: this.options.url, frame: !this.root }));
|
|
202
|
+
await this.processor.loadPage(this.options.content);
|
|
203
|
+
this.onprogress(new ProgressEvent(PAGE_LOADED, { pageURL: this.options.url, frame: !this.root }));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async initialize() {
|
|
207
|
+
this.onprogress(new ProgressEvent(RESOURCES_INITIALIZING, { pageURL: this.options.url }));
|
|
208
|
+
await this.executeStage(RESOLVE_URLS_STAGE);
|
|
209
|
+
this.pendingPromises = this.executeStage(REPLACE_DATA_STAGE);
|
|
210
|
+
if (this.root && this.options.doc) {
|
|
211
|
+
util.postProcessDoc(this.options.doc, this.markedElements, this.invalidElements);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
cancel() {
|
|
216
|
+
this.cancelled = true;
|
|
217
|
+
this.batchRequest.cancel();
|
|
218
|
+
if (this.root) {
|
|
219
|
+
if (this.options.frames) {
|
|
220
|
+
this.options.frames.forEach(cancelRunner);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function cancelRunner(resourceData) {
|
|
225
|
+
if (resourceData.runner) {
|
|
226
|
+
resourceData.runner.cancel();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async run() {
|
|
232
|
+
if (this.root) {
|
|
233
|
+
this.processor.initialize(this.batchRequest);
|
|
234
|
+
this.onprogress(new ProgressEvent(RESOURCES_INITIALIZED, { pageURL: this.options.url, max: this.processor.maxResources }));
|
|
235
|
+
}
|
|
236
|
+
await this.batchRequest.run(detail => {
|
|
237
|
+
detail.pageURL = this.options.url;
|
|
238
|
+
this.onprogress(new ProgressEvent(RESOURCE_LOADED, detail));
|
|
239
|
+
}, this.options);
|
|
240
|
+
await this.pendingPromises;
|
|
241
|
+
this.options.doc = null;
|
|
242
|
+
this.options.win = null;
|
|
243
|
+
await this.executeStage(REPLACE_DOCS_STAGE);
|
|
244
|
+
await this.executeStage(POST_PROCESS_STAGE);
|
|
245
|
+
this.processor.finalize();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
getDocument() {
|
|
249
|
+
return this.processor.doc;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
getStyleSheets() {
|
|
253
|
+
return this.processor.stylesheets;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
getPageData() {
|
|
257
|
+
if (this.root) {
|
|
258
|
+
this.onprogress(new ProgressEvent(PAGE_ENDED, { pageURL: this.options.url }));
|
|
259
|
+
}
|
|
260
|
+
return this.processor.getPageData();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async executeStage(step) {
|
|
264
|
+
if (DEBUG) {
|
|
265
|
+
log("**** STARTED STAGE", step, "****");
|
|
266
|
+
}
|
|
267
|
+
const frame = !this.root;
|
|
268
|
+
this.onprogress(new ProgressEvent(STAGE_STARTED, { pageURL: this.options.url, step, frame }));
|
|
269
|
+
STAGES[step].sequential.forEach(task => {
|
|
270
|
+
let startTime;
|
|
271
|
+
if (DEBUG) {
|
|
272
|
+
startTime = Date.now();
|
|
273
|
+
log(" -- STARTED task =", task.action);
|
|
274
|
+
}
|
|
275
|
+
this.onprogress(new ProgressEvent(STAGE_TASK_STARTED, { pageURL: this.options.url, step, task: task.action, frame }));
|
|
276
|
+
if (!this.cancelled) {
|
|
277
|
+
this.executeTask(task);
|
|
278
|
+
}
|
|
279
|
+
this.onprogress(new ProgressEvent(STAGE_TASK_ENDED, { pageURL: this.options.url, step, task: task.action, frame }));
|
|
280
|
+
if (DEBUG) {
|
|
281
|
+
log(" -- ENDED task =", task.action, "delay =", Date.now() - startTime);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
let parallelTasksPromise;
|
|
285
|
+
if (STAGES[step].parallel) {
|
|
286
|
+
parallelTasksPromise = await Promise.all(STAGES[step].parallel.map(async task => {
|
|
287
|
+
let startTime;
|
|
288
|
+
if (DEBUG) {
|
|
289
|
+
startTime = Date.now();
|
|
290
|
+
log(" // STARTED task =", task.action);
|
|
291
|
+
}
|
|
292
|
+
this.onprogress(new ProgressEvent(STAGE_TASK_STARTED, { pageURL: this.options.url, step, task: task.action, frame }));
|
|
293
|
+
if (!this.cancelled) {
|
|
294
|
+
await this.executeTask(task);
|
|
295
|
+
}
|
|
296
|
+
this.onprogress(new ProgressEvent(STAGE_TASK_ENDED, { pageURL: this.options.url, step, task: task.action, frame }));
|
|
297
|
+
if (DEBUG) {
|
|
298
|
+
log(" // ENDED task =", task.action, "delay =", Date.now() - startTime);
|
|
299
|
+
}
|
|
300
|
+
}));
|
|
301
|
+
} else {
|
|
302
|
+
parallelTasksPromise = Promise.resolve();
|
|
303
|
+
}
|
|
304
|
+
this.onprogress(new ProgressEvent(STAGE_ENDED, { pageURL: this.options.url, step, frame }));
|
|
305
|
+
if (DEBUG) {
|
|
306
|
+
log("**** ENDED STAGE", step, "****");
|
|
307
|
+
}
|
|
308
|
+
return parallelTasksPromise;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
executeTask(task) {
|
|
312
|
+
if (!task.option || ((task.option.startsWith("!") && !this.options[task.option]) || this.options[task.option])) {
|
|
313
|
+
return this.processor[task.action]();
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// ------------
|
|
319
|
+
// BatchRequest
|
|
320
|
+
// ------------
|
|
321
|
+
class BatchRequest {
|
|
322
|
+
constructor() {
|
|
323
|
+
this.requests = new Map();
|
|
324
|
+
this.duplicates = new Map();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
addURL(resourceURL, { asBinary, expectedType, groupDuplicates, baseURI, blockMixedContent } = {}) {
|
|
328
|
+
return new Promise((resolve, reject) => {
|
|
329
|
+
const requestKey = JSON.stringify([resourceURL, asBinary, expectedType, baseURI, blockMixedContent]);
|
|
330
|
+
let resourceRequests = this.requests.get(requestKey);
|
|
331
|
+
if (!resourceRequests) {
|
|
332
|
+
resourceRequests = [];
|
|
333
|
+
this.requests.set(requestKey, resourceRequests);
|
|
334
|
+
}
|
|
335
|
+
const callbacks = { resolve, reject };
|
|
336
|
+
resourceRequests.push(callbacks);
|
|
337
|
+
if (groupDuplicates) {
|
|
338
|
+
let duplicateRequests = this.duplicates.get(requestKey);
|
|
339
|
+
if (!duplicateRequests) {
|
|
340
|
+
duplicateRequests = [];
|
|
341
|
+
this.duplicates.set(requestKey, duplicateRequests);
|
|
342
|
+
}
|
|
343
|
+
duplicateRequests.push(callbacks);
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
getMaxResources() {
|
|
349
|
+
return this.requests.size;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
run(onloadListener, options) {
|
|
353
|
+
const resourceURLs = [...this.requests.keys()];
|
|
354
|
+
let indexResource = 0;
|
|
355
|
+
return Promise.all(resourceURLs.map(async requestKey => {
|
|
356
|
+
const [resourceURL, asBinary, expectedType, baseURI, blockMixedContent] = JSON.parse(requestKey);
|
|
357
|
+
const resourceRequests = this.requests.get(requestKey);
|
|
358
|
+
try {
|
|
359
|
+
const currentIndexResource = indexResource;
|
|
360
|
+
indexResource = indexResource + 1;
|
|
361
|
+
const content = await util.getContent(resourceURL, {
|
|
362
|
+
asBinary,
|
|
363
|
+
expectedType,
|
|
364
|
+
maxResourceSize: options.maxResourceSize,
|
|
365
|
+
maxResourceSizeEnabled: options.maxResourceSizeEnabled,
|
|
366
|
+
frameId: options.windowId,
|
|
367
|
+
resourceReferrer: options.resourceReferrer,
|
|
368
|
+
baseURI,
|
|
369
|
+
blockMixedContent,
|
|
370
|
+
acceptHeaders: options.acceptHeaders,
|
|
371
|
+
networkTimeout: options.networkTimeout
|
|
372
|
+
});
|
|
373
|
+
onloadListener({ url: resourceURL });
|
|
374
|
+
if (!this.cancelled) {
|
|
375
|
+
resourceRequests.forEach(callbacks => {
|
|
376
|
+
const duplicateCallbacks = this.duplicates.get(requestKey);
|
|
377
|
+
const duplicate = duplicateCallbacks && duplicateCallbacks.length > 1 && duplicateCallbacks.includes(callbacks);
|
|
378
|
+
callbacks.resolve({ content: content.data, indexResource: currentIndexResource, duplicate });
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
} catch (error) {
|
|
382
|
+
indexResource = indexResource + 1;
|
|
383
|
+
onloadListener({ url: resourceURL });
|
|
384
|
+
resourceRequests.forEach(resourceRequest => resourceRequest.reject(error));
|
|
385
|
+
}
|
|
386
|
+
this.requests.delete(requestKey);
|
|
387
|
+
}));
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
cancel() {
|
|
391
|
+
this.cancelled = true;
|
|
392
|
+
const resourceURLs = [...this.requests.keys()];
|
|
393
|
+
resourceURLs.forEach(requestKey => {
|
|
394
|
+
const resourceRequests = this.requests.get(requestKey);
|
|
395
|
+
resourceRequests.forEach(callbacks => callbacks.reject());
|
|
396
|
+
this.requests.delete(requestKey);
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// ---------
|
|
402
|
+
// Processor
|
|
403
|
+
// ---------
|
|
404
|
+
const PREFIXES_FORBIDDEN_DATA_URI = ["data:text/"];
|
|
405
|
+
const PREFIX_DATA_URI_IMAGE_SVG = "data:image/svg+xml";
|
|
406
|
+
const SCRIPT_TAG_FOUND = /<script/gi;
|
|
407
|
+
const NOSCRIPT_TAG_FOUND = /<noscript/gi;
|
|
408
|
+
const CANVAS_TAG_FOUND = /<canvas/gi;
|
|
409
|
+
const SHADOWROOT_ATTRIBUTE_NAME = "shadowroot";
|
|
410
|
+
const SCRIPT_TEMPLATE_SHADOW_ROOT = "data-template-shadow-root";
|
|
411
|
+
const UTF8_CHARSET = "utf-8";
|
|
412
|
+
|
|
413
|
+
class Processor {
|
|
414
|
+
constructor(options, batchRequest) {
|
|
415
|
+
this.options = options;
|
|
416
|
+
this.stats = new Stats(options);
|
|
417
|
+
this.baseURI = normalizeURL(options.baseURI || options.url);
|
|
418
|
+
this.batchRequest = batchRequest;
|
|
419
|
+
this.stylesheets = new Map();
|
|
420
|
+
this.styles = new Map();
|
|
421
|
+
this.cssVariables = new Map();
|
|
422
|
+
this.fontTests = options.fontTests;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
initialize() {
|
|
426
|
+
this.options.saveDate = new Date();
|
|
427
|
+
this.options.saveUrl = this.options.url;
|
|
428
|
+
if (this.options.enableMaff) {
|
|
429
|
+
this.maffMetaDataPromise = this.batchRequest.addURL(util.resolveURL("index.rdf", this.options.baseURI || this.options.url), { expectedType: "document" });
|
|
430
|
+
}
|
|
431
|
+
this.maxResources = this.batchRequest.getMaxResources();
|
|
432
|
+
if (!this.options.saveRawPage && !this.options.removeFrames && this.options.frames) {
|
|
433
|
+
this.options.frames.forEach(frameData => this.maxResources += frameData.maxResources || 0);
|
|
434
|
+
}
|
|
435
|
+
this.stats.set("processed", "resources", this.maxResources);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
async loadPage(pageContent, charset) {
|
|
439
|
+
let content;
|
|
440
|
+
if (!pageContent || this.options.saveRawPage) {
|
|
441
|
+
content = await util.getContent(this.baseURI, {
|
|
442
|
+
maxResourceSize: this.options.maxResourceSize,
|
|
443
|
+
maxResourceSizeEnabled: this.options.maxResourceSizeEnabled,
|
|
444
|
+
charset,
|
|
445
|
+
frameId: this.options.windowId,
|
|
446
|
+
resourceReferrer: this.options.resourceReferrer,
|
|
447
|
+
expectedType: "document",
|
|
448
|
+
acceptHeaders: this.options.acceptHeaders,
|
|
449
|
+
networkTimeout: this.options.networkTimeout
|
|
450
|
+
});
|
|
451
|
+
pageContent = content.data || "";
|
|
452
|
+
}
|
|
453
|
+
this.doc = util.parseDocContent(pageContent, this.baseURI);
|
|
454
|
+
if (this.options.saveRawPage) {
|
|
455
|
+
let charset;
|
|
456
|
+
this.doc.querySelectorAll("meta[charset], meta[http-equiv=\"content-type\"]").forEach(element => {
|
|
457
|
+
const charsetDeclaration = element.content.split(";")[1];
|
|
458
|
+
if (charsetDeclaration && !charset) {
|
|
459
|
+
charset = charsetDeclaration.split("=")[1].trim().toLowerCase();
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
if (charset && content.charset && charset.toLowerCase() != content.charset.toLowerCase()) {
|
|
463
|
+
return this.loadPage(pageContent, charset);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
this.workStyleElement = this.doc.createElement("style");
|
|
467
|
+
this.doc.body.appendChild(this.workStyleElement);
|
|
468
|
+
this.onEventAttributeNames = getOnEventAttributeNames(this.doc);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
finalize() {
|
|
472
|
+
if (this.workStyleElement.parentNode) {
|
|
473
|
+
this.workStyleElement.remove();
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
async getPageData() {
|
|
478
|
+
util.postProcessDoc(this.doc);
|
|
479
|
+
const url = util.parseURL(this.baseURI);
|
|
480
|
+
if (this.options.insertSingleFileComment) {
|
|
481
|
+
const firstComment = this.doc.documentElement.firstChild;
|
|
482
|
+
let infobarURL = this.options.saveUrl, infobarSaveDate = this.options.saveDate;
|
|
483
|
+
if (firstComment.nodeType == 8 && (firstComment.textContent.includes(util.COMMENT_HEADER_LEGACY) || firstComment.textContent.includes(util.COMMENT_HEADER))) {
|
|
484
|
+
const info = this.doc.documentElement.firstChild.textContent.split("\n");
|
|
485
|
+
try {
|
|
486
|
+
const [, , url, saveDate] = info;
|
|
487
|
+
infobarURL = url.split("url: ")[1];
|
|
488
|
+
infobarSaveDate = saveDate.split("saved date: ")[1];
|
|
489
|
+
firstComment.remove();
|
|
490
|
+
} catch (error) {
|
|
491
|
+
// ignored
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
const infobarContent = (this.options.infobarContent || "").replace(/\\n/g, "\n").replace(/\\t/g, "\t");
|
|
495
|
+
const commentNode = this.doc.createComment(
|
|
496
|
+
"\n " + (this.options.useLegacyCommentHeader ? util.COMMENT_HEADER_LEGACY : util.COMMENT_HEADER) +
|
|
497
|
+
" \n url: " + infobarURL +
|
|
498
|
+
(this.options.removeSavedDate ? " " : " \n saved date: " + infobarSaveDate) +
|
|
499
|
+
(infobarContent ? " \n info: " + infobarContent : "") + "\n"
|
|
500
|
+
);
|
|
501
|
+
this.doc.documentElement.insertBefore(commentNode, this.doc.documentElement.firstChild);
|
|
502
|
+
}
|
|
503
|
+
const legacyInfobarElement = this.doc.querySelector("singlefile-infobar");
|
|
504
|
+
if (legacyInfobarElement) {
|
|
505
|
+
legacyInfobarElement.remove();
|
|
506
|
+
}
|
|
507
|
+
const infobarElement = this.doc.querySelector(util.INFOBAR_TAGNAME);
|
|
508
|
+
if (infobarElement) {
|
|
509
|
+
infobarElement.remove();
|
|
510
|
+
}
|
|
511
|
+
if (this.options.includeInfobar) {
|
|
512
|
+
util.appendInfobar(this.doc, this.options);
|
|
513
|
+
}
|
|
514
|
+
if (this.doc.querySelector("template[" + SHADOWROOT_ATTRIBUTE_NAME + "]") || (this.options.shadowRoots && this.options.shadowRoots.length)) {
|
|
515
|
+
if (this.options.blockScripts) {
|
|
516
|
+
this.doc.querySelectorAll("script[" + SCRIPT_TEMPLATE_SHADOW_ROOT + "]").forEach(element => element.remove());
|
|
517
|
+
}
|
|
518
|
+
const scriptElement = this.doc.createElement("script");
|
|
519
|
+
scriptElement.setAttribute(SCRIPT_TEMPLATE_SHADOW_ROOT, "");
|
|
520
|
+
scriptElement.textContent = `(()=>{document.currentScript.remove();processNode(document);function processNode(node){node.querySelectorAll("template[${SHADOWROOT_ATTRIBUTE_NAME}]").forEach(element=>{let shadowRoot = element.parentElement.shadowRoot;if (!shadowRoot) {try {shadowRoot=element.parentElement.attachShadow({mode:element.getAttribute("${SHADOWROOT_ATTRIBUTE_NAME}")});shadowRoot.innerHTML=element.innerHTML;element.remove()} catch (error) {} if (shadowRoot) {processNode(shadowRoot)}}})}})()`;
|
|
521
|
+
this.doc.body.appendChild(scriptElement);
|
|
522
|
+
}
|
|
523
|
+
if (this.options.insertCanonicalLink && this.options.saveUrl.match(HTTP_URI_PREFIX)) {
|
|
524
|
+
let canonicalLink = this.doc.querySelector("link[rel=canonical]");
|
|
525
|
+
if (!canonicalLink) {
|
|
526
|
+
canonicalLink = this.doc.createElement("link");
|
|
527
|
+
canonicalLink.setAttribute("rel", "canonical");
|
|
528
|
+
this.doc.head.appendChild(canonicalLink);
|
|
529
|
+
}
|
|
530
|
+
if (canonicalLink && !canonicalLink.href) {
|
|
531
|
+
canonicalLink.href = this.options.saveUrl;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (this.options.insertMetaCSP) {
|
|
535
|
+
const metaTag = this.doc.createElement("meta");
|
|
536
|
+
metaTag.httpEquiv = "content-security-policy";
|
|
537
|
+
metaTag.content = "default-src 'none'; font-src 'self' data:; img-src 'self' data:; style-src 'unsafe-inline'; media-src 'self' data:; script-src 'unsafe-inline' data:; object-src 'self' data:; frame-src 'self' data:;";
|
|
538
|
+
this.doc.head.appendChild(metaTag);
|
|
539
|
+
}
|
|
540
|
+
if (this.options.insertMetaNoIndex) {
|
|
541
|
+
let metaElement = this.doc.querySelector("meta[name=robots][content*=noindex]");
|
|
542
|
+
if (!metaElement) {
|
|
543
|
+
metaElement = this.doc.createElement("meta");
|
|
544
|
+
metaElement.setAttribute("name", "robots");
|
|
545
|
+
metaElement.setAttribute("content", "noindex");
|
|
546
|
+
this.doc.head.appendChild(metaElement);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
const styleElement = this.doc.createElement("style");
|
|
550
|
+
styleElement.textContent = "img[src=\"data:,\"],source[src=\"data:,\"]{display:none!important}";
|
|
551
|
+
this.doc.head.appendChild(styleElement);
|
|
552
|
+
let size;
|
|
553
|
+
if (this.options.displayStats) {
|
|
554
|
+
size = util.getContentSize(this.doc.documentElement.outerHTML);
|
|
555
|
+
}
|
|
556
|
+
const content = util.serialize(this.doc, this.options.compressHTML);
|
|
557
|
+
if (this.options.displayStats) {
|
|
558
|
+
const contentSize = util.getContentSize(content);
|
|
559
|
+
this.stats.set("processed", "HTML bytes", contentSize);
|
|
560
|
+
this.stats.add("discarded", "HTML bytes", size - contentSize);
|
|
561
|
+
}
|
|
562
|
+
const filename = await util.formatFilename(content, this.options);
|
|
563
|
+
const matchTitle = this.baseURI.match(/([^/]*)\/?(\.html?.*)$/) || this.baseURI.match(/\/\/([^/]*)\/?$/);
|
|
564
|
+
const pageData = {
|
|
565
|
+
stats: this.stats.data,
|
|
566
|
+
title: this.options.title || (this.baseURI && matchTitle ? matchTitle[1] : url.hostname ? url.hostname : ""),
|
|
567
|
+
filename,
|
|
568
|
+
content
|
|
569
|
+
};
|
|
570
|
+
if (this.options.addProof) {
|
|
571
|
+
pageData.hash = await util.digest("SHA-256", content);
|
|
572
|
+
}
|
|
573
|
+
if (this.options.retrieveLinks) {
|
|
574
|
+
pageData.links = Array.from(new Set(Array.from(this.doc.links).map(linkElement => linkElement.href)));
|
|
575
|
+
}
|
|
576
|
+
return pageData;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
preProcessPage() {
|
|
580
|
+
if (this.options.win) {
|
|
581
|
+
this.doc.body.querySelectorAll(":not(svg) title, meta, link[href][rel*=\"icon\"]").forEach(element => element instanceof this.options.win.HTMLElement && this.doc.head.appendChild(element));
|
|
582
|
+
}
|
|
583
|
+
if (this.options.images && !this.options.saveRawPage) {
|
|
584
|
+
this.doc.querySelectorAll("img[" + util.IMAGE_ATTRIBUTE_NAME + "]").forEach(imgElement => {
|
|
585
|
+
const attributeValue = imgElement.getAttribute(util.IMAGE_ATTRIBUTE_NAME);
|
|
586
|
+
if (attributeValue) {
|
|
587
|
+
const imageData = this.options.images[Number(attributeValue)];
|
|
588
|
+
if (imageData) {
|
|
589
|
+
if (this.options.removeHiddenElements && (
|
|
590
|
+
(imageData.size && !imageData.size.pxWidth && !imageData.size.pxHeight) ||
|
|
591
|
+
imgElement.getAttribute(util.HIDDEN_CONTENT_ATTRIBUTE_NAME) == "")
|
|
592
|
+
) {
|
|
593
|
+
imgElement.setAttribute("src", util.EMPTY_RESOURCE);
|
|
594
|
+
} else {
|
|
595
|
+
if (imageData.currentSrc) {
|
|
596
|
+
imgElement.dataset.singleFileOriginURL = imgElement.getAttribute("src");
|
|
597
|
+
imgElement.setAttribute("src", imageData.currentSrc);
|
|
598
|
+
}
|
|
599
|
+
if (this.options.loadDeferredImages) {
|
|
600
|
+
if ((!imgElement.getAttribute("src") || imgElement.getAttribute("src") == util.EMPTY_RESOURCE) && imgElement.getAttribute("data-src")) {
|
|
601
|
+
imageData.src = imgElement.dataset.src;
|
|
602
|
+
imgElement.setAttribute("src", imgElement.dataset.src);
|
|
603
|
+
imgElement.removeAttribute("data-src");
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
});
|
|
610
|
+
if (this.options.loadDeferredImages) {
|
|
611
|
+
this.doc.querySelectorAll("img[data-srcset]").forEach(imgElement => {
|
|
612
|
+
if (!imgElement.getAttribute("srcset") && imgElement.getAttribute("data-srcset")) {
|
|
613
|
+
imgElement.setAttribute("srcset", imgElement.dataset.srcset);
|
|
614
|
+
imgElement.removeAttribute("data-srcset");
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
replaceStyleContents() {
|
|
622
|
+
if (this.options.stylesheets) {
|
|
623
|
+
this.doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
|
|
624
|
+
const attributeValue = styleElement.getAttribute(util.STYLESHEET_ATTRIBUTE_NAME);
|
|
625
|
+
if (attributeValue) {
|
|
626
|
+
const stylesheetContent = this.options.stylesheets[Number(styleIndex)];
|
|
627
|
+
if (stylesheetContent) {
|
|
628
|
+
styleElement.textContent = stylesheetContent;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
if (this.options.adoptedStyleSheets && this.options.adoptedStyleSheets.length) {
|
|
634
|
+
const styleElement = this.doc.createElement("style");
|
|
635
|
+
styleElement.textContent = this.options.adoptedStyleSheets.join("\n");
|
|
636
|
+
this.doc.body.appendChild(styleElement);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
removeUnselectedElements() {
|
|
641
|
+
removeUnmarkedElements(this.doc.body);
|
|
642
|
+
this.doc.body.removeAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME);
|
|
643
|
+
|
|
644
|
+
function removeUnmarkedElements(element) {
|
|
645
|
+
let selectedElementFound = false;
|
|
646
|
+
Array.from(element.childNodes).forEach(node => {
|
|
647
|
+
if (node.nodeType == 1) {
|
|
648
|
+
const isSelectedElement = node.getAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME) == "";
|
|
649
|
+
selectedElementFound = selectedElementFound || isSelectedElement;
|
|
650
|
+
if (isSelectedElement) {
|
|
651
|
+
node.removeAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME);
|
|
652
|
+
removeUnmarkedElements(node);
|
|
653
|
+
} else if (selectedElementFound) {
|
|
654
|
+
removeNode(node);
|
|
655
|
+
} else {
|
|
656
|
+
hideNode(node);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
function removeNode(node) {
|
|
663
|
+
if ((node.nodeType != 1 || !node.querySelector("svg,style,link")) && canHideNode(node)) {
|
|
664
|
+
node.remove();
|
|
665
|
+
} else {
|
|
666
|
+
hideNode(node);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function hideNode(node) {
|
|
671
|
+
if (canHideNode(node)) {
|
|
672
|
+
node.style.setProperty("display", "none", "important");
|
|
673
|
+
node.removeAttribute("src");
|
|
674
|
+
node.removeAttribute("srcset");
|
|
675
|
+
node.removeAttribute("srcdoc");
|
|
676
|
+
Array.from(node.childNodes).forEach(removeNode);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
function canHideNode(node) {
|
|
681
|
+
if (node.nodeType == 1) {
|
|
682
|
+
const tagName = node.tagName && node.tagName.toUpperCase();
|
|
683
|
+
return tagName != "SVG" && tagName != "STYLE" && tagName != "LINK";
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
insertVideoPosters() {
|
|
689
|
+
if (this.options.posters) {
|
|
690
|
+
this.doc.querySelectorAll("video, video > source").forEach(element => {
|
|
691
|
+
let videoElement;
|
|
692
|
+
if (element.tagName.toUpperCase() == "VIDEO") {
|
|
693
|
+
videoElement = element;
|
|
694
|
+
} else {
|
|
695
|
+
videoElement = element.parentElement;
|
|
696
|
+
}
|
|
697
|
+
const attributeValue = element.getAttribute(util.POSTER_ATTRIBUTE_NAME);
|
|
698
|
+
if (attributeValue) {
|
|
699
|
+
const posterURL = this.options.posters[Number(attributeValue)];
|
|
700
|
+
if (!videoElement.getAttribute("poster") && posterURL) {
|
|
701
|
+
videoElement.setAttribute("poster", posterURL);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
insertVideoLinks() {
|
|
709
|
+
const LINK_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAgMAAADXB5lNAAABhmlDQ1BJQ0MgcHJvZmlsZQAAKJF9kj1Iw0AYht+mSkUrDnYQcchQnSyIijqWKhbBQmkrtOpgcukfNGlIUlwcBdeCgz+LVQcXZ10dXAVB8AfEydFJ0UVK/C4ptIjx4LiH9+59+e67A4RGhalm1wSgapaRisfEbG5VDLyiDwEAvZiVmKkn0osZeI6ve/j4ehfhWd7n/hz9St5kgE8kjjLdsIg3iGc2LZ3zPnGIlSSF+Jx43KACiR+5Lrv8xrnosMAzQ0YmNU8cIhaLHSx3MCsZKvE0cVhRNcoXsi4rnLc4q5Uaa9XJbxjMaytprtMcQRxLSCAJETJqKKMCCxFaNVJMpGg/5uEfdvxJcsnkKoORYwFVqJAcP/gb/O6tWZiadJOCMaD7xbY/RoHALtCs2/b3sW03TwD/M3Cltf3VBjD3SXq9rYWPgIFt4OK6rcl7wOUOMPSkS4bkSH6aQqEAvJ/RM+WAwVv6EGtu31r7OH0AMtSr5Rvg4BAYK1L2use9ezr79u+ZVv9+AFlNcp0UUpiqAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH5AsHAB8H+DhhoQAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAJUExURQAAAICHi4qKioTuJAkAAAABdFJOUwBA5thmAAAAAWJLR0QCZgt8ZAAAAJJJREFUOI3t070NRCEMA2CnYAOyDyPwpHj/Va7hJ3FzV7zy3ET5JIwoAF6Jk4wzAJAkzxAYG9YRTgB+24wBgKmfrGAKTcEfAY4KRlRoIeBTgKOCERVaCPgU4Khge2GqKOBTgKOCERVaAEC/4PNcnyoSWHpjqkhwKxbcig0Q6AorXYF/+A6eIYD1lVbwG/jdA6/kA2THRAURVubcAAAAAElFTkSuQmCC";
|
|
710
|
+
const ICON_SIZE = "16px";
|
|
711
|
+
this.doc.querySelectorAll("video").forEach(videoElement => {
|
|
712
|
+
const attributeValue = videoElement.getAttribute(util.VIDEO_ATTRIBUTE_NAME);
|
|
713
|
+
if (attributeValue) {
|
|
714
|
+
const videoData = this.options.videos[Number(attributeValue)];
|
|
715
|
+
const src = videoData.src || videoElement.src;
|
|
716
|
+
if (videoElement && src) {
|
|
717
|
+
const linkElement = this.doc.createElement("a");
|
|
718
|
+
const imgElement = this.doc.createElement("img");
|
|
719
|
+
linkElement.href = src;
|
|
720
|
+
linkElement.target = "_blank";
|
|
721
|
+
linkElement.style.setProperty("z-index", 2147483647, "important");
|
|
722
|
+
linkElement.style.setProperty("position", "absolute", "important");
|
|
723
|
+
linkElement.style.setProperty("top", "8px", "important");
|
|
724
|
+
linkElement.style.setProperty("left", "8px", "important");
|
|
725
|
+
linkElement.style.setProperty("width", ICON_SIZE, "important");
|
|
726
|
+
linkElement.style.setProperty("height", ICON_SIZE, "important");
|
|
727
|
+
linkElement.style.setProperty("min-width", ICON_SIZE, "important");
|
|
728
|
+
linkElement.style.setProperty("min-height", ICON_SIZE, "important");
|
|
729
|
+
linkElement.style.setProperty("max-width", ICON_SIZE, "important");
|
|
730
|
+
linkElement.style.setProperty("max-height", ICON_SIZE, "important");
|
|
731
|
+
imgElement.src = LINK_ICON;
|
|
732
|
+
imgElement.style.setProperty("width", ICON_SIZE, "important");
|
|
733
|
+
imgElement.style.setProperty("height", ICON_SIZE, "important");
|
|
734
|
+
imgElement.style.setProperty("min-width", ICON_SIZE, "important");
|
|
735
|
+
imgElement.style.setProperty("min-height", ICON_SIZE, "important");
|
|
736
|
+
imgElement.style.setProperty("max-width", ICON_SIZE, "important");
|
|
737
|
+
imgElement.style.setProperty("max-height", ICON_SIZE, "important");
|
|
738
|
+
linkElement.appendChild(imgElement);
|
|
739
|
+
videoElement.insertAdjacentElement("afterend", linkElement);
|
|
740
|
+
const positionInlineParent = videoElement.parentNode.style.getPropertyValue("position");
|
|
741
|
+
if ((!videoData.positionParent && (!positionInlineParent || positionInlineParent != "static")) || videoData.positionParent == "static") {
|
|
742
|
+
videoElement.parentNode.style.setProperty("position", "relative", "important");
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
removeFrames() {
|
|
750
|
+
const frameElements = this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]");
|
|
751
|
+
this.stats.set("discarded", "frames", frameElements.length);
|
|
752
|
+
this.stats.set("processed", "frames", frameElements.length);
|
|
753
|
+
this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]").forEach(element => element.remove());
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
removeEmbedScripts() {
|
|
757
|
+
const JAVASCRIPT_URI_PREFIX = "javascript:";
|
|
758
|
+
const DISABLED_SCRIPT = "javascript:void(0)";
|
|
759
|
+
this.onEventAttributeNames.forEach(attributeName => this.doc.querySelectorAll("[" + attributeName + "]").forEach(element => element.removeAttribute(attributeName)));
|
|
760
|
+
this.doc.querySelectorAll("[href]").forEach(element => {
|
|
761
|
+
if (element.href && element.href.match && element.href.trim().startsWith(JAVASCRIPT_URI_PREFIX)) {
|
|
762
|
+
element.setAttribute("href", DISABLED_SCRIPT);
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
this.doc.querySelectorAll("[src]").forEach(element => {
|
|
766
|
+
if (element.src && element.src.trim().startsWith(JAVASCRIPT_URI_PREFIX)) {
|
|
767
|
+
element.setAttribute("src", DISABLED_SCRIPT);
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
const scriptElements = this.doc.querySelectorAll("script:not([type=\"application/ld+json\"]):not([" + SCRIPT_TEMPLATE_SHADOW_ROOT + "])");
|
|
771
|
+
this.stats.set("discarded", "scripts", scriptElements.length);
|
|
772
|
+
this.stats.set("processed", "scripts", scriptElements.length);
|
|
773
|
+
scriptElements.forEach(element => element.remove());
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
removeDiscardedResources() {
|
|
777
|
+
this.doc.querySelectorAll("." + util.SINGLE_FILE_UI_ELEMENT_CLASS).forEach(element => element.remove());
|
|
778
|
+
const noscriptPlaceholders = new Map();
|
|
779
|
+
this.doc.querySelectorAll("noscript").forEach(noscriptElement => {
|
|
780
|
+
const placeholderElement = this.doc.createElement("div");
|
|
781
|
+
placeholderElement.innerHTML = noscriptElement.dataset.singleFileDisabledNoscript;
|
|
782
|
+
noscriptElement.replaceWith(placeholderElement);
|
|
783
|
+
noscriptPlaceholders.set(placeholderElement, noscriptElement);
|
|
784
|
+
});
|
|
785
|
+
this.doc.querySelectorAll("meta[http-equiv=refresh], meta[disabled-http-equiv]").forEach(element => element.remove());
|
|
786
|
+
noscriptPlaceholders.forEach((noscriptElement, placeholderElement) => {
|
|
787
|
+
noscriptElement.dataset.singleFileDisabledNoscript = placeholderElement.innerHTML;
|
|
788
|
+
placeholderElement.replaceWith(noscriptElement);
|
|
789
|
+
});
|
|
790
|
+
this.doc.querySelectorAll("meta[http-equiv=\"content-security-policy\"]").forEach(element => element.remove());
|
|
791
|
+
const objectElements = this.doc.querySelectorAll("applet, object[data]:not([type=\"image/svg+xml\"]):not([type=\"image/svg-xml\"]):not([type=\"text/html\"]):not([data*=\".svg\"]):not([data*=\".pdf\"]), embed[src]:not([src*=\".svg\"]):not([src*=\".pdf\"])");
|
|
792
|
+
this.stats.set("discarded", "objects", objectElements.length);
|
|
793
|
+
this.stats.set("processed", "objects", objectElements.length);
|
|
794
|
+
objectElements.forEach(element => element.remove());
|
|
795
|
+
const replacedAttributeValue = this.doc.querySelectorAll("link[rel~=preconnect], link[rel~=prerender], link[rel~=dns-prefetch], link[rel~=preload], link[rel~=manifest], link[rel~=prefetch]");
|
|
796
|
+
replacedAttributeValue.forEach(element => {
|
|
797
|
+
const relValue = element
|
|
798
|
+
.getAttribute("rel")
|
|
799
|
+
.replace(/(preconnect|prerender|dns-prefetch|preload|prefetch|manifest)/g, "")
|
|
800
|
+
.trim();
|
|
801
|
+
if (relValue.length) {
|
|
802
|
+
element.setAttribute("rel", relValue);
|
|
803
|
+
} else {
|
|
804
|
+
element.remove();
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
this.doc.querySelectorAll("link[rel*=stylesheet][rel*=alternate][title],link[rel*=stylesheet]:not([href]),link[rel*=stylesheet][href=\"\"]").forEach(element => element.remove());
|
|
808
|
+
if (this.options.removeHiddenElements) {
|
|
809
|
+
this.doc.querySelectorAll("input[type=hidden]").forEach(element => element.remove());
|
|
810
|
+
}
|
|
811
|
+
if (!this.options.saveFavicon) {
|
|
812
|
+
this.doc.querySelectorAll("link[rel*=\"icon\"]").forEach(element => element.remove());
|
|
813
|
+
}
|
|
814
|
+
this.doc.querySelectorAll("a[ping]").forEach(element => element.removeAttribute("ping"));
|
|
815
|
+
this.doc.querySelectorAll("link[rel=import][href]").forEach(element => element.remove());
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
replaceInvalidElements() {
|
|
819
|
+
this.doc.querySelectorAll("template[" + util.INVALID_ELEMENT_ATTRIBUTE_NAME + "]").forEach(templateElement => {
|
|
820
|
+
const placeHolderElement = this.doc.createElement("span");
|
|
821
|
+
if (templateElement.content) {
|
|
822
|
+
const originalElement = templateElement.content.firstChild;
|
|
823
|
+
if (originalElement) {
|
|
824
|
+
if (originalElement.hasAttributes()) {
|
|
825
|
+
Array.from(originalElement.attributes).forEach(attribute => placeHolderElement.setAttribute(attribute.name, attribute.value));
|
|
826
|
+
}
|
|
827
|
+
originalElement.childNodes.forEach(childNode => placeHolderElement.appendChild(childNode.cloneNode(true)));
|
|
828
|
+
}
|
|
829
|
+
templateElement.replaceWith(placeHolderElement);
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
resetCharsetMeta() {
|
|
835
|
+
let charset;
|
|
836
|
+
this.doc.querySelectorAll("meta[charset], meta[http-equiv=\"content-type\"]").forEach(element => {
|
|
837
|
+
const charsetDeclaration = element.content.split(";")[1];
|
|
838
|
+
if (charsetDeclaration && !charset) {
|
|
839
|
+
charset = charsetDeclaration.split("=")[1];
|
|
840
|
+
if (charset) {
|
|
841
|
+
this.charset = charset.trim().toLowerCase();
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
element.remove();
|
|
845
|
+
});
|
|
846
|
+
const metaElement = this.doc.createElement("meta");
|
|
847
|
+
metaElement.setAttribute("charset", UTF8_CHARSET);
|
|
848
|
+
if (this.doc.head.firstChild) {
|
|
849
|
+
this.doc.head.insertBefore(metaElement, this.doc.head.firstChild);
|
|
850
|
+
} else {
|
|
851
|
+
this.doc.head.appendChild(metaElement);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
setInputValues() {
|
|
856
|
+
this.doc.querySelectorAll("input:not([type=radio]):not([type=checkbox])").forEach(input => {
|
|
857
|
+
const value = input.getAttribute(util.INPUT_VALUE_ATTRIBUTE_NAME);
|
|
858
|
+
input.setAttribute("value", value || "");
|
|
859
|
+
});
|
|
860
|
+
this.doc.querySelectorAll("input[type=radio], input[type=checkbox]").forEach(input => {
|
|
861
|
+
const value = input.getAttribute(util.INPUT_VALUE_ATTRIBUTE_NAME);
|
|
862
|
+
if (value == "true") {
|
|
863
|
+
input.setAttribute("checked", "");
|
|
864
|
+
}
|
|
865
|
+
});
|
|
866
|
+
this.doc.querySelectorAll("textarea").forEach(textarea => {
|
|
867
|
+
const value = textarea.getAttribute(util.INPUT_VALUE_ATTRIBUTE_NAME);
|
|
868
|
+
textarea.textContent = value || "";
|
|
869
|
+
});
|
|
870
|
+
this.doc.querySelectorAll("select").forEach(select => {
|
|
871
|
+
select.querySelectorAll("option").forEach(option => {
|
|
872
|
+
const selected = option.getAttribute(util.INPUT_VALUE_ATTRIBUTE_NAME) != null;
|
|
873
|
+
if (selected) {
|
|
874
|
+
option.setAttribute("selected", "");
|
|
875
|
+
}
|
|
876
|
+
});
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
moveStylesInHead() {
|
|
881
|
+
this.doc.querySelectorAll("style").forEach(stylesheet => {
|
|
882
|
+
if (stylesheet.getAttribute(util.STYLE_ATTRIBUTE_NAME) == "") {
|
|
883
|
+
this.doc.head.appendChild(stylesheet);
|
|
884
|
+
}
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
saveFavicon() {
|
|
889
|
+
let faviconElement = this.doc.querySelector("link[href][rel=\"shortcut icon\"]");
|
|
890
|
+
if (!faviconElement) {
|
|
891
|
+
faviconElement = this.doc.querySelector("link[href][rel=\"icon\"]");
|
|
892
|
+
}
|
|
893
|
+
if (!faviconElement) {
|
|
894
|
+
faviconElement = this.doc.createElement("link");
|
|
895
|
+
faviconElement.setAttribute("type", "image/x-icon");
|
|
896
|
+
faviconElement.setAttribute("rel", "shortcut icon");
|
|
897
|
+
faviconElement.setAttribute("href", "/favicon.ico");
|
|
898
|
+
}
|
|
899
|
+
this.doc.head.appendChild(faviconElement);
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
saveScrollPosition() {
|
|
903
|
+
if (this.options.scrollPosition && this.options.scrolling == "no" && (this.options.scrollPosition.x || this.options.scrollPosition.y)) {
|
|
904
|
+
const scriptElement = this.doc.createElement("script");
|
|
905
|
+
scriptElement.textContent = "document.currentScript.remove();addEventListener(\"load\",()=>scrollTo(" + this.options.scrollPosition.x + "," + this.options.scrollPosition.y + "))";
|
|
906
|
+
this.doc.body.appendChild(scriptElement);
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
replaceCanvasElements() {
|
|
911
|
+
if (this.options.canvases) {
|
|
912
|
+
this.doc.querySelectorAll("canvas").forEach(canvasElement => {
|
|
913
|
+
const attributeValue = canvasElement.getAttribute(util.CANVAS_ATTRIBUTE_NAME);
|
|
914
|
+
if (attributeValue) {
|
|
915
|
+
const canvasData = this.options.canvases[Number(attributeValue)];
|
|
916
|
+
if (canvasData) {
|
|
917
|
+
const backgroundStyle = {};
|
|
918
|
+
if (canvasData.backgroundColor) {
|
|
919
|
+
backgroundStyle["background-color"] = canvasData.backgroundColor;
|
|
920
|
+
}
|
|
921
|
+
ProcessorHelper.setBackgroundImage(canvasElement, "url(" + canvasData.dataURI + ")", backgroundStyle);
|
|
922
|
+
this.stats.add("processed", "canvas", 1);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
insertFonts() {
|
|
930
|
+
if (this.options.fonts && this.options.fonts.length) {
|
|
931
|
+
let firstStylesheet = this.doc.querySelector("style, link[rel=stylesheet]"), previousStyleElement;
|
|
932
|
+
this.options.fonts.forEach(fontData => {
|
|
933
|
+
if (fontData["font-family"] && fontData.src) {
|
|
934
|
+
let stylesheetContent = "@font-face{";
|
|
935
|
+
let stylesContent = "";
|
|
936
|
+
Object.keys(fontData).forEach(fontStyle => {
|
|
937
|
+
if (stylesContent) {
|
|
938
|
+
stylesContent += ";";
|
|
939
|
+
}
|
|
940
|
+
stylesContent += fontStyle + ":" + fontData[fontStyle];
|
|
941
|
+
});
|
|
942
|
+
stylesheetContent += stylesContent + "}";
|
|
943
|
+
const styleElement = this.doc.createElement("style");
|
|
944
|
+
styleElement.textContent = stylesheetContent;
|
|
945
|
+
if (previousStyleElement) {
|
|
946
|
+
previousStyleElement.insertAdjacentElement("afterend", styleElement);
|
|
947
|
+
} else if (firstStylesheet) {
|
|
948
|
+
firstStylesheet.parentElement.insertBefore(styleElement, firstStylesheet);
|
|
949
|
+
} else {
|
|
950
|
+
this.doc.head.appendChild(styleElement);
|
|
951
|
+
}
|
|
952
|
+
previousStyleElement = styleElement;
|
|
953
|
+
}
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
removeHiddenElements() {
|
|
959
|
+
const hiddenElements = this.doc.querySelectorAll("[" + util.HIDDEN_CONTENT_ATTRIBUTE_NAME + "]");
|
|
960
|
+
const removedElements = this.doc.querySelectorAll("[" + util.REMOVED_CONTENT_ATTRIBUTE_NAME + "]");
|
|
961
|
+
this.stats.set("discarded", "hidden elements", removedElements.length);
|
|
962
|
+
this.stats.set("processed", "hidden elements", removedElements.length);
|
|
963
|
+
if (hiddenElements.length) {
|
|
964
|
+
const styleElement = this.doc.createElement("style");
|
|
965
|
+
styleElement.textContent = ".sf-hidden{display:none!important;}";
|
|
966
|
+
this.doc.head.appendChild(styleElement);
|
|
967
|
+
hiddenElements.forEach(element => {
|
|
968
|
+
if (element.style.getPropertyValue("display") != "none") {
|
|
969
|
+
if (element.style.getPropertyPriority("display") == "important") {
|
|
970
|
+
element.style.setProperty("display", "none", "important");
|
|
971
|
+
} else {
|
|
972
|
+
element.classList.add("sf-hidden");
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
removedElements.forEach(element => element.remove());
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
resolveHrefs() {
|
|
981
|
+
this.doc.querySelectorAll("a[href], area[href], link[href]").forEach(element => {
|
|
982
|
+
const href = element.getAttribute("href").trim();
|
|
983
|
+
if (element.tagName.toUpperCase() == "LINK" && element.rel.includes("stylesheet")) {
|
|
984
|
+
if (this.options.saveOriginalURLs && !isDataURL(href)) {
|
|
985
|
+
element.setAttribute("data-sf-original-href", href);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
if (!testIgnoredPath(href)) {
|
|
989
|
+
let resolvedURL;
|
|
990
|
+
try {
|
|
991
|
+
resolvedURL = util.resolveURL(href, this.options.baseURI || this.options.url);
|
|
992
|
+
} catch (error) {
|
|
993
|
+
// ignored
|
|
994
|
+
}
|
|
995
|
+
if (resolvedURL) {
|
|
996
|
+
const url = normalizeURL(this.options.url);
|
|
997
|
+
if (resolvedURL.startsWith(url + "#") && !resolvedURL.startsWith(url + "#!") && !this.options.resolveFragmentIdentifierURLs) {
|
|
998
|
+
resolvedURL = resolvedURL.substring(url.length);
|
|
999
|
+
}
|
|
1000
|
+
try {
|
|
1001
|
+
element.setAttribute("href", resolvedURL);
|
|
1002
|
+
} catch (error) {
|
|
1003
|
+
// ignored
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
});
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
async insertMissingVideoPosters() {
|
|
1011
|
+
await Promise.all(Array.from(this.doc.querySelectorAll("video[src], video > source[src]")).map(async element => {
|
|
1012
|
+
let videoElement;
|
|
1013
|
+
if (element.tagName.toUpperCase() == "VIDEO") {
|
|
1014
|
+
videoElement = element;
|
|
1015
|
+
} else {
|
|
1016
|
+
videoElement = element.parentElement;
|
|
1017
|
+
}
|
|
1018
|
+
if (!videoElement.poster) {
|
|
1019
|
+
const attributeValue = videoElement.getAttribute(util.VIDEO_ATTRIBUTE_NAME);
|
|
1020
|
+
if (attributeValue) {
|
|
1021
|
+
const videoData = this.options.videos[Number(attributeValue)];
|
|
1022
|
+
const src = videoData.src || videoElement.src;
|
|
1023
|
+
if (src) {
|
|
1024
|
+
const temporaryVideoElement = this.doc.createElement("video");
|
|
1025
|
+
temporaryVideoElement.src = src;
|
|
1026
|
+
temporaryVideoElement.style.setProperty("width", videoData.size.pxWidth + "px", "important");
|
|
1027
|
+
temporaryVideoElement.style.setProperty("height", videoData.size.pxHeight + "px", "important");
|
|
1028
|
+
temporaryVideoElement.style.setProperty("display", "none", "important");
|
|
1029
|
+
temporaryVideoElement.crossOrigin = "anonymous";
|
|
1030
|
+
const canvasElement = this.doc.createElement("canvas");
|
|
1031
|
+
const context = canvasElement.getContext("2d");
|
|
1032
|
+
this.options.doc.body.appendChild(temporaryVideoElement);
|
|
1033
|
+
return new Promise(resolve => {
|
|
1034
|
+
temporaryVideoElement.currentTime = videoData.currentTime;
|
|
1035
|
+
temporaryVideoElement.oncanplay = () => {
|
|
1036
|
+
canvasElement.width = videoData.size.pxWidth;
|
|
1037
|
+
canvasElement.height = videoData.size.pxHeight;
|
|
1038
|
+
context.drawImage(temporaryVideoElement, 0, 0, canvasElement.width, canvasElement.height);
|
|
1039
|
+
try {
|
|
1040
|
+
videoElement.poster = canvasElement.toDataURL("image/png", "");
|
|
1041
|
+
} catch (error) {
|
|
1042
|
+
// ignored
|
|
1043
|
+
}
|
|
1044
|
+
temporaryVideoElement.remove();
|
|
1045
|
+
resolve();
|
|
1046
|
+
};
|
|
1047
|
+
temporaryVideoElement.onerror = () => {
|
|
1048
|
+
temporaryVideoElement.remove();
|
|
1049
|
+
resolve();
|
|
1050
|
+
};
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
}));
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
resolveStyleAttributeURLs() {
|
|
1059
|
+
this.doc.querySelectorAll("[style]").forEach(element => {
|
|
1060
|
+
if (this.options.blockStylesheets) {
|
|
1061
|
+
element.removeAttribute("style");
|
|
1062
|
+
} else {
|
|
1063
|
+
const styleContent = element.getAttribute("style");
|
|
1064
|
+
const declarationList = cssTree.parse(styleContent, { context: "declarationList", parseCustomProperty: true });
|
|
1065
|
+
ProcessorHelper.resolveStylesheetURLs(declarationList, this.baseURI, this.workStyleElement);
|
|
1066
|
+
this.styles.set(element, declarationList);
|
|
1067
|
+
}
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
async resolveStylesheetURLs() {
|
|
1072
|
+
await Promise.all(Array.from(this.doc.querySelectorAll("style, link[rel*=stylesheet]")).map(async element => {
|
|
1073
|
+
const options = Object.assign({}, this.options, { charset: this.charset });
|
|
1074
|
+
let mediaText;
|
|
1075
|
+
if (element.media) {
|
|
1076
|
+
mediaText = element.media.toLowerCase();
|
|
1077
|
+
}
|
|
1078
|
+
const scoped = Boolean(element.closest("[" + SHADOWROOT_ATTRIBUTE_NAME + "]"));
|
|
1079
|
+
const stylesheetInfo = {
|
|
1080
|
+
mediaText,
|
|
1081
|
+
scoped
|
|
1082
|
+
};
|
|
1083
|
+
if (element.tagName.toUpperCase() == "LINK" && element.charset) {
|
|
1084
|
+
options.charset = element.charset;
|
|
1085
|
+
}
|
|
1086
|
+
await processElement(element, stylesheetInfo, this.stylesheets, this.baseURI, options, this.workStyleElement);
|
|
1087
|
+
}));
|
|
1088
|
+
if (this.options.rootDocument) {
|
|
1089
|
+
const newResources = Object.keys(this.options.updatedResources)
|
|
1090
|
+
.filter(url => this.options.updatedResources[url].type == "stylesheet" && !this.options.updatedResources[url].retrieved)
|
|
1091
|
+
.map(url => this.options.updatedResources[url]);
|
|
1092
|
+
await Promise.all(newResources.map(async resource => {
|
|
1093
|
+
resource.retrieved = true;
|
|
1094
|
+
if (!this.options.blockStylesheets) {
|
|
1095
|
+
const stylesheetInfo = {};
|
|
1096
|
+
const element = this.doc.createElement("style");
|
|
1097
|
+
this.doc.body.appendChild(element);
|
|
1098
|
+
element.textContent = resource.content;
|
|
1099
|
+
await processElement(element, stylesheetInfo, this.stylesheets, this.baseURI, this.options, this.workStyleElement);
|
|
1100
|
+
}
|
|
1101
|
+
}));
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
async function processElement(element, stylesheetInfo, stylesheets, baseURI, options, workStyleElement) {
|
|
1105
|
+
let stylesheet;
|
|
1106
|
+
stylesheets.set(element, stylesheetInfo);
|
|
1107
|
+
if (!options.blockStylesheets) {
|
|
1108
|
+
if (element.tagName.toUpperCase() == "LINK") {
|
|
1109
|
+
stylesheet = await ProcessorHelper.resolveLinkStylesheetURLs(element.href, baseURI, options, workStyleElement);
|
|
1110
|
+
} else {
|
|
1111
|
+
stylesheet = cssTree.parse(element.textContent, { context: "stylesheet", parseCustomProperty: true });
|
|
1112
|
+
const importFound = await ProcessorHelper.resolveImportURLs(stylesheet, baseURI, options, workStyleElement);
|
|
1113
|
+
if (importFound) {
|
|
1114
|
+
stylesheet = cssTree.parse(cssTree.generate(stylesheet), { context: "stylesheet", parseCustomProperty: true });
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
if (stylesheet && stylesheet.children) {
|
|
1119
|
+
if (options.compressCSS) {
|
|
1120
|
+
ProcessorHelper.removeSingleLineCssComments(stylesheet);
|
|
1121
|
+
}
|
|
1122
|
+
ProcessorHelper.replacePseudoClassDefined(stylesheet);
|
|
1123
|
+
stylesheetInfo.stylesheet = stylesheet;
|
|
1124
|
+
} else {
|
|
1125
|
+
stylesheets.delete(element);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
async resolveFrameURLs() {
|
|
1131
|
+
if (!this.options.saveRawPage) {
|
|
1132
|
+
const frameElements = Array.from(this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"));
|
|
1133
|
+
await Promise.all(frameElements.map(async frameElement => {
|
|
1134
|
+
if (frameElement.tagName.toUpperCase() == "OBJECT") {
|
|
1135
|
+
frameElement.setAttribute("data", "data:text/html,");
|
|
1136
|
+
} else {
|
|
1137
|
+
const src = frameElement.getAttribute("src");
|
|
1138
|
+
if (this.options.saveOriginalURLs && src && !isDataURL(src)) {
|
|
1139
|
+
frameElement.setAttribute("data-sf-original-src", src);
|
|
1140
|
+
}
|
|
1141
|
+
frameElement.removeAttribute("src");
|
|
1142
|
+
frameElement.removeAttribute("srcdoc");
|
|
1143
|
+
}
|
|
1144
|
+
Array.from(frameElement.childNodes).forEach(node => node.remove());
|
|
1145
|
+
const frameWindowId = frameElement.getAttribute(util.WIN_ID_ATTRIBUTE_NAME);
|
|
1146
|
+
if (this.options.frames && frameWindowId) {
|
|
1147
|
+
const frameData = this.options.frames.find(frame => frame.windowId == frameWindowId);
|
|
1148
|
+
if (frameData) {
|
|
1149
|
+
await initializeProcessor(frameData, frameElement, frameWindowId, this.batchRequest, Object.create(this.options));
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
}));
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
async function initializeProcessor(frameData, frameElement, frameWindowId, batchRequest, options) {
|
|
1156
|
+
options.insertSingleFileComment = false;
|
|
1157
|
+
options.insertCanonicalLink = false;
|
|
1158
|
+
options.insertMetaNoIndex = false;
|
|
1159
|
+
options.saveFavicon = false;
|
|
1160
|
+
options.includeInfobar = false;
|
|
1161
|
+
options.url = frameData.baseURI;
|
|
1162
|
+
options.windowId = frameWindowId;
|
|
1163
|
+
if (frameData.content) {
|
|
1164
|
+
options.content = frameData.content;
|
|
1165
|
+
options.canvases = frameData.canvases;
|
|
1166
|
+
options.fonts = frameData.fonts;
|
|
1167
|
+
options.stylesheets = frameData.stylesheets;
|
|
1168
|
+
options.images = frameData.images;
|
|
1169
|
+
options.posters = frameData.posters;
|
|
1170
|
+
options.videos = frameData.videos;
|
|
1171
|
+
options.usedFonts = frameData.usedFonts;
|
|
1172
|
+
options.shadowRoots = frameData.shadowRoots;
|
|
1173
|
+
options.scrollPosition = frameData.scrollPosition;
|
|
1174
|
+
options.scrolling = frameData.scrolling;
|
|
1175
|
+
options.adoptedStyleSheets = frameData.adoptedStyleSheets;
|
|
1176
|
+
frameData.runner = new Runner(options);
|
|
1177
|
+
frameData.frameElement = frameElement;
|
|
1178
|
+
await frameData.runner.loadPage();
|
|
1179
|
+
await frameData.runner.initialize();
|
|
1180
|
+
frameData.maxResources = batchRequest.getMaxResources();
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
insertShadowRootContents() {
|
|
1186
|
+
const doc = this.doc;
|
|
1187
|
+
const options = this.options;
|
|
1188
|
+
if (options.shadowRoots && options.shadowRoots.length) {
|
|
1189
|
+
processElement(this.doc);
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
function processElement(element) {
|
|
1193
|
+
const shadowRootElements = Array.from(element.querySelectorAll("[" + util.SHADOW_ROOT_ATTRIBUTE_NAME + "]"));
|
|
1194
|
+
shadowRootElements.forEach(element => {
|
|
1195
|
+
const attributeValue = element.getAttribute(util.SHADOW_ROOT_ATTRIBUTE_NAME);
|
|
1196
|
+
if (attributeValue) {
|
|
1197
|
+
const shadowRootData = options.shadowRoots[Number(attributeValue)];
|
|
1198
|
+
if (shadowRootData) {
|
|
1199
|
+
const templateElement = doc.createElement("template");
|
|
1200
|
+
templateElement.setAttribute(SHADOWROOT_ATTRIBUTE_NAME, shadowRootData.mode);
|
|
1201
|
+
if (shadowRootData.adoptedStyleSheets && shadowRootData.adoptedStyleSheets.length) {
|
|
1202
|
+
shadowRootData.adoptedStyleSheets.forEach(stylesheetContent => {
|
|
1203
|
+
const styleElement = doc.createElement("style");
|
|
1204
|
+
styleElement.textContent = stylesheetContent;
|
|
1205
|
+
templateElement.appendChild(styleElement);
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1208
|
+
const shadowDoc = util.parseDocContent(shadowRootData.content);
|
|
1209
|
+
if (shadowDoc.head) {
|
|
1210
|
+
const metaCharset = shadowDoc.head.querySelector("meta[charset]");
|
|
1211
|
+
if (metaCharset) {
|
|
1212
|
+
metaCharset.remove();
|
|
1213
|
+
}
|
|
1214
|
+
shadowDoc.head.childNodes.forEach(node => templateElement.appendChild(shadowDoc.importNode(node, true)));
|
|
1215
|
+
}
|
|
1216
|
+
if (shadowDoc.body) {
|
|
1217
|
+
shadowDoc.body.childNodes.forEach(node => templateElement.appendChild(shadowDoc.importNode(node, true)));
|
|
1218
|
+
}
|
|
1219
|
+
processElement(templateElement);
|
|
1220
|
+
if (element.firstChild) {
|
|
1221
|
+
element.insertBefore(templateElement, element.firstChild);
|
|
1222
|
+
} else {
|
|
1223
|
+
element.appendChild(templateElement);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
removeUnusedStyles() {
|
|
1232
|
+
if (!this.mediaAllInfo) {
|
|
1233
|
+
this.mediaAllInfo = util.getMediaAllInfo(this.doc, this.stylesheets, this.styles);
|
|
1234
|
+
}
|
|
1235
|
+
const stats = util.minifyCSSRules(this.stylesheets, this.styles, this.mediaAllInfo);
|
|
1236
|
+
this.stats.set("processed", "CSS rules", stats.processed);
|
|
1237
|
+
this.stats.set("discarded", "CSS rules", stats.discarded);
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
removeUnusedFonts() {
|
|
1241
|
+
util.removeUnusedFonts(this.doc, this.stylesheets, this.styles, this.options);
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
removeAlternativeMedias() {
|
|
1245
|
+
const stats = util.minifyMedias(this.stylesheets);
|
|
1246
|
+
this.stats.set("processed", "medias", stats.processed);
|
|
1247
|
+
this.stats.set("discarded", "medias", stats.discarded);
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
async processStylesheets() {
|
|
1251
|
+
this.options.fontDeclarations = new Map();
|
|
1252
|
+
await Promise.all([...this.stylesheets].map(([, stylesheetInfo]) =>
|
|
1253
|
+
ProcessorHelper.processStylesheet(stylesheetInfo.stylesheet.children, this.baseURI, this.options, this.cssVariables, this.batchRequest)
|
|
1254
|
+
));
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
async processStyleAttributes() {
|
|
1258
|
+
return Promise.all([...this.styles].map(([, stylesheet]) =>
|
|
1259
|
+
ProcessorHelper.processStyle(stylesheet, this.baseURI, this.options, this.cssVariables, this.batchRequest)
|
|
1260
|
+
));
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
async processPageResources() {
|
|
1264
|
+
const processAttributeArgs = [
|
|
1265
|
+
["link[href][rel*=\"icon\"]", "href", false, true],
|
|
1266
|
+
["object[type=\"image/svg+xml\"], object[type=\"image/svg-xml\"], object[data*=\".svg\"]", "data"],
|
|
1267
|
+
["img[src], input[src][type=image]", "src", true],
|
|
1268
|
+
["embed[src*=\".svg\"]", "src"],
|
|
1269
|
+
["video[poster]", "poster"],
|
|
1270
|
+
["*[background]", "background"],
|
|
1271
|
+
["image", "xlink:href"],
|
|
1272
|
+
["image", "href"]
|
|
1273
|
+
];
|
|
1274
|
+
if (this.options.blockImages) {
|
|
1275
|
+
this.doc.querySelectorAll("svg").forEach(element => element.remove());
|
|
1276
|
+
}
|
|
1277
|
+
let resourcePromises = processAttributeArgs.map(([selector, attributeName, processDuplicates, removeElementIfMissing]) =>
|
|
1278
|
+
ProcessorHelper.processAttribute(this.doc.querySelectorAll(selector), attributeName, this.baseURI, this.options, "image", this.cssVariables, this.styles, this.batchRequest, processDuplicates, removeElementIfMissing)
|
|
1279
|
+
);
|
|
1280
|
+
resourcePromises = resourcePromises.concat([
|
|
1281
|
+
ProcessorHelper.processXLinks(this.doc.querySelectorAll("use"), this.doc, this.baseURI, this.options, this.batchRequest),
|
|
1282
|
+
ProcessorHelper.processSrcset(this.doc.querySelectorAll("img[srcset], source[srcset]"), this.baseURI, this.options, this.batchRequest)
|
|
1283
|
+
]);
|
|
1284
|
+
resourcePromises.push(ProcessorHelper.processAttribute(this.doc.querySelectorAll("object[data*=\".pdf\"]"), "data", this.baseURI, this.options, null, this.cssVariables, this.styles, this.batchRequest));
|
|
1285
|
+
resourcePromises.push(ProcessorHelper.processAttribute(this.doc.querySelectorAll("embed[src*=\".pdf\"]"), "src", this.baseURI, this.options, null, this.cssVariables, this.styles, this.batchRequest));
|
|
1286
|
+
resourcePromises.push(ProcessorHelper.processAttribute(this.doc.querySelectorAll("audio[src], audio > source[src]"), "src", this.baseURI, this.options, "audio", this.cssVariables, this.styles, this.batchRequest));
|
|
1287
|
+
resourcePromises.push(ProcessorHelper.processAttribute(this.doc.querySelectorAll("video[src], video > source[src]"), "src", this.baseURI, this.options, "video", this.cssVariables, this.styles, this.batchRequest));
|
|
1288
|
+
await Promise.all(resourcePromises);
|
|
1289
|
+
if (this.options.saveFavicon) {
|
|
1290
|
+
ProcessorHelper.processShortcutIcons(this.doc);
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
async processScripts() {
|
|
1295
|
+
await Promise.all(Array.from(this.doc.querySelectorAll("script[src]")).map(async element => {
|
|
1296
|
+
let resourceURL;
|
|
1297
|
+
let scriptSrc;
|
|
1298
|
+
scriptSrc = element.getAttribute("src");
|
|
1299
|
+
if (this.options.saveOriginalURLs && !isDataURL(scriptSrc)) {
|
|
1300
|
+
element.setAttribute("data-sf-original-src", scriptSrc);
|
|
1301
|
+
}
|
|
1302
|
+
element.removeAttribute("integrity");
|
|
1303
|
+
if (!this.options.blockScripts) {
|
|
1304
|
+
element.textContent = "";
|
|
1305
|
+
try {
|
|
1306
|
+
resourceURL = util.resolveURL(scriptSrc, this.baseURI);
|
|
1307
|
+
} catch (error) {
|
|
1308
|
+
// ignored
|
|
1309
|
+
}
|
|
1310
|
+
if (testValidURL(resourceURL)) {
|
|
1311
|
+
element.removeAttribute("src");
|
|
1312
|
+
const content = await util.getContent(resourceURL, {
|
|
1313
|
+
asBinary: true,
|
|
1314
|
+
charset: this.charset != UTF8_CHARSET && this.charset,
|
|
1315
|
+
maxResourceSize: this.options.maxResourceSize,
|
|
1316
|
+
maxResourceSizeEnabled: this.options.maxResourceSizeEnabled,
|
|
1317
|
+
frameId: this.options.windowId,
|
|
1318
|
+
resourceReferrer: this.options.resourceReferrer,
|
|
1319
|
+
baseURI: this.options.baseURI,
|
|
1320
|
+
blockMixedContent: this.options.blockMixedContent,
|
|
1321
|
+
expectedType: "script",
|
|
1322
|
+
acceptHeaders: this.options.acceptHeaders,
|
|
1323
|
+
networkTimeout: this.options.networkTimeout
|
|
1324
|
+
});
|
|
1325
|
+
content.data = getUpdatedResourceContent(resourceURL, content, this.options);
|
|
1326
|
+
element.setAttribute("src", content.data);
|
|
1327
|
+
if (element.getAttribute("async") == "async" || element.getAttribute(util.ASYNC_SCRIPT_ATTRIBUTE_NAME) == "") {
|
|
1328
|
+
element.setAttribute("async", "");
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
} else {
|
|
1332
|
+
element.removeAttribute("src");
|
|
1333
|
+
}
|
|
1334
|
+
this.stats.add("processed", "scripts", 1);
|
|
1335
|
+
}));
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
removeAlternativeImages() {
|
|
1339
|
+
util.removeAlternativeImages(this.doc);
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
async removeAlternativeFonts() {
|
|
1343
|
+
await util.removeAlternativeFonts(this.doc, this.stylesheets, this.options.fontDeclarations, this.options.fontTests);
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
async processFrames() {
|
|
1347
|
+
if (this.options.frames) {
|
|
1348
|
+
const frameElements = Array.from(this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"));
|
|
1349
|
+
await Promise.all(frameElements.map(async frameElement => {
|
|
1350
|
+
const frameWindowId = frameElement.getAttribute(util.WIN_ID_ATTRIBUTE_NAME);
|
|
1351
|
+
if (frameWindowId) {
|
|
1352
|
+
const frameData = this.options.frames.find(frame => frame.windowId == frameWindowId);
|
|
1353
|
+
if (frameData) {
|
|
1354
|
+
this.options.frames = this.options.frames.filter(frame => frame.windowId != frameWindowId);
|
|
1355
|
+
if (frameData.runner && frameElement.getAttribute(util.HIDDEN_FRAME_ATTRIBUTE_NAME) != "") {
|
|
1356
|
+
this.stats.add("processed", "frames", 1);
|
|
1357
|
+
await frameData.runner.run();
|
|
1358
|
+
const pageData = await frameData.runner.getPageData();
|
|
1359
|
+
frameElement.removeAttribute(util.WIN_ID_ATTRIBUTE_NAME);
|
|
1360
|
+
let sandbox = "allow-popups allow-top-navigation allow-top-navigation-by-user-activation";
|
|
1361
|
+
if (pageData.content.match(NOSCRIPT_TAG_FOUND) || pageData.content.match(CANVAS_TAG_FOUND) || pageData.content.match(SCRIPT_TAG_FOUND)) {
|
|
1362
|
+
sandbox += " allow-scripts allow-same-origin";
|
|
1363
|
+
}
|
|
1364
|
+
frameElement.setAttribute("sandbox", sandbox);
|
|
1365
|
+
if (frameElement.tagName.toUpperCase() == "OBJECT") {
|
|
1366
|
+
frameElement.setAttribute("data", "data:text/html," + pageData.content);
|
|
1367
|
+
} else {
|
|
1368
|
+
if (frameElement.tagName.toUpperCase() == "FRAME") {
|
|
1369
|
+
frameElement.setAttribute("src", "data:text/html," + pageData.content.replace(/%/g, "%25").replace(/#/g, "%23"));
|
|
1370
|
+
} else {
|
|
1371
|
+
frameElement.setAttribute("srcdoc", pageData.content);
|
|
1372
|
+
frameElement.removeAttribute("src");
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
this.stats.addAll(pageData);
|
|
1376
|
+
} else {
|
|
1377
|
+
frameElement.removeAttribute(util.WIN_ID_ATTRIBUTE_NAME);
|
|
1378
|
+
this.stats.add("discarded", "frames", 1);
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
}));
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
replaceStylesheets() {
|
|
1387
|
+
this.doc.querySelectorAll("style").forEach(styleElement => {
|
|
1388
|
+
const stylesheetInfo = this.stylesheets.get(styleElement);
|
|
1389
|
+
if (stylesheetInfo) {
|
|
1390
|
+
this.stylesheets.delete(styleElement);
|
|
1391
|
+
styleElement.textContent = generateStylesheetContent(stylesheetInfo.stylesheet, this.options);
|
|
1392
|
+
if (stylesheetInfo.mediaText) {
|
|
1393
|
+
styleElement.media = stylesheetInfo.mediaText;
|
|
1394
|
+
}
|
|
1395
|
+
} else {
|
|
1396
|
+
styleElement.remove();
|
|
1397
|
+
}
|
|
1398
|
+
});
|
|
1399
|
+
this.doc.querySelectorAll("link[rel*=stylesheet]").forEach(linkElement => {
|
|
1400
|
+
const stylesheetInfo = this.stylesheets.get(linkElement);
|
|
1401
|
+
if (stylesheetInfo) {
|
|
1402
|
+
this.stylesheets.delete(linkElement);
|
|
1403
|
+
const styleElement = this.doc.createElement("style");
|
|
1404
|
+
if (stylesheetInfo.mediaText) {
|
|
1405
|
+
styleElement.media = stylesheetInfo.mediaText;
|
|
1406
|
+
}
|
|
1407
|
+
styleElement.textContent = generateStylesheetContent(stylesheetInfo.stylesheet, this.options);
|
|
1408
|
+
linkElement.parentElement.replaceChild(styleElement, linkElement);
|
|
1409
|
+
} else {
|
|
1410
|
+
linkElement.remove();
|
|
1411
|
+
}
|
|
1412
|
+
});
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
replaceStyleAttributes() {
|
|
1416
|
+
this.doc.querySelectorAll("[style]").forEach(element => {
|
|
1417
|
+
const declarationList = this.styles.get(element);
|
|
1418
|
+
if (declarationList) {
|
|
1419
|
+
this.styles.delete(element);
|
|
1420
|
+
element.setAttribute("style", generateStylesheetContent(declarationList, this.options));
|
|
1421
|
+
} else {
|
|
1422
|
+
element.setAttribute("style", "");
|
|
1423
|
+
}
|
|
1424
|
+
});
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
insertVariables() {
|
|
1428
|
+
if (this.cssVariables.size) {
|
|
1429
|
+
const styleElement = this.doc.createElement("style");
|
|
1430
|
+
const firstStyleElement = this.doc.head.querySelector("style");
|
|
1431
|
+
if (firstStyleElement) {
|
|
1432
|
+
this.doc.head.insertBefore(styleElement, firstStyleElement);
|
|
1433
|
+
} else {
|
|
1434
|
+
this.doc.head.appendChild(styleElement);
|
|
1435
|
+
}
|
|
1436
|
+
let stylesheetContent = "";
|
|
1437
|
+
this.cssVariables.forEach(({ content, url }, indexResource) => {
|
|
1438
|
+
this.cssVariables.delete(indexResource);
|
|
1439
|
+
if (stylesheetContent) {
|
|
1440
|
+
stylesheetContent += ";";
|
|
1441
|
+
}
|
|
1442
|
+
stylesheetContent += `${SINGLE_FILE_VARIABLE_NAME_PREFIX + indexResource}: `;
|
|
1443
|
+
if (this.options.saveOriginalURLs) {
|
|
1444
|
+
stylesheetContent += `/* original URL: ${url} */ `;
|
|
1445
|
+
}
|
|
1446
|
+
stylesheetContent += `url("${content}")`;
|
|
1447
|
+
});
|
|
1448
|
+
styleElement.textContent = ":root{" + stylesheetContent + "}";
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
compressHTML() {
|
|
1453
|
+
let size;
|
|
1454
|
+
if (this.options.displayStats) {
|
|
1455
|
+
size = util.getContentSize(this.doc.documentElement.outerHTML);
|
|
1456
|
+
}
|
|
1457
|
+
util.minifyHTML(this.doc, { PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME: util.PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME });
|
|
1458
|
+
if (this.options.displayStats) {
|
|
1459
|
+
this.stats.add("discarded", "HTML bytes", size - util.getContentSize(this.doc.documentElement.outerHTML));
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
cleanupPage() {
|
|
1464
|
+
this.doc.querySelectorAll("base").forEach(element => element.remove());
|
|
1465
|
+
const metaCharset = this.doc.head.querySelector("meta[charset]");
|
|
1466
|
+
if (metaCharset) {
|
|
1467
|
+
this.doc.head.insertBefore(metaCharset, this.doc.head.firstChild);
|
|
1468
|
+
if (this.doc.head.querySelectorAll("*").length == 1 && this.doc.body.childNodes.length == 0) {
|
|
1469
|
+
this.doc.head.querySelector("meta[charset]").remove();
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
resetZoomLevel() {
|
|
1475
|
+
const transform = this.doc.documentElement.style.getPropertyValue("-sf-transform");
|
|
1476
|
+
const transformPriority = this.doc.documentElement.style.getPropertyPriority("-sf-transform");
|
|
1477
|
+
const transformOrigin = this.doc.documentElement.style.getPropertyValue("-sf-transform-origin");
|
|
1478
|
+
const transformOriginPriority = this.doc.documentElement.style.getPropertyPriority("-sf-transform-origin");
|
|
1479
|
+
const minHeight = this.doc.documentElement.style.getPropertyValue("-sf-min-height");
|
|
1480
|
+
const minHeightPriority = this.doc.documentElement.style.getPropertyPriority("-sf-min-height");
|
|
1481
|
+
this.doc.documentElement.style.setProperty("transform", transform, transformPriority);
|
|
1482
|
+
this.doc.documentElement.style.setProperty("transform-origin", transformOrigin, transformOriginPriority);
|
|
1483
|
+
this.doc.documentElement.style.setProperty("min-height", minHeight, minHeightPriority);
|
|
1484
|
+
this.doc.documentElement.style.removeProperty("-sf-transform");
|
|
1485
|
+
this.doc.documentElement.style.removeProperty("-sf-transform-origin");
|
|
1486
|
+
this.doc.documentElement.style.removeProperty("-sf-min-height");
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
async insertMAFFMetaData() {
|
|
1490
|
+
const maffMetaData = await this.maffMetaDataPromise;
|
|
1491
|
+
if (maffMetaData && maffMetaData.content) {
|
|
1492
|
+
const NAMESPACE_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
|
1493
|
+
const maffDoc = util.parseXMLContent(maffMetaData.content);
|
|
1494
|
+
const originalURLElement = maffDoc.querySelector("RDF > Description > originalurl");
|
|
1495
|
+
const archiveTimeElement = maffDoc.querySelector("RDF > Description > archivetime");
|
|
1496
|
+
if (originalURLElement) {
|
|
1497
|
+
this.options.saveUrl = originalURLElement.getAttributeNS(NAMESPACE_RDF, "resource");
|
|
1498
|
+
}
|
|
1499
|
+
if (archiveTimeElement) {
|
|
1500
|
+
const value = archiveTimeElement.getAttributeNS(NAMESPACE_RDF, "resource");
|
|
1501
|
+
if (value) {
|
|
1502
|
+
const date = new Date(value);
|
|
1503
|
+
if (!isNaN(date.getTime())) {
|
|
1504
|
+
this.options.saveDate = new Date(value);
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
async setDocInfo() {
|
|
1512
|
+
const titleElement = this.doc.querySelector("title");
|
|
1513
|
+
const descriptionElement = this.doc.querySelector("meta[name=description]");
|
|
1514
|
+
const authorElement = this.doc.querySelector("meta[name=author]");
|
|
1515
|
+
const creatorElement = this.doc.querySelector("meta[name=creator]");
|
|
1516
|
+
const publisherElement = this.doc.querySelector("meta[name=publisher]");
|
|
1517
|
+
const headingElement = this.doc.querySelector("h1");
|
|
1518
|
+
this.options.title = titleElement ? titleElement.textContent.trim() : "";
|
|
1519
|
+
this.options.info = {
|
|
1520
|
+
description: descriptionElement && descriptionElement.content ? descriptionElement.content.trim() : "",
|
|
1521
|
+
lang: this.doc.documentElement.lang,
|
|
1522
|
+
author: authorElement && authorElement.content ? authorElement.content.trim() : "",
|
|
1523
|
+
creator: creatorElement && creatorElement.content ? creatorElement.content.trim() : "",
|
|
1524
|
+
publisher: publisherElement && publisherElement.content ? publisherElement.content.trim() : "",
|
|
1525
|
+
heading: headingElement && headingElement.textContent ? headingElement.textContent.trim() : ""
|
|
1526
|
+
};
|
|
1527
|
+
this.options.infobarContent = await util.evalTemplate(this.options.infobarTemplate, this.options, null, true);
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
// ---------------
|
|
1532
|
+
// ProcessorHelper
|
|
1533
|
+
// ---------------
|
|
1534
|
+
const DATA_URI_PREFIX = "data:";
|
|
1535
|
+
const ABOUT_BLANK_URI = "about:blank";
|
|
1536
|
+
const REGEXP_URL_HASH = /(#.+?)$/;
|
|
1537
|
+
const SINGLE_FILE_VARIABLE_NAME_PREFIX = "--sf-img-";
|
|
1538
|
+
const SINGLE_FILE_VARIABLE_MAX_SIZE = 512 * 1024;
|
|
1539
|
+
|
|
1540
|
+
class ProcessorHelper {
|
|
1541
|
+
static setBackgroundImage(element, url, style) {
|
|
1542
|
+
element.style.setProperty("background-blend-mode", "normal", "important");
|
|
1543
|
+
element.style.setProperty("background-clip", "content-box", "important");
|
|
1544
|
+
element.style.setProperty("background-position", style && style["background-position"] ? style["background-position"] : "center", "important");
|
|
1545
|
+
element.style.setProperty("background-color", style && style["background-color"] ? style["background-color"] : "transparent", "important");
|
|
1546
|
+
element.style.setProperty("background-image", url, "important");
|
|
1547
|
+
element.style.setProperty("background-size", style && style["background-size"] ? style["background-size"] : "100% 100%", "important");
|
|
1548
|
+
element.style.setProperty("background-origin", "content-box", "important");
|
|
1549
|
+
element.style.setProperty("background-repeat", "no-repeat", "important");
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
static processShortcutIcons(doc) {
|
|
1553
|
+
let shortcutIcon = findShortcutIcon(Array.from(doc.querySelectorAll("link[href][rel=\"shortcut icon\"]")));
|
|
1554
|
+
if (!shortcutIcon) {
|
|
1555
|
+
shortcutIcon = findShortcutIcon(Array.from(doc.querySelectorAll("link[href][rel=\"icon\"]")));
|
|
1556
|
+
}
|
|
1557
|
+
if (!shortcutIcon) {
|
|
1558
|
+
shortcutIcon = findShortcutIcon(Array.from(doc.querySelectorAll("link[href][rel*=\"icon\"]")));
|
|
1559
|
+
if (shortcutIcon) {
|
|
1560
|
+
shortcutIcon.rel = "shortcut icon";
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
if (shortcutIcon) {
|
|
1564
|
+
doc.querySelectorAll("link[href][rel*=\"icon\"]").forEach(linkElement => {
|
|
1565
|
+
if (linkElement != shortcutIcon) {
|
|
1566
|
+
linkElement.remove();
|
|
1567
|
+
}
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
static removeSingleLineCssComments(stylesheet) {
|
|
1573
|
+
if (stylesheet.children) {
|
|
1574
|
+
const removedRules = [];
|
|
1575
|
+
for (let cssRule = stylesheet.children.head; cssRule; cssRule = cssRule.next) {
|
|
1576
|
+
const ruleData = cssRule.data;
|
|
1577
|
+
if (ruleData.type == "Raw" && ruleData.value && ruleData.value.trim().startsWith("//")) {
|
|
1578
|
+
removedRules.push(cssRule);
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
removedRules.forEach(cssRule => stylesheet.children.remove(cssRule));
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
static replacePseudoClassDefined(stylesheet) {
|
|
1586
|
+
const removedSelectors = [];
|
|
1587
|
+
if (stylesheet.children) {
|
|
1588
|
+
for (let cssRule = stylesheet.children.head; cssRule; cssRule = cssRule.next) {
|
|
1589
|
+
const ruleData = cssRule.data;
|
|
1590
|
+
if (ruleData.type == "Rule" && ruleData.prelude && ruleData.prelude.children) {
|
|
1591
|
+
for (let selector = ruleData.prelude.children.head; selector; selector = selector.next) {
|
|
1592
|
+
replacePseudoDefinedSelector(selector, ruleData.prelude);
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
if (removedSelectors.length) {
|
|
1598
|
+
removedSelectors.forEach(({ parentSelector, selector }) => {
|
|
1599
|
+
if (parentSelector.data.children.size == 0 || !selector.prev || selector.prev.data.type == "Combinator" || selector.prev.data.type == "WhiteSpace") {
|
|
1600
|
+
parentSelector.data.children.replace(selector, cssTree.parse("*", { context: "selector" }).children.head);
|
|
1601
|
+
} else {
|
|
1602
|
+
parentSelector.data.children.remove(selector);
|
|
1603
|
+
}
|
|
1604
|
+
});
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
function replacePseudoDefinedSelector(selector, parentSelector) {
|
|
1608
|
+
if (selector.data.children) {
|
|
1609
|
+
for (let childSelector = selector.data.children.head; childSelector; childSelector = childSelector.next) {
|
|
1610
|
+
replacePseudoDefinedSelector(childSelector, selector);
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
if (selector.data.type == "PseudoClassSelector" && selector.data.name == "defined") {
|
|
1614
|
+
removedSelectors.push({ parentSelector, selector });
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
static async resolveImportURLs(stylesheet, baseURI, options, workStylesheet, importedStyleSheets = new Set()) {
|
|
1620
|
+
let importFound;
|
|
1621
|
+
ProcessorHelper.resolveStylesheetURLs(stylesheet, baseURI, workStylesheet);
|
|
1622
|
+
const imports = getImportFunctions(stylesheet);
|
|
1623
|
+
await Promise.all(imports.map(async node => {
|
|
1624
|
+
const urlNode = cssTree.find(node, node => node.type == "Url") || cssTree.find(node, node => node.type == "String");
|
|
1625
|
+
if (urlNode) {
|
|
1626
|
+
let resourceURL = normalizeURL(urlNode.value);
|
|
1627
|
+
if (!testIgnoredPath(resourceURL) && testValidPath(resourceURL)) {
|
|
1628
|
+
urlNode.value = util.EMPTY_RESOURCE;
|
|
1629
|
+
try {
|
|
1630
|
+
resourceURL = util.resolveURL(resourceURL, baseURI);
|
|
1631
|
+
} catch (error) {
|
|
1632
|
+
// ignored
|
|
1633
|
+
}
|
|
1634
|
+
if (testValidURL(resourceURL) && !importedStyleSheets.has(resourceURL)) {
|
|
1635
|
+
const content = await getStylesheetContent(resourceURL);
|
|
1636
|
+
resourceURL = content.resourceURL;
|
|
1637
|
+
content.data = getUpdatedResourceContent(resourceURL, content, options);
|
|
1638
|
+
if (content.data && content.data.match(/^<!doctype /i)) {
|
|
1639
|
+
content.data = "";
|
|
1640
|
+
}
|
|
1641
|
+
const mediaQueryListNode = cssTree.find(node, node => node.type == "MediaQueryList");
|
|
1642
|
+
if (mediaQueryListNode) {
|
|
1643
|
+
content.data = wrapMediaQuery(content.data, cssTree.generate(mediaQueryListNode));
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
content.data = content.data.replace(/:defined/gi, "*");
|
|
1647
|
+
|
|
1648
|
+
const importedStylesheet = cssTree.parse(content.data, { context: "stylesheet", parseCustomProperty: true });
|
|
1649
|
+
const ancestorStyleSheets = new Set(importedStyleSheets);
|
|
1650
|
+
ancestorStyleSheets.add(resourceURL);
|
|
1651
|
+
await ProcessorHelper.resolveImportURLs(importedStylesheet, resourceURL, options, workStylesheet, ancestorStyleSheets);
|
|
1652
|
+
for (let keyName of Object.keys(importedStylesheet)) {
|
|
1653
|
+
node[keyName] = importedStylesheet[keyName];
|
|
1654
|
+
}
|
|
1655
|
+
importFound = true;
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
}));
|
|
1660
|
+
return importFound;
|
|
1661
|
+
|
|
1662
|
+
async function getStylesheetContent(resourceURL) {
|
|
1663
|
+
const content = await util.getContent(resourceURL, {
|
|
1664
|
+
maxResourceSize: options.maxResourceSize,
|
|
1665
|
+
maxResourceSizeEnabled: options.maxResourceSizeEnabled,
|
|
1666
|
+
validateTextContentType: true,
|
|
1667
|
+
frameId: options.frameId,
|
|
1668
|
+
charset: options.charset,
|
|
1669
|
+
resourceReferrer: options.resourceReferrer,
|
|
1670
|
+
baseURI: options.baseURI,
|
|
1671
|
+
blockMixedContent: options.blockMixedContent,
|
|
1672
|
+
expectedType: "stylesheet",
|
|
1673
|
+
acceptHeaders: options.acceptHeaders,
|
|
1674
|
+
networkTimeout: options.networkTimeout
|
|
1675
|
+
});
|
|
1676
|
+
if (!(matchCharsetEquals(content.data, content.charset) || matchCharsetEquals(content.data, options.charset))) {
|
|
1677
|
+
options = Object.assign({}, options, { charset: getCharset(content.data) });
|
|
1678
|
+
return util.getContent(resourceURL, {
|
|
1679
|
+
maxResourceSize: options.maxResourceSize,
|
|
1680
|
+
maxResourceSizeEnabled: options.maxResourceSizeEnabled,
|
|
1681
|
+
validateTextContentType: true,
|
|
1682
|
+
frameId: options.frameId,
|
|
1683
|
+
charset: options.charset,
|
|
1684
|
+
resourceReferrer: options.resourceReferrer,
|
|
1685
|
+
baseURI: options.baseURI,
|
|
1686
|
+
blockMixedContent: options.blockMixedContent,
|
|
1687
|
+
expectedType: "stylesheet",
|
|
1688
|
+
acceptHeaders: options.acceptHeaders,
|
|
1689
|
+
networkTimeout: options.networkTimeout
|
|
1690
|
+
});
|
|
1691
|
+
} else {
|
|
1692
|
+
return content;
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
static resolveStylesheetURLs(stylesheet, baseURI, workStylesheet) {
|
|
1698
|
+
const urls = getUrlFunctions(stylesheet);
|
|
1699
|
+
urls.map(urlNode => {
|
|
1700
|
+
const originalResourceURL = urlNode.value;
|
|
1701
|
+
let resourceURL = normalizeURL(originalResourceURL);
|
|
1702
|
+
if (!testIgnoredPath(resourceURL)) {
|
|
1703
|
+
workStylesheet.textContent = "tmp { content:\"" + resourceURL + "\"}";
|
|
1704
|
+
if (workStylesheet.sheet && workStylesheet.sheet.cssRules) {
|
|
1705
|
+
resourceURL = util.removeQuotes(workStylesheet.sheet.cssRules[0].style.getPropertyValue("content"));
|
|
1706
|
+
}
|
|
1707
|
+
if (!testIgnoredPath(resourceURL)) {
|
|
1708
|
+
if (!resourceURL || testValidPath(resourceURL)) {
|
|
1709
|
+
let resolvedURL;
|
|
1710
|
+
if (!originalResourceURL.startsWith("#")) {
|
|
1711
|
+
try {
|
|
1712
|
+
resolvedURL = util.resolveURL(resourceURL, baseURI);
|
|
1713
|
+
} catch (error) {
|
|
1714
|
+
// ignored
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
if (testValidURL(resolvedURL)) {
|
|
1718
|
+
urlNode.value = resolvedURL;
|
|
1719
|
+
}
|
|
1720
|
+
} else {
|
|
1721
|
+
urlNode.value = util.EMPTY_RESOURCE;
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
static async resolveLinkStylesheetURLs(resourceURL, baseURI, options, workStylesheet) {
|
|
1729
|
+
resourceURL = normalizeURL(resourceURL);
|
|
1730
|
+
if (resourceURL && resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
|
|
1731
|
+
const content = await util.getContent(resourceURL, {
|
|
1732
|
+
maxResourceSize: options.maxResourceSize,
|
|
1733
|
+
maxResourceSizeEnabled: options.maxResourceSizeEnabled,
|
|
1734
|
+
charset: options.charset,
|
|
1735
|
+
frameId: options.frameId,
|
|
1736
|
+
resourceReferrer: options.resourceReferrer,
|
|
1737
|
+
validateTextContentType: true,
|
|
1738
|
+
baseURI: baseURI,
|
|
1739
|
+
blockMixedContent: options.blockMixedContent,
|
|
1740
|
+
expectedType: "stylesheet",
|
|
1741
|
+
acceptHeaders: options.acceptHeaders,
|
|
1742
|
+
networkTimeout: options.networkTimeout
|
|
1743
|
+
});
|
|
1744
|
+
if (!(matchCharsetEquals(content.data, content.charset) || matchCharsetEquals(content.data, options.charset))) {
|
|
1745
|
+
options = Object.assign({}, options, { charset: getCharset(content.data) });
|
|
1746
|
+
return ProcessorHelper.resolveLinkStylesheetURLs(resourceURL, baseURI, options, workStylesheet);
|
|
1747
|
+
}
|
|
1748
|
+
resourceURL = content.resourceURL;
|
|
1749
|
+
content.data = getUpdatedResourceContent(content.resourceURL, content, options);
|
|
1750
|
+
if (content.data && content.data.match(/^<!doctype /i)) {
|
|
1751
|
+
content.data = "";
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
content.data = content.data.replace(/:defined/gi, "*");
|
|
1755
|
+
|
|
1756
|
+
let stylesheet = cssTree.parse(content.data, { context: "stylesheet", parseCustomProperty: true });
|
|
1757
|
+
const importFound = await ProcessorHelper.resolveImportURLs(stylesheet, resourceURL, options, workStylesheet);
|
|
1758
|
+
if (importFound) {
|
|
1759
|
+
stylesheet = cssTree.parse(cssTree.generate(stylesheet), { context: "stylesheet", parseCustomProperty: true });
|
|
1760
|
+
}
|
|
1761
|
+
return stylesheet;
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
static async processStylesheet(cssRules, baseURI, options, cssVariables, batchRequest) {
|
|
1766
|
+
const promises = [];
|
|
1767
|
+
const removedRules = [];
|
|
1768
|
+
for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
|
|
1769
|
+
const ruleData = cssRule.data;
|
|
1770
|
+
if (ruleData.type == "Atrule" && ruleData.name == "charset") {
|
|
1771
|
+
removedRules.push(cssRule);
|
|
1772
|
+
} else if (ruleData.block && ruleData.block.children) {
|
|
1773
|
+
if (ruleData.type == "Rule") {
|
|
1774
|
+
promises.push(this.processStyle(ruleData, baseURI, options, cssVariables, batchRequest));
|
|
1775
|
+
} else if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports")) {
|
|
1776
|
+
promises.push(this.processStylesheet(ruleData.block.children, baseURI, options, cssVariables, batchRequest));
|
|
1777
|
+
} else if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
|
|
1778
|
+
promises.push(processFontFaceRule(ruleData));
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
removedRules.forEach(cssRule => cssRules.remove(cssRule));
|
|
1783
|
+
await Promise.all(promises);
|
|
1784
|
+
|
|
1785
|
+
async function processFontFaceRule(ruleData) {
|
|
1786
|
+
const urls = getUrlFunctions(ruleData);
|
|
1787
|
+
await Promise.all(urls.map(async urlNode => {
|
|
1788
|
+
const originalResourceURL = urlNode.value;
|
|
1789
|
+
if (!options.blockFonts) {
|
|
1790
|
+
const resourceURL = normalizeURL(originalResourceURL);
|
|
1791
|
+
if (!testIgnoredPath(resourceURL) && testValidURL(resourceURL)) {
|
|
1792
|
+
let { content } = await batchRequest.addURL(resourceURL, { asBinary: true, expectedType: "font", baseURI, blockMixedContent: options.blockMixedContent });
|
|
1793
|
+
let resourceURLs = options.fontDeclarations.get(urlNode);
|
|
1794
|
+
if (!resourceURLs) {
|
|
1795
|
+
resourceURLs = [];
|
|
1796
|
+
options.fontDeclarations.set(urlNode, resourceURLs);
|
|
1797
|
+
}
|
|
1798
|
+
resourceURLs.push(resourceURL);
|
|
1799
|
+
if (!isDataURL(resourceURL) && options.saveOriginalURLs) {
|
|
1800
|
+
urlNode.value = "-sf-url-original(" + JSON.stringify(originalResourceURL) + ") " + content;
|
|
1801
|
+
} else {
|
|
1802
|
+
urlNode.value = content;
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
} else {
|
|
1806
|
+
urlNode.value = util.EMPTY_RESOURCE;
|
|
1807
|
+
}
|
|
1808
|
+
}));
|
|
1809
|
+
}
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
static async processStyle(ruleData, baseURI, options, cssVariables, batchRequest) {
|
|
1813
|
+
const urls = getUrlFunctions(ruleData);
|
|
1814
|
+
await Promise.all(urls.map(async urlNode => {
|
|
1815
|
+
const originalResourceURL = urlNode.value;
|
|
1816
|
+
if (!options.blockImages) {
|
|
1817
|
+
const resourceURL = normalizeURL(originalResourceURL);
|
|
1818
|
+
if (!testIgnoredPath(resourceURL) && testValidURL(resourceURL)) {
|
|
1819
|
+
let { content, indexResource, duplicate } = await batchRequest.addURL(resourceURL, { asBinary: true, expectedType: "image", groupDuplicates: options.groupDuplicateImages });
|
|
1820
|
+
if (!originalResourceURL.startsWith("#")) {
|
|
1821
|
+
const maxSizeDuplicateImages = options.maxSizeDuplicateImages || SINGLE_FILE_VARIABLE_MAX_SIZE;
|
|
1822
|
+
if (duplicate && options.groupDuplicateImages && util.getContentSize(content) < maxSizeDuplicateImages) {
|
|
1823
|
+
const varNode = cssTree.parse("var(" + SINGLE_FILE_VARIABLE_NAME_PREFIX + indexResource + ")", { context: "value" });
|
|
1824
|
+
for (let keyName of Object.keys(varNode.children.head.data)) {
|
|
1825
|
+
urlNode[keyName] = varNode.children.head.data[keyName];
|
|
1826
|
+
}
|
|
1827
|
+
cssVariables.set(indexResource, { content, url: originalResourceURL });
|
|
1828
|
+
} else {
|
|
1829
|
+
if (!isDataURL(resourceURL) && options.saveOriginalURLs) {
|
|
1830
|
+
urlNode.value = "-sf-url-original(" + JSON.stringify(originalResourceURL) + ") " + content;
|
|
1831
|
+
} else {
|
|
1832
|
+
urlNode.value = content;
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
} else {
|
|
1838
|
+
urlNode.value = util.EMPTY_RESOURCE;
|
|
1839
|
+
}
|
|
1840
|
+
}));
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
static async processAttribute(resourceElements, attributeName, baseURI, options, expectedType, cssVariables, styles, batchRequest, processDuplicates, removeElementIfMissing) {
|
|
1844
|
+
await Promise.all(Array.from(resourceElements).map(async resourceElement => {
|
|
1845
|
+
let resourceURL = resourceElement.getAttribute(attributeName);
|
|
1846
|
+
if (resourceURL != null) {
|
|
1847
|
+
resourceURL = normalizeURL(resourceURL);
|
|
1848
|
+
let originURL = resourceElement.dataset.singleFileOriginURL;
|
|
1849
|
+
if (options.saveOriginalURLs && !isDataURL(resourceURL)) {
|
|
1850
|
+
resourceElement.setAttribute("data-sf-original-" + attributeName, resourceURL);
|
|
1851
|
+
}
|
|
1852
|
+
delete resourceElement.dataset.singleFileOriginURL;
|
|
1853
|
+
if (!options["block" + expectedType.charAt(0).toUpperCase() + expectedType.substring(1) + "s"]) {
|
|
1854
|
+
if (!testIgnoredPath(resourceURL)) {
|
|
1855
|
+
setAttributeEmpty(resourceElement, attributeName, expectedType);
|
|
1856
|
+
if (testValidPath(resourceURL)) {
|
|
1857
|
+
try {
|
|
1858
|
+
resourceURL = util.resolveURL(resourceURL, baseURI);
|
|
1859
|
+
} catch (error) {
|
|
1860
|
+
// ignored
|
|
1861
|
+
}
|
|
1862
|
+
if (testValidURL(resourceURL)) {
|
|
1863
|
+
let { content, indexResource, duplicate } = await batchRequest.addURL(
|
|
1864
|
+
resourceURL,
|
|
1865
|
+
{ asBinary: true, expectedType, groupDuplicates: options.groupDuplicateImages && resourceElement.tagName.toUpperCase() == "IMG" && attributeName == "src" });
|
|
1866
|
+
if (originURL) {
|
|
1867
|
+
if (content == util.EMPTY_RESOURCE) {
|
|
1868
|
+
try {
|
|
1869
|
+
originURL = util.resolveURL(originURL, baseURI);
|
|
1870
|
+
} catch (error) {
|
|
1871
|
+
// ignored
|
|
1872
|
+
}
|
|
1873
|
+
try {
|
|
1874
|
+
resourceURL = originURL;
|
|
1875
|
+
content = (
|
|
1876
|
+
await util.getContent(resourceURL, {
|
|
1877
|
+
asBinary: true,
|
|
1878
|
+
expectedType,
|
|
1879
|
+
maxResourceSize: options.maxResourceSize,
|
|
1880
|
+
maxResourceSizeEnabled: options.maxResourceSizeEnabled,
|
|
1881
|
+
frameId: options.windowId,
|
|
1882
|
+
resourceReferrer: options.resourceReferrer,
|
|
1883
|
+
acceptHeaders: options.acceptHeaders,
|
|
1884
|
+
networkTimeout: options.networkTimeout
|
|
1885
|
+
})
|
|
1886
|
+
).data;
|
|
1887
|
+
} catch (error) {
|
|
1888
|
+
// ignored
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
if (removeElementIfMissing && content == util.EMPTY_RESOURCE) {
|
|
1893
|
+
resourceElement.remove();
|
|
1894
|
+
} else if (content !== util.EMPTY_RESOURCE) {
|
|
1895
|
+
let forbiddenPrefixFound = PREFIXES_FORBIDDEN_DATA_URI.filter(prefixDataURI => content.startsWith(prefixDataURI)).length;
|
|
1896
|
+
if (expectedType == "image") {
|
|
1897
|
+
if (forbiddenPrefixFound && Image) {
|
|
1898
|
+
forbiddenPrefixFound = await new Promise((resolve) => {
|
|
1899
|
+
const image = new Image();
|
|
1900
|
+
const timeoutId = setTimeout(() => resolve(true), 100);
|
|
1901
|
+
image.src = content;
|
|
1902
|
+
image.onload = () => cleanupAndResolve();
|
|
1903
|
+
image.onerror = () => cleanupAndResolve(true);
|
|
1904
|
+
|
|
1905
|
+
function cleanupAndResolve(value) {
|
|
1906
|
+
clearTimeout(timeoutId);
|
|
1907
|
+
resolve(value);
|
|
1908
|
+
}
|
|
1909
|
+
});
|
|
1910
|
+
}
|
|
1911
|
+
if (!forbiddenPrefixFound) {
|
|
1912
|
+
const isSVG = content.startsWith(PREFIX_DATA_URI_IMAGE_SVG);
|
|
1913
|
+
const maxSizeDuplicateImages = options.maxSizeDuplicateImages || SINGLE_FILE_VARIABLE_MAX_SIZE;
|
|
1914
|
+
if (processDuplicates && duplicate && !isSVG && util.getContentSize(content) < maxSizeDuplicateImages) {
|
|
1915
|
+
if (ProcessorHelper.replaceImageSource(resourceElement, SINGLE_FILE_VARIABLE_NAME_PREFIX + indexResource, options)) {
|
|
1916
|
+
cssVariables.set(indexResource, { content, url: originURL });
|
|
1917
|
+
const declarationList = cssTree.parse(resourceElement.getAttribute("style"), { context: "declarationList", parseCustomProperty: true });
|
|
1918
|
+
styles.set(resourceElement, declarationList);
|
|
1919
|
+
} else {
|
|
1920
|
+
resourceElement.setAttribute(attributeName, content);
|
|
1921
|
+
}
|
|
1922
|
+
} else {
|
|
1923
|
+
resourceElement.setAttribute(attributeName, content);
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
} else {
|
|
1927
|
+
resourceElement.setAttribute(attributeName, content);
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
} else {
|
|
1934
|
+
setAttributeEmpty(resourceElement, attributeName, expectedType);
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
}));
|
|
1938
|
+
|
|
1939
|
+
function setAttributeEmpty(resourceElement, attributeName, expectedType) {
|
|
1940
|
+
if (expectedType == "video" || expectedType == "audio") {
|
|
1941
|
+
resourceElement.removeAttribute(attributeName);
|
|
1942
|
+
} else {
|
|
1943
|
+
resourceElement.setAttribute(attributeName, util.EMPTY_RESOURCE);
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
static async processXLinks(resourceElements, doc, baseURI, options, batchRequest) {
|
|
1949
|
+
let attributeName = "xlink:href";
|
|
1950
|
+
await Promise.all(Array.from(resourceElements).map(async resourceElement => {
|
|
1951
|
+
let originalResourceURL = resourceElement.getAttribute(attributeName);
|
|
1952
|
+
if (originalResourceURL == null) {
|
|
1953
|
+
attributeName = "href";
|
|
1954
|
+
originalResourceURL = resourceElement.getAttribute(attributeName);
|
|
1955
|
+
}
|
|
1956
|
+
if (options.saveOriginalURLs && !isDataURL(originalResourceURL)) {
|
|
1957
|
+
resourceElement.setAttribute("data-sf-original-href", originalResourceURL);
|
|
1958
|
+
}
|
|
1959
|
+
let resourceURL = normalizeURL(originalResourceURL);
|
|
1960
|
+
if (!options.blockImages) {
|
|
1961
|
+
if (testValidPath(resourceURL) && !testIgnoredPath(resourceURL)) {
|
|
1962
|
+
resourceElement.setAttribute(attributeName, util.EMPTY_RESOURCE);
|
|
1963
|
+
try {
|
|
1964
|
+
resourceURL = util.resolveURL(resourceURL, baseURI);
|
|
1965
|
+
} catch (error) {
|
|
1966
|
+
// ignored
|
|
1967
|
+
}
|
|
1968
|
+
if (testValidURL(resourceURL)) {
|
|
1969
|
+
const hashMatch = originalResourceURL.match(REGEXP_URL_HASH);
|
|
1970
|
+
if (originalResourceURL.startsWith(baseURI + "#")) {
|
|
1971
|
+
resourceElement.setAttribute(attributeName, hashMatch[0]);
|
|
1972
|
+
} else {
|
|
1973
|
+
const response = await batchRequest.addURL(resourceURL, { expectedType: "image" });
|
|
1974
|
+
const svgDoc = util.parseSVGContent(response.content);
|
|
1975
|
+
if (hashMatch && hashMatch[0]) {
|
|
1976
|
+
let symbolElement;
|
|
1977
|
+
try {
|
|
1978
|
+
symbolElement = svgDoc.querySelector(hashMatch[0]);
|
|
1979
|
+
} catch (error) {
|
|
1980
|
+
// ignored
|
|
1981
|
+
}
|
|
1982
|
+
if (symbolElement) {
|
|
1983
|
+
resourceElement.setAttribute(attributeName, hashMatch[0]);
|
|
1984
|
+
resourceElement.parentElement.insertBefore(symbolElement, resourceElement.parentElement.firstChild);
|
|
1985
|
+
}
|
|
1986
|
+
} else {
|
|
1987
|
+
const content = await batchRequest.addURL(resourceURL, { expectedType: "image" });
|
|
1988
|
+
resourceElement.setAttribute(attributeName, PREFIX_DATA_URI_IMAGE_SVG + "," + content);
|
|
1989
|
+
}
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
} else if (resourceURL == options.url) {
|
|
1993
|
+
resourceElement.setAttribute(attributeName, originalResourceURL.substring(resourceURL.length));
|
|
1994
|
+
}
|
|
1995
|
+
} else {
|
|
1996
|
+
resourceElement.setAttribute(attributeName, util.EMPTY_RESOURCE);
|
|
1997
|
+
}
|
|
1998
|
+
}));
|
|
1999
|
+
}
|
|
2000
|
+
|
|
2001
|
+
static async processSrcset(resourceElements, baseURI, options, batchRequest) {
|
|
2002
|
+
await Promise.all(Array.from(resourceElements).map(async resourceElement => {
|
|
2003
|
+
const originSrcset = resourceElement.getAttribute("srcset");
|
|
2004
|
+
const srcset = util.parseSrcset(originSrcset);
|
|
2005
|
+
if (options.saveOriginalURLs && !isDataURL(originSrcset)) {
|
|
2006
|
+
resourceElement.setAttribute("data-sf-original-srcset", originSrcset);
|
|
2007
|
+
}
|
|
2008
|
+
if (!options.blockImages) {
|
|
2009
|
+
const srcsetValues = await Promise.all(srcset.map(async srcsetValue => {
|
|
2010
|
+
let resourceURL = normalizeURL(srcsetValue.url);
|
|
2011
|
+
if (!testIgnoredPath(resourceURL)) {
|
|
2012
|
+
if (testValidPath(resourceURL)) {
|
|
2013
|
+
try {
|
|
2014
|
+
resourceURL = util.resolveURL(resourceURL, baseURI);
|
|
2015
|
+
} catch (error) {
|
|
2016
|
+
// ignored
|
|
2017
|
+
}
|
|
2018
|
+
if (testValidURL(resourceURL)) {
|
|
2019
|
+
const { content } = await batchRequest.addURL(resourceURL, { asBinary: true, expectedType: "image" });
|
|
2020
|
+
const forbiddenPrefixFound = PREFIXES_FORBIDDEN_DATA_URI.filter(prefixDataURI => content.startsWith(prefixDataURI)).length;
|
|
2021
|
+
if (forbiddenPrefixFound) {
|
|
2022
|
+
return "";
|
|
2023
|
+
}
|
|
2024
|
+
return content + (srcsetValue.w ? " " + srcsetValue.w + "w" : srcsetValue.d ? " " + srcsetValue.d + "x" : "");
|
|
2025
|
+
} else {
|
|
2026
|
+
return "";
|
|
2027
|
+
}
|
|
2028
|
+
} else {
|
|
2029
|
+
return "";
|
|
2030
|
+
}
|
|
2031
|
+
} else {
|
|
2032
|
+
return resourceURL + (srcsetValue.w ? " " + srcsetValue.w + "w" : srcsetValue.d ? " " + srcsetValue.d + "x" : "");
|
|
2033
|
+
}
|
|
2034
|
+
}));
|
|
2035
|
+
resourceElement.setAttribute("srcset", srcsetValues.join(", "));
|
|
2036
|
+
} else {
|
|
2037
|
+
resourceElement.setAttribute("srcset", "");
|
|
2038
|
+
}
|
|
2039
|
+
}));
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
static replaceImageSource(imgElement, variableName, options) {
|
|
2043
|
+
const attributeValue = imgElement.getAttribute(util.IMAGE_ATTRIBUTE_NAME);
|
|
2044
|
+
if (attributeValue) {
|
|
2045
|
+
const imageData = options.images[Number(imgElement.getAttribute(util.IMAGE_ATTRIBUTE_NAME))];
|
|
2046
|
+
if (imageData && imageData.replaceable) {
|
|
2047
|
+
imgElement.setAttribute("src", `${PREFIX_DATA_URI_IMAGE_SVG},<svg xmlns="http://www.w3.org/2000/svg" width="${imageData.size.pxWidth}" height="${imageData.size.pxHeight}"><rect fill-opacity="0"/></svg>`);
|
|
2048
|
+
const backgroundStyle = {};
|
|
2049
|
+
const backgroundSize = (imageData.objectFit == "content" || imageData.objectFit == "cover") && imageData.objectFit;
|
|
2050
|
+
if (backgroundSize) {
|
|
2051
|
+
backgroundStyle["background-size"] = imageData.objectFit;
|
|
2052
|
+
}
|
|
2053
|
+
if (imageData.objectPosition) {
|
|
2054
|
+
backgroundStyle["background-position"] = imageData.objectPosition;
|
|
2055
|
+
}
|
|
2056
|
+
if (imageData.backgroundColor) {
|
|
2057
|
+
backgroundStyle["background-color"] = imageData.backgroundColor;
|
|
2058
|
+
}
|
|
2059
|
+
ProcessorHelper.setBackgroundImage(imgElement, "var(" + variableName + ")", backgroundStyle);
|
|
2060
|
+
imgElement.removeAttribute(util.IMAGE_ATTRIBUTE_NAME);
|
|
2061
|
+
return true;
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
|
|
2067
|
+
// ----
|
|
2068
|
+
// Util
|
|
2069
|
+
// ----
|
|
2070
|
+
const BLOB_URI_PREFIX = "blob:";
|
|
2071
|
+
const HTTP_URI_PREFIX = /^https?:\/\//;
|
|
2072
|
+
const FILE_URI_PREFIX = /^file:\/\//;
|
|
2073
|
+
const EMPTY_URL = /^https?:\/\/+\s*$/;
|
|
2074
|
+
const NOT_EMPTY_URL = /^(https?:\/\/|file:\/\/|blob:).+/;
|
|
2075
|
+
|
|
2076
|
+
function getUpdatedResourceContent(resourceURL, content, options) {
|
|
2077
|
+
if (options.rootDocument && options.updatedResources[resourceURL]) {
|
|
2078
|
+
options.updatedResources[resourceURL].retrieved = true;
|
|
2079
|
+
return options.updatedResources[resourceURL].content;
|
|
2080
|
+
} else {
|
|
2081
|
+
return content.data || "";
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
|
|
2085
|
+
function normalizeURL(url) {
|
|
2086
|
+
if (!url || url.startsWith(DATA_URI_PREFIX)) {
|
|
2087
|
+
return url;
|
|
2088
|
+
} else {
|
|
2089
|
+
return url.split("#")[0];
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
function matchCharsetEquals(stylesheetContent = "", charset = UTF8_CHARSET) {
|
|
2094
|
+
const stylesheetCharset = getCharset(stylesheetContent);
|
|
2095
|
+
if (stylesheetCharset) {
|
|
2096
|
+
return stylesheetCharset == charset.toLowerCase();
|
|
2097
|
+
} else {
|
|
2098
|
+
return true;
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
|
|
2102
|
+
function getCharset(stylesheetContent = "") {
|
|
2103
|
+
const match = stylesheetContent.match(/^@charset\s+"([^"]*)";/i);
|
|
2104
|
+
if (match && match[1]) {
|
|
2105
|
+
return match[1].toLowerCase().trim();
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
|
|
2109
|
+
function getOnEventAttributeNames(doc) {
|
|
2110
|
+
const element = doc.createElement("div");
|
|
2111
|
+
const attributeNames = [];
|
|
2112
|
+
for (const propertyName in element) {
|
|
2113
|
+
if (propertyName.startsWith("on")) {
|
|
2114
|
+
attributeNames.push(propertyName);
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
attributeNames.push("onunload");
|
|
2118
|
+
return attributeNames;
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
function getUrlFunctions(declarationList) {
|
|
2122
|
+
return cssTree.findAll(declarationList, node => node.type == "Url");
|
|
2123
|
+
}
|
|
2124
|
+
|
|
2125
|
+
function getImportFunctions(declarationList) {
|
|
2126
|
+
return cssTree.findAll(declarationList, node => node.type == "Atrule" && node.name == "import");
|
|
2127
|
+
}
|
|
2128
|
+
|
|
2129
|
+
function generateStylesheetContent(stylesheet, options) {
|
|
2130
|
+
let stylesheetContent = cssTree.generate(stylesheet);
|
|
2131
|
+
if (options.compressCSS) {
|
|
2132
|
+
stylesheetContent = util.compressCSS(stylesheetContent);
|
|
2133
|
+
}
|
|
2134
|
+
if (options.saveOriginalURLs) {
|
|
2135
|
+
stylesheetContent = replaceOriginalURLs(stylesheetContent);
|
|
2136
|
+
}
|
|
2137
|
+
return stylesheetContent;
|
|
2138
|
+
}
|
|
2139
|
+
|
|
2140
|
+
function findShortcutIcon(shortcutIcons) {
|
|
2141
|
+
shortcutIcons = shortcutIcons.filter(linkElement => linkElement.href != util.EMPTY_RESOURCE);
|
|
2142
|
+
shortcutIcons.sort((linkElement1, linkElement2) => (parseInt(linkElement2.sizes, 10) || 16) - (parseInt(linkElement1.sizes, 10) || 16));
|
|
2143
|
+
return shortcutIcons[0];
|
|
2144
|
+
}
|
|
2145
|
+
|
|
2146
|
+
function isDataURL(url) {
|
|
2147
|
+
return url && (url.startsWith(DATA_URI_PREFIX) || url.startsWith(BLOB_URI_PREFIX));
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
function replaceOriginalURLs(stylesheetContent) {
|
|
2151
|
+
return stylesheetContent.replace(/url\(-sf-url-original\\\(\\"(.*?)\\"\\\)\\ /g, "/* original URL: $1 */url(");
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
function testIgnoredPath(resourceURL) {
|
|
2155
|
+
return resourceURL && (resourceURL.startsWith(DATA_URI_PREFIX) || resourceURL == ABOUT_BLANK_URI);
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
function testValidPath(resourceURL) {
|
|
2159
|
+
return resourceURL && !resourceURL.match(EMPTY_URL);
|
|
2160
|
+
}
|
|
2161
|
+
|
|
2162
|
+
function testValidURL(resourceURL) {
|
|
2163
|
+
return testValidPath(resourceURL) && (resourceURL.match(HTTP_URI_PREFIX) || resourceURL.match(FILE_URI_PREFIX) || resourceURL.startsWith(BLOB_URI_PREFIX)) && resourceURL.match(NOT_EMPTY_URL);
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2166
|
+
function wrapMediaQuery(stylesheetContent, mediaQuery) {
|
|
2167
|
+
if (mediaQuery) {
|
|
2168
|
+
return "@media " + mediaQuery + "{ " + stylesheetContent + " }";
|
|
2169
|
+
} else {
|
|
2170
|
+
return stylesheetContent;
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
function log(...args) {
|
|
2175
|
+
console.log("S-File <core> ", ...args); // eslint-disable-line no-console
|
|
2176
|
+
}
|
|
2177
|
+
|
|
2178
|
+
// -----
|
|
2179
|
+
// Stats
|
|
2180
|
+
// -----
|
|
2181
|
+
const STATS_DEFAULT_VALUES = {
|
|
2182
|
+
discarded: {
|
|
2183
|
+
"HTML bytes": 0,
|
|
2184
|
+
"hidden elements": 0,
|
|
2185
|
+
scripts: 0,
|
|
2186
|
+
objects: 0,
|
|
2187
|
+
"audio sources": 0,
|
|
2188
|
+
"video sources": 0,
|
|
2189
|
+
frames: 0,
|
|
2190
|
+
"CSS rules": 0,
|
|
2191
|
+
canvas: 0,
|
|
2192
|
+
stylesheets: 0,
|
|
2193
|
+
resources: 0,
|
|
2194
|
+
medias: 0
|
|
2195
|
+
},
|
|
2196
|
+
processed: {
|
|
2197
|
+
"HTML bytes": 0,
|
|
2198
|
+
"hidden elements": 0,
|
|
2199
|
+
scripts: 0,
|
|
2200
|
+
objects: 0,
|
|
2201
|
+
"audio sources": 0,
|
|
2202
|
+
"video sources": 0,
|
|
2203
|
+
frames: 0,
|
|
2204
|
+
"CSS rules": 0,
|
|
2205
|
+
canvas: 0,
|
|
2206
|
+
stylesheets: 0,
|
|
2207
|
+
resources: 0,
|
|
2208
|
+
medias: 0
|
|
2209
|
+
}
|
|
2210
|
+
};
|
|
2211
|
+
|
|
2212
|
+
class Stats {
|
|
2213
|
+
constructor(options) {
|
|
2214
|
+
this.options = options;
|
|
2215
|
+
if (options.displayStats) {
|
|
2216
|
+
this.data = JSON.parse(JSON.stringify(STATS_DEFAULT_VALUES));
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
set(type, subType, value) {
|
|
2220
|
+
if (this.options.displayStats) {
|
|
2221
|
+
this.data[type][subType] = value;
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
add(type, subType, value) {
|
|
2225
|
+
if (this.options.displayStats) {
|
|
2226
|
+
this.data[type][subType] += value;
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
addAll(pageData) {
|
|
2230
|
+
if (this.options.displayStats) {
|
|
2231
|
+
Object.keys(this.data.discarded).forEach(key => this.add("discarded", key, pageData.stats.discarded[key] || 0));
|
|
2232
|
+
Object.keys(this.data.processed).forEach(key => this.add("processed", key, pageData.stats.processed[key] || 0));
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2232
2235
|
}
|