content-lazy-loader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, srcAttributeChanged;
  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. srcAttributeChanged = 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 (!srcAttributeChanged) {
  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. timeoutId = await deferLazyLoadEnd(timeoutId, idleTimeoutId, observer, options, cleanupAndResolve);
  64. }
  65. function cleanupAndResolve(value) {
  66. observer.disconnect();
  67. removeEventListener(hooksFrame.LOAD_IMAGE_EVENT, onImageEvent);
  68. removeEventListener(hooksFrame.IMAGE_LOADED_EVENT, onImageEvent);
  69. resolve(value);
  70. }
  71. });
  72. }
  73. async function deferLazyLoadEnd(timeoutId, idleTimeoutId, observer, options, resolve) {
  74. await clearAsyncTimeout(timeoutId);
  75. return setAsyncTimeout(async () => await lazyLoadEnd(idleTimeoutId, observer, resolve), options.loadDeferredImagesMaxIdleTime);
  76. }
  77. function lazyLoadEnd(idleTimeoutId, observer, resolve) {
  78. clearAsyncTimeout(idleTimeoutId);
  79. if (typeof hooksFrame != "undefined") {
  80. hooksFrame.loadDeferredImagesEnd();
  81. }
  82. setAsyncTimeout(resolve, 100);
  83. observer.disconnect();
  84. }
  85. async function setAsyncTimeout(callback, delay) {
  86. if (this.browser && browser.runtime && browser.runtime.sendMessage) {
  87. const timeoutId = await browser.runtime.sendMessage({ setTimeoutRequest: true, delay });
  88. const timeoutCallback = message => {
  89. if (message.onTimeout && message.id == timeoutId) {
  90. browser.runtime.onMessage.removeListener(timeoutCallback);
  91. callback();
  92. }
  93. };
  94. browser.runtime.onMessage.addListener(timeoutCallback);
  95. return timeoutId;
  96. } else {
  97. return setTimeout(callback, delay);
  98. }
  99. }
  100. async function clearAsyncTimeout(timeoutId) {
  101. if (this.browser && browser && browser.runtime && browser.runtime.sendMessage) {
  102. await browser.runtime.sendMessage({ clearTimeout: true, id: timeoutId });
  103. } else {
  104. return clearTimeout(timeoutId);
  105. }
  106. }
  107. })();