content-lazy-loader.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. 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. const maxTimeoutId = await setTimeout(() => {
  33. clearTimeout(timeoutId);
  34. lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve);
  35. }, options.maxLazyLoadImagesIdleTime * 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. return mutation.attributeName != LAZY_SRC_ATTRIBUTE_NAME && mutation.target.className != SINGLE_FILE_UI_ELEMENT_CLASS;
  44. });
  45. if (updated.length) {
  46. srcAttributeChanged = true;
  47. timeoutId = await deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, options, resolve);
  48. }
  49. }
  50. });
  51. observer.observe(document, { subtree: true, childList: true, attributes: true });
  52. const idleTimeoutId = await setTimeout(async () => {
  53. if (!srcAttributeChanged) {
  54. await clearTimeout(timeoutId);
  55. lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve);
  56. }
  57. }, options.maxLazyLoadImagesIdleTime * 1.2);
  58. injectScript(SCRIPT_BEFORE_PATH);
  59. });
  60. }
  61. async function deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, options, resolve) {
  62. await clearTimeout(timeoutId);
  63. return await setTimeout(async () => await lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve), options.maxLazyLoadImagesIdleTime);
  64. }
  65. async function lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve) {
  66. await clearTimeout(idleTimeoutId);
  67. injectScript(SCRIPT_AFTER_PATH);
  68. await setTimeout(resolve, 100);
  69. observer.disconnect();
  70. }
  71. function injectScript(path) {
  72. const scriptElement = document.createElement(SCRIPT_TAG_NAME);
  73. scriptElement.src = browser.runtime.getURL(path);
  74. document.body.appendChild(scriptElement);
  75. scriptElement.onload = () => scriptElement.remove();
  76. }
  77. async function setTimeout(callback, delay) {
  78. const timeoutId = await browser.runtime.sendMessage({ setTimeoutRequest: true, delay });
  79. const timeoutCallback = message => {
  80. if (message.onTimeout && message.id == timeoutId) {
  81. browser.runtime.onMessage.removeListener(timeoutCallback);
  82. callback();
  83. }
  84. };
  85. browser.runtime.onMessage.addListener(timeoutCallback);
  86. return timeoutId;
  87. }
  88. async function clearTimeout(timeoutId) {
  89. await browser.runtime.sendMessage({ clearTimeout: true, id: timeoutId });
  90. }
  91. })();