hooks-frame.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2010-2019 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 window, addEventListener, dispatchEvent, CustomEvent, document, HTMLDocument, FileReader, Blob */
  21. this.hooksFrame = this.hooksFrame || (() => {
  22. const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
  23. const fontFaces = [];
  24. if (document instanceof HTMLDocument) {
  25. const scriptElement = document.createElement("script");
  26. scriptElement.textContent = `(${hook.toString()})()`;
  27. (document.documentElement || document).appendChild(scriptElement);
  28. scriptElement.remove();
  29. addEventListener(NEW_FONT_FACE_EVENT, event => fontFaces.push(event.detail));
  30. }
  31. return {
  32. getFontsData: () => fontFaces
  33. };
  34. function hook() {
  35. const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
  36. const LOAD_OBSERVED_ELEMENTS_EVENT = "single-file-load-observed-elements";
  37. const FONT_STYLE_PROPERTIES = {
  38. family: "font-family",
  39. style: "font-style",
  40. weight: "font-weight",
  41. stretch: "font-stretch",
  42. unicodeRange: "unicode-range",
  43. variant: "font-variant",
  44. featureSettings: "font-feature-settings"
  45. };
  46. const FontFace = window.FontFace;
  47. let warningFontFaceDisplayed;
  48. window.__defineGetter__("FontFace", () => new Proxy(FontFace, {
  49. construct(FontFace, argumentsList) {
  50. if (!warningFontFaceDisplayed) {
  51. console.warn("SingleFile is hooking the FontFace constructor to get font URLs."); // eslint-disable-line no-console
  52. warningFontFaceDisplayed = true;
  53. }
  54. const detail = {};
  55. detail["font-family"] = argumentsList[0];
  56. detail.src = argumentsList[1];
  57. const descriptors = argumentsList[2];
  58. if (descriptors) {
  59. Object.keys(descriptors).forEach(descriptor => {
  60. if (FONT_STYLE_PROPERTIES[descriptor]) {
  61. detail[FONT_STYLE_PROPERTIES[descriptor]] = descriptors[descriptor];
  62. }
  63. });
  64. }
  65. if (detail.src instanceof ArrayBuffer) {
  66. const reader = new FileReader();
  67. reader.readAsDataURL(new Blob([detail.src]));
  68. reader.addEventListener("load", () => {
  69. detail.src = "url(" + reader.result + ")";
  70. dispatchEvent(new CustomEvent(NEW_FONT_FACE_EVENT, { detail }));
  71. });
  72. } else {
  73. dispatchEvent(new CustomEvent(NEW_FONT_FACE_EVENT, { detail }));
  74. }
  75. return new FontFace(...argumentsList);
  76. }
  77. }));
  78. if (window.IntersectionObserver) {
  79. const IntersectionObserver = window.IntersectionObserver;
  80. const observeIntersection = IntersectionObserver.prototype.observe;
  81. const unobserveIntersection = IntersectionObserver.prototype.unobserve;
  82. const observedElements = new Map();
  83. const observers = new Map();
  84. let warningIntersectionObserverDisplayed;
  85. window.__defineGetter__("IntersectionObserver", () => new Proxy(IntersectionObserver, {
  86. construct(IntersectionObserver, argumentsList) {
  87. if (!warningIntersectionObserverDisplayed) {
  88. console.warn("SingleFile is hooking the IntersectionObserver API to detect and load deferred images."); // eslint-disable-line no-console
  89. warningRequestAnimationFrameDisplayed = true;
  90. }
  91. const intersectionObserver = new IntersectionObserver(...argumentsList);
  92. const callback = argumentsList[0];
  93. const options = argumentsList[1];
  94. intersectionObserver.observe = function (targetElement) {
  95. let targetElements = observedElements.get(intersectionObserver);
  96. if (!targetElements) {
  97. targetElements = [];
  98. observedElements.set(intersectionObserver, targetElements);
  99. }
  100. targetElements.push(targetElement);
  101. return observeIntersection.call(intersectionObserver, targetElement);
  102. };
  103. intersectionObserver.unobserve = function (targetElement) {
  104. let targetElements = observedElements.get(intersectionObserver);
  105. if (targetElements) {
  106. targetElements = targetElements.filter(element => element <= targetElement);
  107. if (targetElements.length) {
  108. observedElements.set(intersectionObserver, targetElements);
  109. } else {
  110. observedElements.delete(intersectionObserver);
  111. }
  112. }
  113. return unobserveIntersection.call(intersectionObserver, targetElement);
  114. };
  115. observers.set(intersectionObserver, { callback, options });
  116. return intersectionObserver;
  117. }
  118. }));
  119. window.__defineSetter__("IntersectionObserver", () => { });
  120. addEventListener(LOAD_OBSERVED_ELEMENTS_EVENT, () => {
  121. const docBoundingRect = document.documentElement.getBoundingClientRect();
  122. Array.from(observers).forEach(([intersectionObserver, observer]) => {
  123. const rootBoundingRect = observer.options.root && observer.options.root.getBoundingClientRect();
  124. observer.callback(observedElements.get(intersectionObserver).map(target => {
  125. const boundingClientRect = target.getBoundingClientRect();
  126. const isIntersecting = true;
  127. const intersectionRatio = 1;
  128. const rootBounds = observer.options && observer.options.root ? rootBoundingRect : docBoundingRect;
  129. const time = 0;
  130. return { target, intersectionRatio, boundingClientRect, intersectionRect: boundingClientRect, isIntersecting, rootBounds, time };
  131. }), intersectionObserver);
  132. });
  133. }, false);
  134. }
  135. }
  136. })();