lazy-loader.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. this.lazyLoader = this.lazyLoader || (() => {
  21. const DATA_URI_PREFIX = "data:";
  22. const EMPTY_DATA_URI = "data:base64,";
  23. return {
  24. process(doc) {
  25. replaceSrc(doc.querySelectorAll("img[data-src]"), "src");
  26. replaceSrc(doc.querySelectorAll("img[data-original]"), "original");
  27. doc.querySelectorAll("[data-bg]").forEach(element => {
  28. const dataBg = element.dataset.bg;
  29. if (dataBg && dataBg.startsWith(DATA_URI_PREFIX) && dataBg != EMPTY_DATA_URI && !element.style.backgroundImage.includes(dataBg)) {
  30. element.style.backgroundImage = "url(" + element.dataset.bg + ")";
  31. element.removeAttribute("data-bg");
  32. processElement(element);
  33. }
  34. });
  35. doc.querySelectorAll("[data-srcset]").forEach(imgElement => {
  36. const srcset = imgElement.dataset.srcset;
  37. if (srcset && imgElement.srcset != srcset) {
  38. imgElement.srcset = srcset;
  39. imgElement.removeAttribute("data-srcset");
  40. processElement(imgElement);
  41. }
  42. });
  43. doc.querySelectorAll(".lazyload").forEach(element => {
  44. element.classList.add("lazypreload");
  45. element.classList.remove("lazyload");
  46. });
  47. },
  48. imageSelectors: {
  49. src: {
  50. "img[data-src]": "data-src",
  51. "img[data-original]": "data-original",
  52. "img[data-bg]": "data-bg"
  53. },
  54. srcset: {
  55. "[data-srcset]": "data-srcset"
  56. }
  57. }
  58. };
  59. function replaceSrc(elements, attributeName) {
  60. elements.forEach(element => {
  61. const dataSrc = element.dataset[attributeName];
  62. if (dataSrc && dataSrc.startsWith(DATA_URI_PREFIX) && dataSrc != EMPTY_DATA_URI && element.src != dataSrc) {
  63. element.src = element.dataset[attributeName];
  64. element.removeAttribute("data-" + attributeName);
  65. processElement(element);
  66. }
  67. });
  68. }
  69. function processElement(element) {
  70. element.removeAttribute("data-lazyload");
  71. element.classList.remove("b-lazy");
  72. }
  73. })();