content-lazy-loader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global browser, document, MutationObserver, setTimeout, clearTimeout, hooksFrame, addEventListener, removeEventListener */
  21. this.lazyLoader = this.lazyLoader || (() => {
  22. const ATTRIBUTES_MUTATION_TYPE = "attributes";
  23. const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
  24. const LAZY_SRC_ATTRIBUTE_NAME = "data-lazy-loaded-src";
  25. return { process };
  26. function process(options) {
  27. return new Promise(async resolve => {
  28. let timeoutId, loadingImages;
  29. setAsyncTimeout(() => {
  30. clearAsyncTimeout(timeoutId);
  31. lazyLoadEnd(idleTimeoutId, observer, cleanupAndResolve);
  32. }, options.loadDeferredImagesMaxIdleTime * 5);
  33. const observer = new MutationObserver(async mutations => {
  34. mutations = mutations.filter(mutation => mutation.type == ATTRIBUTES_MUTATION_TYPE);
  35. if (mutations.length) {
  36. const updated = mutations.filter(mutation => {
  37. if (mutation.attributeName == "src") {
  38. mutation.target.setAttribute(LAZY_SRC_ATTRIBUTE_NAME, mutation.target.src);
  39. }
  40. if (mutation.attributeName == "src" || mutation.attributeName == "srcset" || mutation.target.tagName == "SOURCE") {
  41. return mutation.target.className != SINGLE_FILE_UI_ELEMENT_CLASS;
  42. }
  43. });
  44. if (updated.length) {
  45. loadingImages = true;
  46. timeoutId = await deferLazyLoadEnd(timeoutId, idleTimeoutId, observer, options, cleanupAndResolve);
  47. }
  48. }
  49. });
  50. observer.observe(document, { subtree: true, childList: true, attributes: true });
  51. const idleTimeoutId = await setAsyncTimeout(() => {
  52. if (!loadingImages) {
  53. clearAsyncTimeout(timeoutId);
  54. lazyLoadEnd(idleTimeoutId, observer, cleanupAndResolve);
  55. }
  56. }, options.loadDeferredImagesMaxIdleTime * 1.2);
  57. if (typeof hooksFrame != "undefined") {
  58. hooksFrame.loadDeferredImagesStart();
  59. }
  60. addEventListener(hooksFrame.LOAD_IMAGE_EVENT, onImageEvent);
  61. addEventListener(hooksFrame.IMAGE_LOADED_EVENT, onImageEvent);
  62. async function onImageEvent() {
  63. loadingImages = true;
  64. timeoutId = await deferLazyLoadEnd(timeoutId, idleTimeoutId, observer, options, cleanupAndResolve);
  65. }
  66. function cleanupAndResolve(value) {
  67. observer.disconnect();
  68. removeEventListener(hooksFrame.LOAD_IMAGE_EVENT, onImageEvent);
  69. removeEventListener(hooksFrame.IMAGE_LOADED_EVENT, onImageEvent);
  70. resolve(value);
  71. }
  72. });
  73. }
  74. async function deferLazyLoadEnd(timeoutId, idleTimeoutId, observer, options, resolve) {
  75. await clearAsyncTimeout(timeoutId);
  76. return setAsyncTimeout(async () => await lazyLoadEnd(idleTimeoutId, observer, resolve), options.loadDeferredImagesMaxIdleTime);
  77. }
  78. function lazyLoadEnd(idleTimeoutId, observer, resolve) {
  79. clearAsyncTimeout(idleTimeoutId);
  80. if (typeof hooksFrame != "undefined") {
  81. hooksFrame.loadDeferredImagesEnd();
  82. }
  83. setAsyncTimeout(resolve, 100);
  84. observer.disconnect();
  85. }
  86. async function setAsyncTimeout(callback, delay) {
  87. if (this.browser && browser.runtime && browser.runtime.sendMessage) {
  88. const timeoutId = await browser.runtime.sendMessage({ setTimeoutRequest: true, delay });
  89. const timeoutCallback = message => {
  90. if (message.onTimeout && message.id == timeoutId) {
  91. browser.runtime.onMessage.removeListener(timeoutCallback);
  92. callback();
  93. }
  94. };
  95. browser.runtime.onMessage.addListener(timeoutCallback);
  96. return timeoutId;
  97. } else {
  98. return setTimeout(callback, delay);
  99. }
  100. }
  101. async function clearAsyncTimeout(timeoutId) {
  102. if (this.browser && browser && browser.runtime && browser.runtime.sendMessage) {
  103. await browser.runtime.sendMessage({ clearTimeout: true, id: timeoutId });
  104. } else {
  105. return clearTimeout(timeoutId);
  106. }
  107. }
  108. })();