single-file-core 1.6.36 → 1.6.38

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/core/helper.js CHANGED
@@ -1011,12 +1011,6 @@ function getResourcesInfo(win, doc, element, options, data, elementHidden, compu
1011
1011
  }
1012
1012
  }
1013
1013
  }
1014
- if (tagName == "IFRAME") {
1015
- if (elementHidden && options.removeHiddenElements) {
1016
- element.setAttribute(HIDDEN_FRAME_ATTRIBUTE_NAME, "");
1017
- data.markedElements.push(element);
1018
- }
1019
- }
1020
1014
  if (tagName == "INPUT") {
1021
1015
  if (element.type != "password") {
1022
1016
  element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.value);
@@ -1198,8 +1192,7 @@ function testHiddenElement(element, computedStyle) {
1198
1192
  const display = computedStyle.getPropertyValue("display");
1199
1193
  const opacity = computedStyle.getPropertyValue("opacity");
1200
1194
  const visibility = computedStyle.getPropertyValue("visibility");
1201
- const tagName = element.tagName && element.tagName.toUpperCase();
1202
- hidden = display == "none" || (visibility == "hidden" && tagName == "IFRAME");
1195
+ hidden = display == "none";
1203
1196
  if (!hidden && (opacity == "0" || visibility == "hidden") && element.getBoundingClientRect) {
1204
1197
  const boundingRect = element.getBoundingClientRect();
1205
1198
  hidden = !boundingRect.width && !boundingRect.height;
package/core/index.js CHANGED
@@ -1770,6 +1770,8 @@ class Processor {
1770
1770
  this.stats.add("discarded", "frames", 1);
1771
1771
  }
1772
1772
  }
1773
+ } else if (frameElement.getAttribute(util.HIDDEN_FRAME_ATTRIBUTE_NAME) == "") {
1774
+ this.stats.add("discarded", "frames", 1);
1773
1775
  }
1774
1776
  }));
