content-lazy-loader.js 2.8 KB

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