content-lazy-loader.js 3.9 KB

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