content-lazy-loader.js 6.1 KB

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