content-lazy-loader.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 = 3000;
  24. const MAX_LAZY_LOADING_TIMEOUT = 30000;
  25. return { process };
  26. function process() {
  27. return new Promise(resolve => {
  28. let timeoutId, maxTimeoutId, idleTimeoutId, srcAttributeChanged;
  29. const observer = new MutationObserver(() => {
  30. srcAttributeChanged = true;
  31. timeoutId = deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, resolve);
  32. });
  33. observer.observe(document, { attributeFilter: ["src", "srcset"], subtree: true });
  34. const scriptBeforeElement = document.createElement("script");
  35. scriptBeforeElement.src = browser.runtime.getURL("lib/lazy/web-lazy-loader-before.js");
  36. document.body.appendChild(scriptBeforeElement);
  37. scriptBeforeElement.onload = () => scriptBeforeElement.remove();
  38. idleTimeoutId = timeout.set(() => {
  39. if (!srcAttributeChanged) {
  40. timeout.clear(timeoutId);
  41. lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, resolve);
  42. }
  43. }, IDLE_LAZY_LOADING_TIMEOUT);
  44. maxTimeoutId = timeout.set(() => {
  45. timeout.clear(timeoutId);
  46. lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, resolve);
  47. }, MAX_LAZY_LOADING_TIMEOUT);
  48. });
  49. }
  50. function deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, resolve) {
  51. timeout.clear(timeoutId);
  52. return timeout.set(() => lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, resolve), LAZY_LOADING_TIMEOUT);
  53. }
  54. function lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, resolve) {
  55. timeout.clear(maxTimeoutId);
  56. timeout.clear(idleTimeoutId);
  57. timeout.set(resolve, LAZY_LOADING_TIMEOUT);
  58. const scriptURL = browser.runtime.getURL("lib/lazy/web-lazy-loader-after.js");
  59. const scriptAfterElement = document.createElement("script");
  60. scriptAfterElement.src = scriptURL;
  61. document.body.appendChild(scriptAfterElement);
  62. scriptAfterElement.onload = () => scriptAfterElement.remove();
  63. observer.disconnect();
  64. }
  65. })();