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.
Files changed (37) hide show
  1. package/.eslintrc.js +0 -0
  2. package/LICENSE +0 -0
  3. package/core/constants.js +13 -0
  4. package/core/helper.js +604 -596
  5. package/core/index.js +2234 -2231
  6. package/core/infobar.js +276 -272
  7. package/core/util.js +408 -407
  8. package/modules/css-fonts-alt-minifier.js +342 -342
  9. package/modules/css-fonts-minifier.js +376 -376
  10. package/modules/css-matched-rules.js +0 -0
  11. package/modules/css-medias-alt-minifier.js +90 -90
  12. package/modules/css-rules-minifier.js +0 -0
  13. package/modules/html-images-alt-minifier.js +0 -0
  14. package/modules/html-minifier.js +0 -0
  15. package/modules/html-serializer.js +0 -0
  16. package/modules/index.js +0 -0
  17. package/modules/template-formatter.js +0 -0
  18. package/package.json +18 -18
  19. package/processors/frame-tree/content/content-frame-tree.js +426 -424
  20. package/processors/hooks/content/content-hooks-frames-web.js +0 -0
  21. package/processors/hooks/content/content-hooks-frames.js +0 -0
  22. package/processors/index.js +0 -0
  23. package/processors/lazy/content/content-lazy-loader.js +233 -233
  24. package/single-file-bootstrap.js +55 -55
  25. package/single-file-editor-helper.js +49 -49
  26. package/single-file-frames.js +0 -0
  27. package/single-file-hooks-frames.js +0 -0
  28. package/single-file-infobar.js +62 -62
  29. package/single-file.js +101 -101
  30. package/vendor/css-font-property-parser.js +0 -0
  31. package/vendor/css-media-query-parser.js +0 -0
  32. package/vendor/css-minifier.js +0 -0
  33. package/vendor/css-tree.js +0 -0
  34. package/vendor/css-unescape.js +0 -0
  35. package/vendor/html-srcset-parser.js +0 -0
  36. package/vendor/index.js +0 -0
  37. package/vendor/mime-type-parser.js +0 -0
