content-lazy-loader.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 LAZY_LOADING_TIMEOUT = 1000;
  23. const IDLE_LAZY_LOADING_TIMEOUT = 1200;
  24. const MAX_LAZY_LOADING_TIMEOUT = 30000;
  25. const SCRIPT_TAG_NAME = "script";
  26. const MONITORED_ATTRIBUTES = ["src", "srcset"];
  27. const ATTRIBUTES_MUTATION_TYPE = "attributes";
  28. const SCRIPT_BEFORE_PATH = "lib/lazy/web-lazy-loader-before.js";
  29. const SCRIPT_AFTER_PATH = "lib/lazy/web-lazy-loader-after.js";
  30. return { process };
  31. function process() {
  32. return new Promise(resolve => {
  33. let timeoutId, srcAttributeChanged;
  34. const idleTimeoutId = timeout.set(() => {
  35. if (!srcAttributeChanged) {
  36. timeout.clear(timeoutId);
  37. lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, resolve);
  38. }
  39. }, IDLE_LAZY_LOADING_TIMEOUT);
  40. const maxTimeoutId = timeout.set(() => {
  41. timeout.clear(timeoutId);
  42. lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, resolve);
  43. }, MAX_LAZY_LOADING_TIMEOUT);
  44. const observer = new MutationObserver(mutations => {
  45. mutations = mutations.filter(mutation => mutation.type == ATTRIBUTES_MUTATION_TYPE);
  46. if (mutations.length) {
  47. mutations.forEach(mutation => {
  48. if (mutation.target.src) {
  49. mutation.target.setAttribute("data-lazy-loaded-src", mutation.target.src);
  50. }
  51. });
  52. srcAttributeChanged = true;
  53. timeoutId = deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, resolve);
  54. }
  55. });
  56. observer.observe(document, { attributeFilter: MONITORED_ATTRIBUTES, subtree: true, childList: true, attributes: true });
  57. injectScript(SCRIPT_BEFORE_PATH);
  58. });
  59. }
  60. function deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, resolve) {
  61. timeout.clear(timeoutId);
  62. return timeout.set(() => lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, resolve), LAZY_LOADING_TIMEOUT);
  63. }
  64. function lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, resolve) {
  65. timeout.clear(maxTimeoutId);
  66. timeout.clear(idleTimeoutId);
  67. timeout.set(resolve, LAZY_LOADING_TIMEOUT);
  68. injectScript(SCRIPT_AFTER_PATH);
  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. })();