content-lazy-loader.js 7.0 KB

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