content-lazy-loader.js 3.7 KB

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