content-lazy-loader.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2010-2020 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. /* global window */
  24. this.singlefile.lib.processors.lazy.content.loader = this.singlefile.lib.processors.lazy.content.loader || (() => {
  25. const ATTRIBUTES_MUTATION_TYPE = "attributes";
  26. const singlefile = this.singlefile;
  27. const browser = this.browser;
  28. const document = window.document;
  29. const MutationObserver = window.MutationObserver;
  30. const addEventListener = (type, listener, options) => window.addEventListener(type, listener, options);
  31. const removeEventListener = (type, listener, options) => window.removeEventListener(type, listener, options);
  32. return {
  33. process: async options => {
  34. if (document.documentElement) {
  35. const maxScrollY = Math.max(document.documentElement.scrollHeight - (document.documentElement.clientHeight * 1.5), 0);
  36. const maxScrollX = Math.max(document.documentElement.scrollWidth - (document.documentElement.clientWidth * 1.5), 0);
  37. if (window.scrollY <= maxScrollY && window.scrollX <= maxScrollX) {
  38. return process(options);
  39. }
  40. }
  41. },
  42. resetZoomLevel: () => {
  43. const frames = singlefile.lib.processors.hooks.content.frames;
  44. if (frames) {
  45. frames.loadDeferredImagesResetZoomLevel();
  46. }
  47. }
  48. };
  49. function process(options) {
  50. const frames = singlefile.lib.processors.hooks.content.frames;
  51. return new Promise(async resolve => { // eslint-disable-line no-async-promise-executor
  52. let timeoutId, idleTimeoutId, maxTimeoutId, loadingImages;
  53. const pendingImages = new Set();
  54. const observer = new MutationObserver(async mutations => {
  55. mutations = mutations.filter(mutation => mutation.type == ATTRIBUTES_MUTATION_TYPE);
  56. if (mutations.length) {
  57. const updated = mutations.filter(mutation => {
  58. if (mutation.attributeName == "src") {
  59. mutation.target.setAttribute(singlefile.lib.helper.LAZY_SRC_ATTRIBUTE_NAME, mutation.target.src);
  60. mutation.target.addEventListener("load", onResourceLoad);
  61. }
  62. if (mutation.attributeName == "src" || mutation.attributeName == "srcset" || mutation.target.tagName == "SOURCE") {
  63. return !mutation.target.classList || !mutation.target.classList.contains(singlefile.lib.helper.SINGLE_FILE_UI_ELEMENT_CLASS);
  64. }
  65. });
  66. if (updated.length) {
  67. loadingImages = true;
  68. maxTimeoutId = await deferForceLazyLoadEnd(timeoutId, idleTimeoutId, maxTimeoutId, observer, options, cleanupAndResolve);
  69. if (!pendingImages.size) {
  70. timeoutId = await deferLazyLoadEnd(timeoutId, idleTimeoutId, observer, options, cleanupAndResolve);
  71. }
  72. }
  73. }
  74. });
  75. idleTimeoutId = await setAsyncTimeout(() => {
  76. if (!loadingImages) {
  77. clearAsyncTimeout(timeoutId);
  78. lazyLoadEnd(idleTimeoutId, observer, options, cleanupAndResolve);
  79. }
  80. }, options.loadDeferredImagesMaxIdleTime * 1.2);
  81. maxTimeoutId = await deferForceLazyLoadEnd(timeoutId, idleTimeoutId, maxTimeoutId, observer, options, cleanupAndResolve);
  82. observer.observe(document, { subtree: true, childList: true, attributes: true });
  83. if (frames) {
  84. addEventListener(frames.LOAD_IMAGE_EVENT, onImageLoadEvent);
  85. addEventListener(frames.IMAGE_LOADED_EVENT, onImageLoadedEvent);
  86. frames.loadDeferredImagesStart(options);
  87. }
  88. function onResourceLoad(event) {
  89. const element = event.target;
  90. element.removeAttribute(singlefile.lib.helper.LAZY_SRC_ATTRIBUTE_NAME);
  91. element.removeEventListener("load", onResourceLoad);
  92. }
  93. async function onImageLoadEvent(event) {
  94. loadingImages = true;
  95. maxTimeoutId = await deferForceLazyLoadEnd(timeoutId, idleTimeoutId, maxTimeoutId, observer, options, cleanupAndResolve);
  96. if (event.detail) {
  97. pendingImages.add(event.detail);
  98. }
  99. }
  100. async function onImageLoadedEvent(event) {
  101. maxTimeoutId = await deferForceLazyLoadEnd(timeoutId, idleTimeoutId, maxTimeoutId, observer, options, cleanupAndResolve);
  102. pendingImages.delete(event.detail);
  103. if (!pendingImages.size) {
  104. timeoutId = await deferLazyLoadEnd(timeoutId, idleTimeoutId, observer, options, cleanupAndResolve);
  105. }
  106. }
  107. function cleanupAndResolve(value) {
  108. observer.disconnect();
  109. if (frames) {
  110. removeEventListener(frames.LOAD_IMAGE_EVENT, onImageLoadEvent);
  111. removeEventListener(frames.IMAGE_LOADED_EVENT, onImageLoadedEvent);
  112. }
  113. resolve(value);
  114. }
  115. });
  116. }
  117. async function deferLazyLoadEnd(timeoutId, idleTimeoutId, observer, options, resolve) {
  118. await clearAsyncTimeout(timeoutId);
  119. return setAsyncTimeout(() => lazyLoadEnd(idleTimeoutId, observer, options, resolve), options.loadDeferredImagesMaxIdleTime);
  120. }
  121. function deferForceLazyLoadEnd(timeoutId, idleTimeoutId, maxTimeoutId, observer, options, resolve) {
  122. clearAsyncTimeout(maxTimeoutId);
  123. return setAsyncTimeout(() => {
  124. clearAsyncTimeout(timeoutId);
  125. lazyLoadEnd(idleTimeoutId, observer, options, resolve);
  126. }, options.loadDeferredImagesMaxIdleTime * 10);
  127. }
  128. function lazyLoadEnd(idleTimeoutId, observer, options, resolve) {
  129. clearAsyncTimeout(idleTimeoutId);
  130. if (singlefile.lib.processors.hooks.content.frames) {
  131. singlefile.lib.processors.hooks.content.frames.loadDeferredImagesEnd(options);
  132. }
  133. setAsyncTimeout(resolve, options.loadDeferredImagesMaxIdleTime / 2);
  134. observer.disconnect();
  135. }
  136. async function setAsyncTimeout(callback, delay) {
  137. if (browser && browser.runtime && browser.runtime.sendMessage) {
  138. const timeoutId = await browser.runtime.sendMessage({ method: "singlefile.lazyTimeout.setTimeout", delay });
  139. const timeoutCallback = message => {
  140. if (message.method == "singlefile.lazyTimeout.onTimeout" && message.id == timeoutId) {
  141. browser.runtime.onMessage.removeListener(timeoutCallback);
  142. callback();
  143. return Promise.resolve({});
  144. }
  145. };
  146. browser.runtime.onMessage.addListener(timeoutCallback);
  147. return timeoutId;
  148. } else {
  149. return window.setTimeout(callback, delay);
  150. }
  151. }
  152. async function clearAsyncTimeout(timeoutId) {
  153. if (browser && browser.runtime && browser.runtime.sendMessage) {
  154. await browser.runtime.sendMessage({ method: "singlefile.lazyTimeout.clearTimeout", id: timeoutId });
  155. } else {
  156. return window.clearTimeout(timeoutId);
  157. }
  158. }
  159. })();