content-lazy-loader.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, lazyLoader */
  21. this.lazyLoader = this.lazyLoader || (() => {
  22. const SCRIPT_TAG_NAME = "script";
  23. const ATTRIBUTES_MUTATION_TYPE = "attributes";
  24. const SCRIPT_BEFORE_PATH = "lib/lazy/web/web-lazy-loader-before.js";
  25. const SCRIPT_AFTER_PATH = "lib/lazy/web/web-lazy-loader-after.js";
  26. const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
  27. const LAZY_SRC_ATTRIBUTE_NAME = "data-lazy-loaded-src";
  28. return { process };
  29. function process(options) {
  30. return new Promise(async resolve => {
  31. let timeoutId, srcAttributeChanged;
  32. setAsyncTimeout(() => {
  33. clearAsyncTimeout(timeoutId);
  34. lazyLoadEnd(idleTimeoutId, observer, resolve);
  35. }, options.loadDeferredImagesMaxIdleTime * 5);
  36. const observer = new MutationObserver(async mutations => {
  37. mutations = mutations.filter(mutation => mutation.type == ATTRIBUTES_MUTATION_TYPE);
  38. if (mutations.length) {
  39. const updated = mutations.filter(mutation => {
  40. if (mutation.attributeName == "src") {
  41. mutation.target.setAttribute(LAZY_SRC_ATTRIBUTE_NAME, mutation.target.src);
  42. }
  43. if (mutation.attributeName == "src" || mutation.attributeName == "srcset" || mutation.target.tagName == "SOURCE") {
  44. return mutation.target.className != SINGLE_FILE_UI_ELEMENT_CLASS;
  45. }
  46. });
  47. if (updated.length) {
  48. srcAttributeChanged = true;
  49. timeoutId = await deferLazyLoadEnd(timeoutId, idleTimeoutId, observer, options, resolve);
  50. }
  51. }
  52. });
  53. observer.observe(document, { subtree: true, childList: true, attributes: true });
  54. const idleTimeoutId = await setAsyncTimeout(() => {
  55. if (!srcAttributeChanged) {
  56. clearAsyncTimeout(timeoutId);
  57. lazyLoadEnd(idleTimeoutId, observer, resolve);
  58. }
  59. }, options.loadDeferredImagesMaxIdleTime * 1.2);
  60. injectScript(SCRIPT_BEFORE_PATH);
  61. });
  62. }
  63. async function deferLazyLoadEnd(timeoutId, idleTimeoutId, observer, options, resolve) {
  64. await clearAsyncTimeout(timeoutId);
  65. return setAsyncTimeout(async () => await lazyLoadEnd(idleTimeoutId, observer, resolve), options.loadDeferredImagesMaxIdleTime);
  66. }
  67. function lazyLoadEnd(idleTimeoutId, observer, resolve) {
  68. clearAsyncTimeout(idleTimeoutId);
  69. injectScript(SCRIPT_AFTER_PATH);
  70. setAsyncTimeout(resolve, 100);
  71. observer.disconnect();
  72. }
  73. function injectScript(path) {
  74. const scriptElement = document.createElement(SCRIPT_TAG_NAME);
  75. if (lazyLoader.getScriptPath) {
  76. scriptElement.src = lazyLoader.getScriptPath(path);
  77. } else {
  78. scriptElement.textContent = lazyLoader.getScriptContent(path);
  79. }
  80. (document.documentElement || document).appendChild(scriptElement);
  81. scriptElement.onload = () => scriptElement.remove();
  82. }
  83. async function setAsyncTimeout(callback, delay) {
  84. if (this.browser && browser.runtime && browser.runtime.sendMessage) {
  85. const timeoutId = await browser.runtime.sendMessage({ setTimeoutRequest: true, delay });
  86. const timeoutCallback = message => {
  87. if (message.onTimeout && message.id == timeoutId) {
  88. browser.runtime.onMessage.removeListener(timeoutCallback);
  89. callback();
  90. }
  91. };
  92. browser.runtime.onMessage.addListener(timeoutCallback);
  93. return timeoutId;
  94. } else {
  95. return setTimeout(callback, delay);
  96. }
  97. }
  98. async function clearAsyncTimeout(timeoutId) {
  99. if (this.browser && browser && browser.runtime && browser.runtime.sendMessage) {
  100. await browser.runtime.sendMessage({ clearTimeout: true, id: timeoutId });
  101. } else {
  102. return clearTimeout(timeoutId);
  103. }
  104. }
  105. })();