1775
1777
  capturedFrames.forEach(capturedFrame => {
package/core/util.js CHANGED
@@ -82,6 +82,7 @@ const MANIFEST_OUTCOME_OVERSIZED = "oversized";
82
82
  const MANIFEST_FETCHED_BY_PAGE = "page";
83
83
  const MANIFEST_FETCHED_BY_FRAME = "frame";
84
84
  const MANIFEST_HEADER_NAMES = ["date", "last-modified", "etag", "content-length", "content-encoding", "cache-control", "age", "expires", "server"];
85
+ const MIN_SERVER_ERROR_STATUS = 500;
85
86
 
86
87
  const URL = globalThis.URL;
87
88
  const DOMParser = globalThis.DOMParser;
@@ -95,7 +96,7 @@ export {
95
96
 
96
97
  function getInstance(utilOptions) {
97
98
  utilOptions = utilOptions || {};
98
- utilOptions.fetch = utilOptions.fetch || ((url, options) => globalThis.fetch(url, { ...options, cache: "force-cache", referrerPolicy: "strict-origin-when-cross-origin" }));
99
+ utilOptions.fetch = utilOptions.fetch || ((url, options) => globalThis.fetch(url, { ...options, cache: options.cache || "force-cache", referrerPolicy: "strict-origin-when-cross-origin" }));
99
100
  utilOptions.frameFetch = utilOptions.frameFetch || utilOptions.fetch;
100
101
  return {
101
102
  getDoctypeString,
@@ -287,28 +288,32 @@ function getInstance(utilOptions) {
287
288
  resolveNetworkTimeoutPromise = resolve;
288
289
  });
289
290
  }
291
+ const fetchWithRetry = async (fetchFunction, fetchOptions) => {
292
+ let response = await Promise.race([fetchFunction(resourceURL, fetchOptions), networkTimeoutPromise]);
293
+ if (response.status >= MIN_SERVER_ERROR_STATUS) {
294
+ try {
295
+ response = await Promise.race([fetchFunction(resourceURL, { ...fetchOptions, cache: "reload" }), networkTimeoutPromise]);
296
+ // eslint-disable-next-line no-unused-vars
297
+ } catch (error) {
298
+ // the stored error stands when the network cannot do better
299
+ }
300
+ }
301
+ return response;
302
+ };
290
303
  try {
291
- const accept = (options.acceptHeaders && options.acceptHeaders[options.expectedType]) || "*/*";
304
+ const accept =(options.acceptHeaders && options.acceptHeaders[options.expectedType]) || "*/*";
305
+ const fetchOptions = { referrer: options.resourceReferrer, referrerPolicy: options.referrerPolicy || undefined, stylesheetURL: options.stylesheetURL || undefined, stylesheetReferrerPolicy: options.stylesheetReferrerPolicy || undefined, headers: { accept } };
292
306
  if (options.frameId) {
293
307
  try {
294
- response = await Promise.race([
295
- fetchFrameResource(resourceURL, { frameId: options.frameId, referrer: options.resourceReferrer, referrerPolicy: options.referrerPolicy || undefined, stylesheetURL: options.stylesheetURL || undefined, stylesheetReferrerPolicy: options.stylesheetReferrerPolicy || undefined, headers: { accept } }),
296
- networkTimeoutPromise
297
- ]);
308
+ response = await fetchWithRetry(fetchFrameResource, { frameId: options.frameId, ...fetchOptions });
298
309
  setManifestResponse(entry, response, MANIFEST_FETCHED_BY_FRAME);
299
310
  // eslint-disable-next-line no-unused-vars
300
311
  } catch (error) {
301
- response = await Promise.race([
302
- fetchResource(resourceURL, { referrer: options.resourceReferrer, referrerPolicy: options.referrerPolicy || undefined, stylesheetURL: options.stylesheetURL || undefined, stylesheetReferrerPolicy: options.stylesheetReferrerPolicy || undefined, headers: { accept } }),
303
- networkTimeoutPromise
304
- ]);
312
+ response = await fetchWithRetry(fetchResource, fetchOptions);
305
313
  setManifestResponse(entry, response, MANIFEST_FETCHED_BY_PAGE);
306
314
  }
307
315
  } else {
308
- response = await Promise.race([
309
- fetchResource(resourceURL, { referrer: options.resourceReferrer, referrerPolicy: options.referrerPolicy || undefined, stylesheetURL: options.stylesheetURL || undefined, stylesheetReferrerPolicy: options.stylesheetReferrerPolicy || undefined, headers: { accept } }),
310
- networkTimeoutPromise
311
- ]);
316
+ response = await fetchWithRetry(fetchResource, fetchOptions);
312
317
  setManifestResponse(entry, response, MANIFEST_FETCHED_BY_PAGE);
313
318
  }
314
319
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.6.36",
3
+ "version": "1.6.38",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -26,6 +26,7 @@ import {
26
26
  ON_BEFORE_CAPTURE_EVENT_NAME,
27
27
  ON_AFTER_CAPTURE_EVENT_NAME,
28
28
  WIN_ID_ATTRIBUTE_NAME,
29
+ HIDDEN_FRAME_ATTRIBUTE_NAME,
29
30
  WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
30
31
  MESSAGE_PREFIX,
31
32
  preProcessDoc,
@@ -39,6 +40,7 @@ const helper = {
39
40
  ON_BEFORE_CAPTURE_EVENT_NAME,
40
41
  ON_AFTER_CAPTURE_EVENT_NAME,
41
42
  WIN_ID_ATTRIBUTE_NAME,
43
+ HIDDEN_FRAME_ATTRIBUTE_NAME,
42
44
  WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
43
45
  preProcessDoc,
44
46
  serialize,
@@ -303,6 +305,7 @@ function initResponse(message) {
303
305
  }
304
306
  function processFrames(doc, options, parentWindowId, sessionId, waitForFrames) {
305
307
  const frameElements = getFrames(doc);
308
+ markHiddenFrames(frameElements, options);
306
309
  processFramesAsync(doc, frameElements, options, parentWindowId, sessionId, waitForFrames);
307
310
  if (frameElements.length) {
308
311
  processFramesSync(doc, frameElements, options, parentWindowId, sessionId, waitForFrames);
@@ -319,12 +322,18 @@ function processFramesAsync(doc, frameElements, options, parentWindowId, session
319
322
  sessions.set(sessionId, { requestTimeouts });
320
323
  }
321
324
  frameElements.forEach((frameElement, frameIndex) => {
325
+ if (isHiddenFrame(frameElement)) {
326
+ return;
327
+ }
322
328
  const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
323
329
  frameElement.setAttribute(helper.WIN_ID_ATTRIBUTE_NAME, windowId);
324
330
  frames.push({ windowId });
325
331
  });
326
332
  sendInitResponse({ frames, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
327
333
  frameElements.forEach((frameElement, frameIndex) => {
334
+ if (isHiddenFrame(frameElement)) {
335
+ return;
336
+ }
328
337
  const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
329
338
  try {
330
339
  sendMessage(frameElement.contentWindow, { method: INIT_REQUEST_MESSAGE, windowId, sessionId, options: RUNTIME_MESSAGING ? undefined : options, scrolling: frameElement.scrolling, waitForFrames });
@@ -341,6 +350,9 @@ function processFramesAsync(doc, frameElements, options, parentWindowId, session
341
350
  function processFramesSync(doc, frameElements, options, parentWindowId, sessionId, waitForFrames) {
342
351
  const frames = [];
343
352
  frameElements.forEach((frameElement, frameIndex) => {
353
+ if (isHiddenFrame(frameElement)) {
354
+ return;
355
+ }
344
356
  const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
345
357
  let frameDoc, frameWindow;
346
358
  try {
@@ -430,6 +442,7 @@ function cleanupFrames(frameElements, parentWindowId, sessionId) {
430
442
  frameElements.forEach((frameElement, frameIndex) => {
431
443
  const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
432
444
  frameElement.removeAttribute(helper.WIN_ID_ATTRIBUTE_NAME);
445
+ frameElement.removeAttribute(helper.HIDDEN_FRAME_ATTRIBUTE_NAME);
433
446
  try {
434
447
  sendMessage(frameElement.contentWindow, { method: CLEANUP_REQUEST_MESSAGE, windowId, sessionId });
435
448
  // eslint-disable-next-line no-unused-vars
@@ -519,4 +532,39 @@ function getFrames(document) {
519
532
  }
520
533
  });
521
534
  return frames;
535
+ }
536
+
537
+ // hidden frames are marked and left out of the tree: their content is dropped anyway when hidden
538
+ // elements are removed, and each one would otherwise cost up to the ack timeout; the frames keep
539
+ // their index in the list, which is what the cleanup numbers them by
540
+ function markHiddenFrames(frameElements, options) {
541
+ frameElements.forEach(frameElement => {
542
+ if (options.removeHiddenElements && testHiddenFrame(frameElement)) {
543
+ frameElement.setAttribute(helper.HIDDEN_FRAME_ATTRIBUTE_NAME, "");
544
+ } else {
545
+ frameElement.removeAttribute(helper.HIDDEN_FRAME_ATTRIBUTE_NAME);
546
+ }
547
+ });
548
+ }
549
+
550
+ function isHiddenFrame(frameElement) {
551
+ return frameElement.getAttribute(helper.HIDDEN_FRAME_ATTRIBUTE_NAME) == "";
552
+ }
553
+
554
+ // a frame is a replaced element: nothing in it can overflow its box, and its document cannot undo
555
+ // the visibility set on the element, so a frame with no box or not painted shows nothing, whatever it
556
+ // holds; an opacity of 0 is left alone, it is how an embed fades in once loaded
557
+ function testHiddenFrame(frameElement) {
558
+ try {
559
+ const win = frameElement.ownerDocument.defaultView;
560
+ const computedStyle = win.getComputedStyle(frameElement);
561
+ if (computedStyle.getPropertyValue("display") == "none" || computedStyle.getPropertyValue("visibility") == "hidden") {
562
+ return true;
563
+ }
564
+ const boundingRect = frameElement.getBoundingClientRect();
565
+ return !boundingRect.width && !boundingRect.height;
566
+ // eslint-disable-next-line no-unused-vars
567
+ } catch (error) {
568
+ return false;
569
+ }
522
570
  }