hooks.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 window, addEventListener, dispatchEvent, CustomEvent, document, HTMLDocument */
  21. this.hooks = this.hooks || (() => {
  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.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 FONT_STYLE_PROPERTIES = {
  37. family: "font-family",
  38. style: "font-style",
  39. weight: "font-weight",
  40. stretch: "font-stretch",
  41. unicodeRange: "unicode-range",
  42. variant: "font-variant",
  43. featureSettings: "font-feature-settings"
  44. };
  45. const FontFace = window.FontFace;
  46. console.warn("SingleFile is hooking the FontFace constructor to get font URLs."); // eslint-disable-line no-console
  47. window.__defineGetter__("FontFace", () => new Proxy(FontFace, {
  48. construct(FontFace, argumentsList) {
  49. const detail = {};
  50. detail["font-family"] = argumentsList[0];
  51. detail.src = argumentsList[1];
  52. const descriptors = argumentsList[2];
  53. if (descriptors) {
  54. Object.keys(descriptors).forEach(descriptor => detail[FONT_STYLE_PROPERTIES[descriptor]] = descriptors[descriptor]);
  55. }
  56. dispatchEvent(new CustomEvent(NEW_FONT_FACE_EVENT, { detail }));
  57. return new FontFace(...argumentsList);
  58. }
  59. }));
  60. const IntersectionObserver = window.IntersectionObserver;
  61. const observeIntersection = IntersectionObserver.prototype.observe;
  62. const unobserveIntersection = IntersectionObserver.prototype.unobserve;
  63. const observedElements = new Map();
  64. const observers = new Map();
  65. console.warn("SingleFile is hooking the IntersectionObserver API to detect and load deferred images."); // eslint-disable-line no-console
  66. window.__defineGetter__("IntersectionObserver", () => new Proxy(IntersectionObserver, {
  67. construct(IntersectionObserver, argumentsList) {
  68. const intersectionObserver = new IntersectionObserver(...argumentsList);
  69. const callback = argumentsList[0];
  70. const options = argumentsList[1];
  71. intersectionObserver.observe = function (targetElement) {
  72. let targetElements = observedElements.get(intersectionObserver);
  73. if (!targetElements) {
  74. targetElements = [];
  75. observedElements.set(intersectionObserver, targetElements);
  76. }
  77. targetElements.push(targetElement);
  78. return observeIntersection.call(intersectionObserver, targetElement);
  79. };
  80. intersectionObserver.unobserve = function (targetElement) {
  81. let targetElements = observedElements.get(intersectionObserver);
  82. if (targetElements) {
  83. targetElements = targetElements.filter(element => element <= targetElement);
  84. if (targetElements.length) {
  85. observedElements.set(intersectionObserver, targetElements);
  86. } else {
  87. observedElements.delete(intersectionObserver);
  88. }
  89. }
  90. return unobserveIntersection.call(intersectionObserver, targetElement);
  91. };
  92. observers.set(intersectionObserver, { callback, options });
  93. return intersectionObserver;
  94. }
  95. }));
  96. window.__defineSetter__("IntersectionObserver", () => { });
  97. addEventListener("single-file-load-observed-elements", () => {
  98. Array.from(observers).forEach(([intersectionObserver, observer]) => {
  99. observer.callback(observedElements.get(intersectionObserver).map(target => {
  100. const boundingClientRect = target.getBoundingClientRect();
  101. const intersectionRect = target.getBoundingClientRect();
  102. const isIntersecting = true;
  103. const intersectionRatio = 1;
  104. const rootBounds = observer.options && observer.options.root ? observer.options.root.getBoundingClientRect() : document.documentElement.getBoundingClientRect();
  105. const time = 0;
  106. return { target, intersectionRatio, boundingClientRect, intersectionRect, isIntersecting, rootBounds, time };
  107. }), intersectionObserver);
  108. });
  109. }, false);
  110. }
  111. })();