content-lazy-loader.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, timeout, 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-lazy-loader-before.js";
  26. const SCRIPT_AFTER_PATH = "lib/lazy/web-lazy-loader-after.js";
  27. return { process };
  28. function process(options) {
  29. return new Promise(resolve => {
  30. let timeoutId, srcAttributeChanged;
  31. const idleTimeoutId = timeout.set(() => {
  32. if (!srcAttributeChanged) {
  33. timeout.clear(timeoutId);
  34. lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve);
  35. }
  36. }, options.maxLazyLoadImagesIdleTime * 1.2);
  37. const maxTimeoutId = timeout.set(() => {
  38. timeout.clear(timeoutId);
  39. lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve);
  40. }, options.maxLazyLoadImagesIdleTime * 10);
  41. const observer = new MutationObserver(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 = 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. function deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, options, resolve) {
  58. timeout.clear(timeoutId);
  59. return timeout.set(() => lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve), options.maxLazyLoadImagesIdleTime);
  60. }
  61. function lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve) {
  62. timeout.clear(maxTimeoutId);
  63. timeout.clear(idleTimeoutId);
  64. timeout.set(resolve, options.maxLazyLoadImagesIdleTime);
  65. injectScript(SCRIPT_AFTER_PATH);
  66. observer.disconnect();
  67. }
  68. function injectScript(path) {
  69. const scriptElement = document.createElement(SCRIPT_TAG_NAME);
  70. scriptElement.src = browser.runtime.getURL(path);
  71. document.body.appendChild(scriptElement);
  72. scriptElement.onload = () => scriptElement.remove();
  73. }
  74. })();