@@ -1,425 +1,427 @@
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
- import * as lazy from "./../../lazy/content/content-lazy-loader.js";
27
- import {
28
- ON_BEFORE_CAPTURE_EVENT_NAME,
29
- ON_AFTER_CAPTURE_EVENT_NAME,
30
- WIN_ID_ATTRIBUTE_NAME,
31
- preProcessDoc,
32
- serialize,
33
- postProcessDoc,
34
- getShadowRoot
35
- } from "./../../../core/helper.js";
36
-
37
- const helper = {
38
- ON_BEFORE_CAPTURE_EVENT_NAME,
39
- ON_AFTER_CAPTURE_EVENT_NAME,
40
- WIN_ID_ATTRIBUTE_NAME,
41
- preProcessDoc,
42
- serialize,
43
- postProcessDoc,
44
- getShadowRoot
45
- };
46
-
47
- const MESSAGE_PREFIX = "__frameTree__::";
48
- const FRAMES_CSS_SELECTOR = "iframe, frame, object[type=\"text/html\"][data]";
49
- const ALL_ELEMENTS_CSS_SELECTOR = "*";
50
- const INIT_REQUEST_MESSAGE = "singlefile.frameTree.initRequest";
51
- const ACK_INIT_REQUEST_MESSAGE = "singlefile.frameTree.ackInitRequest";
52
- const CLEANUP_REQUEST_MESSAGE = "singlefile.frameTree.cleanupRequest";
53
- const INIT_RESPONSE_MESSAGE = "singlefile.frameTree.initResponse";
54
- const TARGET_ORIGIN = "*";
55
- const TIMEOUT_INIT_REQUEST_MESSAGE = 5000;
56
- const TIMEOUT_INIT_RESPONSE_MESSAGE = 10000;
57
- const TOP_WINDOW_ID = "0";
58
- const WINDOW_ID_SEPARATOR = ".";
59
- const TOP_WINDOW = globalThis.window == globalThis.top;
60
-
61
- const browser = globalThis.browser;
62
- const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
63
- const top = globalThis.top;
64
- const MessageChannel = globalThis.MessageChannel;
65
- const document = globalThis.document;
66
- const JSON = globalThis.JSON;
67
-
68
- let sessions = globalThis.sessions;
69
- if (!sessions) {
70
- sessions = globalThis.sessions = new Map();
71
- }
72
- let windowId;
73
- if (TOP_WINDOW) {
74
- windowId = TOP_WINDOW_ID;
75
- if (browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
76
- browser.runtime.onMessage.addListener(message => {
77
- if (message.method == INIT_RESPONSE_MESSAGE) {
78
- initResponse(message);
79
- return Promise.resolve({});
80
- } else if (message.method == ACK_INIT_REQUEST_MESSAGE) {
81
- clearFrameTimeout("requestTimeouts", message.sessionId, message.windowId);
82
- createFrameResponseTimeout(message.sessionId, message.windowId);
83
- return Promise.resolve({});
84
- }
85
- });
86
- }
87
- }
88
- addEventListener("message", async event => {
89
- if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX)) {
90
- event.preventDefault();
91
- event.stopPropagation();
92
- const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length));
93
- if (message.method == INIT_REQUEST_MESSAGE) {
94
- if (event.source) {
95
- sendMessage(event.source, { method: ACK_INIT_REQUEST_MESSAGE, windowId: message.windowId, sessionId: message.sessionId });
96
- }
97
- if (!TOP_WINDOW) {
98
- globalThis.stop();
99
- if (message.options.loadDeferredImages) {
100
- lazy.process(message.options);
101
- }
102
- await initRequestAsync(message);
103
- }
104
- } else if (message.method == ACK_INIT_REQUEST_MESSAGE) {
105
- clearFrameTimeout("requestTimeouts", message.sessionId, message.windowId);
106
- createFrameResponseTimeout(message.sessionId, message.windowId);
107
- } else if (message.method == CLEANUP_REQUEST_MESSAGE) {
108
- cleanupRequest(message);
109
- } else if (message.method == INIT_RESPONSE_MESSAGE && sessions.get(message.sessionId)) {
110
- const port = event.ports[0];
111
- port.onmessage = event => initResponse(event.data);
112
- }
113
- }
114
- }, true);
115
-
116
- export {
117
- getAsync,
118
- getSync,
119
- cleanup,
120
- initResponse,
121
- TIMEOUT_INIT_REQUEST_MESSAGE
122
- };
123
-
124
- function getAsync(options) {
125
- const sessionId = getNewSessionId();
126
- options = JSON.parse(JSON.stringify(options));
127
- return new Promise(resolve => {
128
- sessions.set(sessionId, {
129
- frames: [],
130
- requestTimeouts: {},
131
- responseTimeouts: {},
132
- resolve: frames => {
133
- frames.sessionId = sessionId;
134
- resolve(frames);
135
- }
136
- });
137
- initRequestAsync({ windowId, sessionId, options });
138
- });
139
- }
140
-
141
- function getSync(options) {
142
- const sessionId = getNewSessionId();
143
- options = JSON.parse(JSON.stringify(options));
144
- sessions.set(sessionId, {
145
- frames: [],
146
- requestTimeouts: {},
147
- responseTimeouts: {}
148
- });
149
- initRequestSync({ windowId, sessionId, options });
150
- const frames = sessions.get(sessionId).frames;
151
- frames.sessionId = sessionId;
152
- return frames;
153
- }
154
-
155
- function cleanup(sessionId) {
156
- sessions.delete(sessionId);
157
- cleanupRequest({ windowId, sessionId, options: { sessionId } });
158
- }
159
-
160
- function getNewSessionId() {
161
- return globalThis.crypto.getRandomValues(new Uint32Array(32)).join("");
162
- }
163
-
164
- function initRequestSync(message) {
165
- const sessionId = message.sessionId;
166
- const waitForUserScript = globalThis._singleFile_waitForUserScript;
167
- delete globalThis._singleFile_cleaningUp;
168
- if (!TOP_WINDOW) {
169
- windowId = globalThis.frameId = message.windowId;
170
- }
171
- processFrames(document, message.options, windowId, sessionId);
172
- if (!TOP_WINDOW) {
173
- if (message.options.userScriptEnabled && waitForUserScript) {
174
- waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
175
- }
176
- sendInitResponse({ frames: [getFrameData(document, globalThis, windowId, message.options, message.scrolling)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
177
- if (message.options.userScriptEnabled && waitForUserScript) {
178
- waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
179
- }
180
- delete document.documentElement.dataset.requestedFrameId;
181
- }
182
- }
183
-
184
- async function initRequestAsync(message) {
185
- const sessionId = message.sessionId;
186
- const waitForUserScript = globalThis._singleFile_waitForUserScript;
187
- delete globalThis._singleFile_cleaningUp;
188
- if (!TOP_WINDOW) {
189
- windowId = globalThis.frameId = message.windowId;
190
- }
191
- processFrames(document, message.options, windowId, sessionId);
192
- if (!TOP_WINDOW) {
193
- if (message.options.userScriptEnabled && waitForUserScript) {
194
- await waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
195
- }
196
- sendInitResponse({ frames: [getFrameData(document, globalThis, windowId, message.options, message.scrolling)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
197
- if (message.options.userScriptEnabled && waitForUserScript) {
198
- await waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
199
- }
200
- delete document.documentElement.dataset.requestedFrameId;
201
- }
202
- }
203
-
204
- function cleanupRequest(message) {
205
- if (!globalThis._singleFile_cleaningUp) {
206
- globalThis._singleFile_cleaningUp = true;
207
- const sessionId = message.sessionId;
208
- cleanupFrames(getFrames(document), message.windowId, sessionId);
209
- }
210
- }
211
-
212
- function initResponse(message) {
213
- message.frames.forEach(frameData => clearFrameTimeout("responseTimeouts", message.sessionId, frameData.windowId));
214
- const windowData = sessions.get(message.sessionId);
215
- if (windowData) {
216
- if (message.requestedFrameId) {
217
- windowData.requestedFrameId = message.requestedFrameId;
218
- }
219
- message.frames.forEach(messageFrameData => {
220
- let frameData = windowData.frames.find(frameData => messageFrameData.windowId == frameData.windowId);
221
- if (!frameData) {
222
- frameData = { windowId: messageFrameData.windowId };
223
- windowData.frames.push(frameData);
224
- }
225
- if (!frameData.processed) {
226
- frameData.content = messageFrameData.content;
227
- frameData.baseURI = messageFrameData.baseURI;
228
- frameData.title = messageFrameData.title;
229
- frameData.url = messageFrameData.url;
230
- frameData.canvases = messageFrameData.canvases;
231
- frameData.fonts = messageFrameData.fonts;
232
- frameData.stylesheets = messageFrameData.stylesheets;
233
- frameData.images = messageFrameData.images;
234
- frameData.posters = messageFrameData.posters;
235
- frameData.videos = messageFrameData.videos;
236
- frameData.usedFonts = messageFrameData.usedFonts;
237
- frameData.shadowRoots = messageFrameData.shadowRoots;
238
- frameData.processed = messageFrameData.processed;
239
- frameData.scrollPosition = messageFrameData.scrollPosition;
240
- frameData.scrolling = messageFrameData.scrolling;
241
- frameData.adoptedStyleSheets = messageFrameData.adoptedStyleSheets;
242
- }
243
- });
244
- const remainingFrames = windowData.frames.filter(frameData => !frameData.processed).length;
245
- if (!remainingFrames) {
246
- windowData.frames = windowData.frames.sort((frame1, frame2) => frame2.windowId.split(WINDOW_ID_SEPARATOR).length - frame1.windowId.split(WINDOW_ID_SEPARATOR).length);
247
- if (windowData.resolve) {
248
- if (windowData.requestedFrameId) {
249
- windowData.frames.forEach(frameData => {
250
- if (frameData.windowId == windowData.requestedFrameId) {
251
- frameData.requestedFrame = true;
252
- }
253
- });
254
- }
255
- windowData.resolve(windowData.frames);
256
- }
257
- }
258
- }
259
- }
260
- function processFrames(doc, options, parentWindowId, sessionId) {
261
- const frameElements = getFrames(doc);
262
- processFramesAsync(doc, frameElements, options, parentWindowId, sessionId);
263
- if (frameElements.length) {
264
- processFramesSync(doc, frameElements, options, parentWindowId, sessionId);
265
- }
266
- }
267
-
268
- function processFramesAsync(doc, frameElements, options, parentWindowId, sessionId) {
269
- const frames = [];
270
- let requestTimeouts;
271
- if (sessions.get(sessionId)) {
272
- requestTimeouts = sessions.get(sessionId).requestTimeouts;
273
- } else {
274
- requestTimeouts = {};
275
- sessions.set(sessionId, { requestTimeouts });
276
- }
277
- frameElements.forEach((frameElement, frameIndex) => {
278
- const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
279
- frameElement.setAttribute(helper.WIN_ID_ATTRIBUTE_NAME, windowId);
280
- frames.push({ windowId });
281
- });
282
- sendInitResponse({ frames, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
283
- frameElements.forEach((frameElement, frameIndex) => {
284
- const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
285
- try {
286
- sendMessage(frameElement.contentWindow, { method: INIT_REQUEST_MESSAGE, windowId, sessionId, options, scrolling: frameElement.scrolling });
287
- } catch (error) {
288
- // ignored
289
- }
290
- requestTimeouts[windowId] = globalThis.setTimeout(() => sendInitResponse({ frames: [{ windowId, processed: true }], sessionId }), TIMEOUT_INIT_REQUEST_MESSAGE);
291
- });
292
- delete doc.documentElement.dataset.requestedFrameId;
293
- }
294
-
295
- function processFramesSync(doc, frameElements, options, parentWindowId, sessionId) {
296
- const frames = [];
297
- frameElements.forEach((frameElement, frameIndex) => {
298
- const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
299
- let frameDoc;
300
- try {
301
- frameDoc = frameElement.contentDocument;
302
- } catch (error) {
303
- // ignored
304
- }
305
- if (frameDoc) {
306
- try {
307
- const frameWindow = frameElement.contentWindow;
308
- frameWindow.stop();
309
- clearFrameTimeout("requestTimeouts", sessionId, windowId);
310
- processFrames(frameDoc, options, windowId, sessionId);
311
- frames.push(getFrameData(frameDoc, frameWindow, windowId, options, frameElement.scrolling));
312
- } catch (error) {
313
- frames.push({ windowId, processed: true });
314
- }
315
- }
316
- });
317
- sendInitResponse({ frames, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
318
- delete doc.documentElement.dataset.requestedFrameId;
319
- }
320
-
321
- function clearFrameTimeout(type, sessionId, windowId) {
322
- const session = sessions.get(sessionId);
323
- if (session && session[type]) {
324
- const timeout = session[type][windowId];
325
- if (timeout) {
326
- globalThis.clearTimeout(timeout);
327
- delete session[type][windowId];
328
- }
329
- }
330
- }
331
-
332
- function createFrameResponseTimeout(sessionId, windowId) {
333
- const session = sessions.get(sessionId);
334
- if (session && session.responseTimeouts) {
335
- session.responseTimeouts[windowId] = globalThis.setTimeout(() => sendInitResponse({ frames: [{ windowId: windowId, processed: true }], sessionId: sessionId }), TIMEOUT_INIT_RESPONSE_MESSAGE);
336
- }
337
- }
338
-
339
- function cleanupFrames(frameElements, parentWindowId, sessionId) {
340
- frameElements.forEach((frameElement, frameIndex) => {
341
- const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
342
- frameElement.removeAttribute(helper.WIN_ID_ATTRIBUTE_NAME);
343
- try {
344
- sendMessage(frameElement.contentWindow, { method: CLEANUP_REQUEST_MESSAGE, windowId, sessionId });
345
- } catch (error) {
346
- // ignored
347
- }
348
- });
349
- frameElements.forEach((frameElement, frameIndex) => {
350
- const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
351
- let frameDoc;
352
- try {
353
- frameDoc = frameElement.contentDocument;
354
- } catch (error) {
355
- // ignored
356
- }
357
- if (frameDoc) {
358
- try {
359
- cleanupFrames(getFrames(frameDoc), windowId, sessionId);
360
- } catch (error) {
361
- // ignored
362
- }
363
- }
364
- });
365
- }
366
-
367
- function sendInitResponse(message) {
368
- message.method = INIT_RESPONSE_MESSAGE;
369
- try {
370
- top.singlefile.processors.frameTree.initResponse(message);
371
- } catch (error) {
372
- sendMessage(top, message, true);
373
- }
374
- }
375
-
376
- function sendMessage(targetWindow, message, useChannel) {
377
- if (targetWindow == top && browser && browser.runtime && browser.runtime.sendMessage) {
378
- browser.runtime.sendMessage(message);
379
- } else {
380
- if (useChannel) {
381
- const channel = new MessageChannel();
382
- targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: message.method, sessionId: message.sessionId }), TARGET_ORIGIN, [channel.port2]);
383
- channel.port1.postMessage(message);
384
- } else {
385
- targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify(message), TARGET_ORIGIN);
386
- }
387
- }
388
- }
389
-
390
- function getFrameData(document, globalThis, windowId, options, scrolling) {
391
- const docData = helper.preProcessDoc(document, globalThis, options);
392
- const content = helper.serialize(document);
393
- helper.postProcessDoc(document, docData.markedElements, docData.invalidElements);
394
- const baseURI = document.baseURI.split("#")[0];
395
- return {
396
- windowId,
397
- content,
398
- baseURI,
399
- url: document.location.href,
400
- title: document.title,
401
- canvases: docData.canvases,
402
- fonts: docData.fonts,
403
- stylesheets: docData.stylesheets,
404
- images: docData.images,
405
- posters: docData.posters,
406
- videos: docData.videos,
407
- usedFonts: docData.usedFonts,
408
- shadowRoots: docData.shadowRoots,
409
- scrollPosition: docData.scrollPosition,
410
- scrolling,
411
- adoptedStyleSheets: docData.adoptedStyleSheets,
412
- processed: true
413
- };
414
- }
415
-
416
- function getFrames(document) {
417
- let frames = Array.from(document.querySelectorAll(FRAMES_CSS_SELECTOR));
418
- document.querySelectorAll(ALL_ELEMENTS_CSS_SELECTOR).forEach(element => {
419
- const shadowRoot = helper.getShadowRoot(element);
420
- if (shadowRoot) {
421
- frames = frames.concat(...shadowRoot.querySelectorAll(FRAMES_CSS_SELECTOR));
422
- }
423
- });
424
- return frames;
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
+ import * as lazy from "./../../lazy/content/content-lazy-loader.js";
27
+ import {
28
+ ON_BEFORE_CAPTURE_EVENT_NAME,
29
+ ON_AFTER_CAPTURE_EVENT_NAME,
30
+ WIN_ID_ATTRIBUTE_NAME,
31
+ WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
32
+ MESSAGE_PREFIX,
33
+ preProcessDoc,
34
+ serialize,
35
+ postProcessDoc,
36
+ getShadowRoot
37
+ } from "./../../../core/helper.js";
38
+
39
+ const helper = {
40
+ ON_BEFORE_CAPTURE_EVENT_NAME,
41
+ ON_AFTER_CAPTURE_EVENT_NAME,
42
+ WIN_ID_ATTRIBUTE_NAME,
43
+ WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
44
+ preProcessDoc,
45
+ serialize,
46
+ postProcessDoc,
47
+ getShadowRoot
48
+ };
49
+
50
+ const FRAMES_CSS_SELECTOR = "iframe, frame, object[type=\"text/html\"][data]";
51
+ const ALL_ELEMENTS_CSS_SELECTOR = "*";
52
+ const INIT_REQUEST_MESSAGE = "singlefile.frameTree.initRequest";
53
+ const ACK_INIT_REQUEST_MESSAGE = "singlefile.frameTree.ackInitRequest";
54
+ const CLEANUP_REQUEST_MESSAGE = "singlefile.frameTree.cleanupRequest";
55
+ const INIT_RESPONSE_MESSAGE = "singlefile.frameTree.initResponse";
56
+ const TARGET_ORIGIN = "*";
57
+ const TIMEOUT_INIT_REQUEST_MESSAGE = 5000;
58
+ const TIMEOUT_INIT_RESPONSE_MESSAGE = 10000;
59
+ const TOP_WINDOW_ID = "0";
60
+ const WINDOW_ID_SEPARATOR = ".";
61
+ const TOP_WINDOW = globalThis.window == globalThis.top;
62
+
63
+ const browser = globalThis.browser;
64
+ const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
65
+ const top = globalThis.top;
66
+ const MessageChannel = globalThis.MessageChannel;
67
+ const document = globalThis.document;
68
+ const JSON = globalThis.JSON;
69
+
70
+ let sessions = globalThis.sessions;
71
+ if (!sessions) {
72
+ sessions = globalThis.sessions = new Map();
73
+ }
74
+ let windowId;
75
+ if (TOP_WINDOW) {
76
+ windowId = TOP_WINDOW_ID;
77
+ if (browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
78
+ browser.runtime.onMessage.addListener(message => {
79
+ if (message.method == INIT_RESPONSE_MESSAGE) {
80
+ initResponse(message);
81
+ return Promise.resolve({});
82
+ } else if (message.method == ACK_INIT_REQUEST_MESSAGE) {
83
+ clearFrameTimeout("requestTimeouts", message.sessionId, message.windowId);
84
+ createFrameResponseTimeout(message.sessionId, message.windowId);
85
+ return Promise.resolve({});
86
+ }
87
+ });
88
+ }
89
+ }
90
+ addEventListener("message", async event => {
91
+ if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX)) {
92
+ event.preventDefault();
93
+ event.stopPropagation();
94
+ const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length));
95
+ if (message.method == INIT_REQUEST_MESSAGE) {
96
+ if (event.source) {
97
+ sendMessage(event.source, { method: ACK_INIT_REQUEST_MESSAGE, windowId: message.windowId, sessionId: message.sessionId });
98
+ }
99
+ if (!TOP_WINDOW) {
100
+ globalThis.stop();
101
+ if (message.options.loadDeferredImages) {
102
+ lazy.process(message.options);
103
+ }
104
+ await initRequestAsync(message);
105
+ }
106
+ } else if (message.method == ACK_INIT_REQUEST_MESSAGE) {
107
+ clearFrameTimeout("requestTimeouts", message.sessionId, message.windowId);
108
+ createFrameResponseTimeout(message.sessionId, message.windowId);
109
+ } else if (message.method == CLEANUP_REQUEST_MESSAGE) {
110
+ cleanupRequest(message);
111
+ } else if (message.method == INIT_RESPONSE_MESSAGE && sessions.get(message.sessionId)) {
112
+ const port = event.ports[0];
113
+ port.onmessage = event => initResponse(event.data);
114
+ }
115
+ }
116
+ }, true);
117
+
118
+ export {
119
+ getAsync,
120
+ getSync,
121
+ cleanup,
122
+ initResponse,
123
+ TIMEOUT_INIT_REQUEST_MESSAGE
124
+ };
125
+
126
+ function getAsync(options) {
127
+ const sessionId = getNewSessionId();
128
+ options = JSON.parse(JSON.stringify(options));
129
+ return new Promise(resolve => {
130
+ sessions.set(sessionId, {
131
+ frames: [],
132
+ requestTimeouts: {},
133
+ responseTimeouts: {},
134
+ resolve: frames => {
135
+ frames.sessionId = sessionId;
136
+ resolve(frames);
137
+ }
138
+ });
139
+ initRequestAsync({ windowId, sessionId, options });
140
+ });
141
+ }
142
+
143
+ function getSync(options) {
144
+ const sessionId = getNewSessionId();
145
+ options = JSON.parse(JSON.stringify(options));
146
+ sessions.set(sessionId, {
147
+ frames: [],
148
+ requestTimeouts: {},
149
+ responseTimeouts: {}
150
+ });
151
+ initRequestSync({ windowId, sessionId, options });
152
+ const frames = sessions.get(sessionId).frames;
153
+ frames.sessionId = sessionId;
154
+ return frames;
155
+ }
156
+
157
+ function cleanup(sessionId) {
158
+ sessions.delete(sessionId);
159
+ cleanupRequest({ windowId, sessionId, options: { sessionId } });
160
+ }
161
+
162
+ function getNewSessionId() {
163
+ return globalThis.crypto.getRandomValues(new Uint32Array(32)).join("");
164
+ }
165
+
166
+ function initRequestSync(message) {
167
+ const sessionId = message.sessionId;
168
+ const waitForUserScript = globalThis[helper.WAIT_FOR_USERSCRIPT_PROPERTY_NAME];
169
+ delete globalThis._singleFile_cleaningUp;
170
+ if (!TOP_WINDOW) {
171
+ windowId = globalThis.frameId = message.windowId;
172
+ }
173
+ processFrames(document, message.options, windowId, sessionId);
174
+ if (!TOP_WINDOW) {
175
+ if (message.options.userScriptEnabled && waitForUserScript) {
176
+ waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
177
+ }
178
+ sendInitResponse({ frames: [getFrameData(document, globalThis, windowId, message.options, message.scrolling)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
179
+ if (message.options.userScriptEnabled && waitForUserScript) {
180
+ waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
181
+ }
182
+ delete document.documentElement.dataset.requestedFrameId;
183
+ }
184
+ }
185
+
186
+ async function initRequestAsync(message) {
187
+ const sessionId = message.sessionId;
188
+ const waitForUserScript = globalThis[helper.WAIT_FOR_USERSCRIPT_PROPERTY_NAME];
189
+ delete globalThis._singleFile_cleaningUp;
190
+ if (!TOP_WINDOW) {
191
+ windowId = globalThis.frameId = message.windowId;
192
+ }
193
+ processFrames(document, message.options, windowId, sessionId);
194
+ if (!TOP_WINDOW) {
195
+ if (message.options.userScriptEnabled && waitForUserScript) {
196
+ await waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
197
+ }
198
+ sendInitResponse({ frames: [getFrameData(document, globalThis, windowId, message.options, message.scrolling)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
199
+ if (message.options.userScriptEnabled && waitForUserScript) {
200
+ await waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
201
+ }
202
+ delete document.documentElement.dataset.requestedFrameId;
203
+ }
204
+ }
205
+
206
+ function cleanupRequest(message) {
207
+ if (!globalThis._singleFile_cleaningUp) {
208
+ globalThis._singleFile_cleaningUp = true;
209
+ const sessionId = message.sessionId;
210
+ cleanupFrames(getFrames(document), message.windowId, sessionId);
211
+ }
212
+ }
213
+
214
+ function initResponse(message) {
215
+ message.frames.forEach(frameData => clearFrameTimeout("responseTimeouts", message.sessionId, frameData.windowId));
216
+ const windowData = sessions.get(message.sessionId);
217
+ if (windowData) {
218
+ if (message.requestedFrameId) {
219
+ windowData.requestedFrameId = message.requestedFrameId;
220
+ }
221
+ message.frames.forEach(messageFrameData => {
222
+ let frameData = windowData.frames.find(frameData => messageFrameData.windowId == frameData.windowId);
223
+ if (!frameData) {
224
+ frameData = { windowId: messageFrameData.windowId };
225
+ windowData.frames.push(frameData);
226
+ }
227
+ if (!frameData.processed) {
228
+ frameData.content = messageFrameData.content;
229
+ frameData.baseURI = messageFrameData.baseURI;
230
+ frameData.title = messageFrameData.title;
231
+ frameData.url = messageFrameData.url;
232
+ frameData.canvases = messageFrameData.canvases;
233
+ frameData.fonts = messageFrameData.fonts;
234
+ frameData.stylesheets = messageFrameData.stylesheets;
235
+ frameData.images = messageFrameData.images;
236
+ frameData.posters = messageFrameData.posters;
237
+ frameData.videos = messageFrameData.videos;
238
+ frameData.usedFonts = messageFrameData.usedFonts;
239
+ frameData.shadowRoots = messageFrameData.shadowRoots;
240
+ frameData.processed = messageFrameData.processed;
241
+ frameData.scrollPosition = messageFrameData.scrollPosition;
242
+ frameData.scrolling = messageFrameData.scrolling;
243
+ frameData.adoptedStyleSheets = messageFrameData.adoptedStyleSheets;
244
+ }
245
+ });
246
+ const remainingFrames = windowData.frames.filter(frameData => !frameData.processed).length;
247
+ if (!remainingFrames) {
248
+ windowData.frames = windowData.frames.sort((frame1, frame2) => frame2.windowId.split(WINDOW_ID_SEPARATOR).length - frame1.windowId.split(WINDOW_ID_SEPARATOR).length);
249
+ if (windowData.resolve) {
250
+ if (windowData.requestedFrameId) {
251
+ windowData.frames.forEach(frameData => {
252
+ if (frameData.windowId == windowData.requestedFrameId) {
253
+ frameData.requestedFrame = true;
254
+ }
255
+ });
256
+ }
257
+ windowData.resolve(windowData.frames);
258
+ }
259
+ }
260
+ }
261
+ }
262
+ function processFrames(doc, options, parentWindowId, sessionId) {
263
+ const frameElements = getFrames(doc);
264
+ processFramesAsync(doc, frameElements, options, parentWindowId, sessionId);
265
+ if (frameElements.length) {
266
+ processFramesSync(doc, frameElements, options, parentWindowId, sessionId);
267
+ }
268
+ }
269
+
270
+ function processFramesAsync(doc, frameElements, options, parentWindowId, sessionId) {
271
+ const frames = [];
272
+ let requestTimeouts;
273
+ if (sessions.get(sessionId)) {
274
+ requestTimeouts = sessions.get(sessionId).requestTimeouts;
275
+ } else {
276
+ requestTimeouts = {};
277
+ sessions.set(sessionId, { requestTimeouts });
278
+ }
279
+ frameElements.forEach((frameElement, frameIndex) => {
280
+ const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
281
+ frameElement.setAttribute(helper.WIN_ID_ATTRIBUTE_NAME, windowId);
282
+ frames.push({ windowId });
283
+ });
284
+ sendInitResponse({ frames, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
285
+ frameElements.forEach((frameElement, frameIndex) => {
286
+ const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
287
+ try {
288
+ sendMessage(frameElement.contentWindow, { method: INIT_REQUEST_MESSAGE, windowId, sessionId, options, scrolling: frameElement.scrolling });
289
+ } catch (error) {
290
+ // ignored
291
+ }
292
+ requestTimeouts[windowId] = globalThis.setTimeout(() => sendInitResponse({ frames: [{ windowId, processed: true }], sessionId }), TIMEOUT_INIT_REQUEST_MESSAGE);
293
+ });
294
+ delete doc.documentElement.dataset.requestedFrameId;
295
+ }
296
+
297
+ function processFramesSync(doc, frameElements, options, parentWindowId, sessionId) {
298
+ const frames = [];
299
+ frameElements.forEach((frameElement, frameIndex) => {
300
+ const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
301
+ let frameDoc;
302
+ try {
303
+ frameDoc = frameElement.contentDocument;
304
+ } catch (error) {
305
+ // ignored
306
+ }
307
+ if (frameDoc) {
308
+ try {
309
+ const frameWindow = frameElement.contentWindow;
310
+ frameWindow.stop();
311
+ clearFrameTimeout("requestTimeouts", sessionId, windowId);
312
+ processFrames(frameDoc, options, windowId, sessionId);
313
+ frames.push(getFrameData(frameDoc, frameWindow, windowId, options, frameElement.scrolling));
314
+ } catch (error) {
315
+ frames.push({ windowId, processed: true });
316
+ }
317
+ }
318
+ });
319
+ sendInitResponse({ frames, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
320
+ delete doc.documentElement.dataset.requestedFrameId;
321
+ }
322
+
323
+ function clearFrameTimeout(type, sessionId, windowId) {
324
+ const session = sessions.get(sessionId);
325
+ if (session && session[type]) {
326
+ const timeout = session[type][windowId];
327
+ if (timeout) {
328
+ globalThis.clearTimeout(timeout);
329
+ delete session[type][windowId];
330
+ }
331
+ }
332
+ }
333
+
334
+ function createFrameResponseTimeout(sessionId, windowId) {
335
+ const session = sessions.get(sessionId);
336
+ if (session && session.responseTimeouts) {
337
+ session.responseTimeouts[windowId] = globalThis.setTimeout(() => sendInitResponse({ frames: [{ windowId: windowId, processed: true }], sessionId: sessionId }), TIMEOUT_INIT_RESPONSE_MESSAGE);
338
+ }
339
+ }
340
+
341
+ function cleanupFrames(frameElements, parentWindowId, sessionId) {
342
+ frameElements.forEach((frameElement, frameIndex) => {
343
+ const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
344
+ frameElement.removeAttribute(helper.WIN_ID_ATTRIBUTE_NAME);
345
+ try {
346
+ sendMessage(frameElement.contentWindow, { method: CLEANUP_REQUEST_MESSAGE, windowId, sessionId });
347
+ } catch (error) {
348
+ // ignored
349
+ }
350
+ });
351
+ frameElements.forEach((frameElement, frameIndex) => {
352
+ const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
353
+ let frameDoc;
354
+ try {
355
+ frameDoc = frameElement.contentDocument;
356
+ } catch (error) {
357
+ // ignored
358
+ }
359
+ if (frameDoc) {
360
+ try {
361
+ cleanupFrames(getFrames(frameDoc), windowId, sessionId);
362
+ } catch (error) {
363
+ // ignored
364
+ }
365
+ }
366
+ });
367
+ }
368
+
369
+ function sendInitResponse(message) {
370
+ message.method = INIT_RESPONSE_MESSAGE;
371
+ try {
372
+ top.singlefile.processors.frameTree.initResponse(message);
373
+ } catch (error) {
374
+ sendMessage(top, message, true);
375
+ }
376
+ }
377
+
378
+ function sendMessage(targetWindow, message, useChannel) {
379
+ if (targetWindow == top && browser && browser.runtime && browser.runtime.sendMessage) {
380
+ browser.runtime.sendMessage(message);
381
+ } else {
382
+ if (useChannel) {
383
+ const channel = new MessageChannel();
384
+ targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: message.method, sessionId: message.sessionId }), TARGET_ORIGIN, [channel.port2]);
385
+ channel.port1.postMessage(message);
386
+ } else {
387
+ targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify(message), TARGET_ORIGIN);
388
+ }
389
+ }
390
+ }
391
+
392
+ function getFrameData(document, globalThis, windowId, options, scrolling) {
393
+ const docData = helper.preProcessDoc(document, globalThis, options);
394
+ const content = helper.serialize(document);
395
+ helper.postProcessDoc(document, docData.markedElements, docData.invalidElements);
396
+ const baseURI = document.baseURI.split("#")[0];
397
+ return {
398
+ windowId,
399
+ content,
400
+ baseURI,
401
+ url: document.location.href,
402
+ title: document.title,
403
+ canvases: docData.canvases,
404
+ fonts: docData.fonts,
405
+ stylesheets: docData.stylesheets,
406
+ images: docData.images,
407
+ posters: docData.posters,
408
+ videos: docData.videos,
409
+ usedFonts: docData.usedFonts,
410
+ shadowRoots: docData.shadowRoots,
411
+ scrollPosition: docData.scrollPosition,
412
+ scrolling,
413
+ adoptedStyleSheets: docData.adoptedStyleSheets,
414
+ processed: true
415
+ };
416
+ }
417
+
418
+ function getFrames(document) {
419
+ let frames = Array.from(document.querySelectorAll(FRAMES_CSS_SELECTOR));
420
+ document.querySelectorAll(ALL_ELEMENTS_CSS_SELECTOR).forEach(element => {
421
+ const shadowRoot = helper.getShadowRoot(element);
422
+ if (shadowRoot) {
423
+ frames = frames.concat(...shadowRoot.querySelectorAll(FRAMES_CSS_SELECTOR));
424
+ }
425
+ });
426
+ return frames;
425
427
  }