content-lazy-loader.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2018 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 */
  21. this.lazyLoader = this.lazyLoader || (() => {
  22. const SCRIPT_TAG_NAME = "script";
  23. const MONITORED_ATTRIBUTES = ["src", "srcset"];
  24. const ATTRIBUTES_MUTATION_TYPE = "attributes";
  25. const SCRIPT_BEFORE_PATH = "lib/lazy/web/web-lazy-loader-before.js";
  26. const SCRIPT_AFTER_PATH = "lib/lazy/web/web-lazy-loader-after.js";
  27. return { process };
  28. function process(options) {
  29. return new Promise(async resolve => {
  30. let timeoutId, srcAttributeChanged;
  31. const idleTimeoutId = await setTimeout(() => {
  32. if (!srcAttributeChanged) {
  33. clearTimeout(timeoutId);
  34. lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve);
  35. }
  36. }, options.maxLazyLoadImagesIdleTime * 1.2);
  37. const maxTimeoutId = await setTimeout(() => {
  38. clearTimeout(timeoutId);
  39. lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve);
  40. }, options.maxLazyLoadImagesIdleTime * 3);
  41. const observer = new MutationObserver(async mutations => {
  42. mutations = mutations.filter(mutation => mutation.type == ATTRIBUTES_MUTATION_TYPE);
  43. if (mutations.length) {
  44. mutations.forEach(mutation => {
  45. if (mutation.target.src) {
  46. mutation.target.setAttribute("data-lazy-loaded-src", mutation.target.src);
  47. }
  48. });
  49. srcAttributeChanged = true;
  50. timeoutId = await deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, options, resolve);
  51. }
  52. });
  53. observer.observe(document, { attributeFilter: MONITORED_ATTRIBUTES, subtree: true, childList: true, attributes: true });
  54. injectScript(SCRIPT_BEFORE_PATH);
  55. });
  56. }
  57. async function deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, options, resolve) {
  58. await clearTimeout(timeoutId);
  59. return await setTimeout(async () => await lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve), options.maxLazyLoadImagesIdleTime);
  60. }
  61. async function lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve) {
  62. await clearTimeout(idleTimeoutId);
  63. injectScript(SCRIPT_AFTER_PATH);
  64. await setTimeout(resolve, 100);
  65. observer.disconnect();
  66. }
  67. function injectScript(path) {
  68. const scriptElement = document.createElement(SCRIPT_TAG_NAME);
  69. scriptElement.src = browser.runtime.getURL(path);
  70. document.body.appendChild(scriptElement);
  71. scriptElement.onload = () => scriptElement.remove();
  72. }
  73. async function setTimeout(callback, delay) {
  74. const timeoutId = await browser.runtime.sendMessage({ setTimeoutRequest: true, delay });
  75. const timeoutCallback = message => {
  76. if (message.onTimeout && message.id == timeoutId) {
  77. browser.runtime.onMessage.removeListener(timeoutCallback);
  78. callback();
  79. }
  80. };
  81. browser.runtime.onMessage.addListener(timeoutCallback);
  82. return timeoutId;
  83. }
  84. async function clearTimeout(timeoutId) {
  85. await browser.runtime.sendMessage({ clearTimeout: true, id: timeoutId });
  86. }
  87. })();