content-hooks-frames-web.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file 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 GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global window */
  24. (() => {
  25. const LOAD_DEFERRED_IMAGES_START_EVENT = "single-file-load-deferred-images-start";
  26. const LOAD_DEFERRED_IMAGES_END_EVENT = "single-file-load-deferred-images-end";
  27. const BLOCK_COOKIES_START_EVENT = "single-file-block-cookies-start";
  28. const BLOCK_COOKIES_END_EVENT = "single-file-block-cookies-end";
  29. const BLOCK_STORAGE_START_EVENT = "single-file-block-storage-start";
  30. const BLOCK_STORAGE_END_EVENT = "single-file-block-storage-end";
  31. const LAZY_LOAD_ATTRIBUTE = "single-file-lazy-load";
  32. const LOAD_IMAGE_EVENT = "single-file-load-image";
  33. const IMAGE_LOADED_EVENT = "single-file-image-loaded";
  34. const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
  35. const FONT_STYLE_PROPERTIES = {
  36. family: "font-family",
  37. style: "font-style",
  38. weight: "font-weight",
  39. stretch: "font-stretch",
  40. unicodeRange: "unicode-range",
  41. variant: "font-variant",
  42. featureSettings: "font-feature-settings"
  43. };
  44. const addEventListener = window.addEventListener;
  45. const dispatchEvent = window.dispatchEvent;
  46. const CustomEvent = window.CustomEvent;
  47. const document = window.document;
  48. const screen = window.screen;
  49. const Element = window.Element;
  50. const UIEvent = window.UIEvent;
  51. const FileReader = window.FileReader;
  52. const Blob = window.Blob;
  53. const console = window.console;
  54. const warn = (console && console.warn) || (() => { });
  55. const observers = new Map();
  56. const observedElements = new Map();
  57. addEventListener.call(window, LOAD_DEFERRED_IMAGES_START_EVENT, () => {
  58. const scrollingElement = document.scrollingElement || document.documentElement;
  59. const clientHeight = scrollingElement.clientHeight;
  60. const clientWidth = scrollingElement.clientWidth;
  61. const scrollHeight = Math.max(scrollingElement.scrollHeight - clientHeight, clientHeight);
  62. const scrollWidth = Math.max(scrollingElement.scrollWidth - clientWidth, clientWidth);
  63. scrollingElement.__defineGetter__("clientHeight", () => scrollHeight);
  64. scrollingElement.__defineGetter__("clientWidth", () => scrollWidth);
  65. screen.__defineGetter__("height", () => scrollHeight);
  66. screen.__defineGetter__("width", () => scrollWidth);
  67. document.querySelectorAll("[loading=lazy]").forEach(element => {
  68. element.loading = "eager";
  69. element.setAttribute(LAZY_LOAD_ATTRIBUTE, "");
  70. });
  71. if (!window._singleFile_getBoundingClientRect) {
  72. window._singleFile_getBoundingClientRect = Element.prototype.getBoundingClientRect;
  73. Element.prototype.getBoundingClientRect = function () {
  74. const boundingRect = window._singleFile_getBoundingClientRect.call(this);
  75. if (this == scrollingElement) {
  76. boundingRect.__defineGetter__("height", () => scrollHeight);
  77. boundingRect.__defineGetter__("bottom", () => scrollHeight + boundingRect.top);
  78. boundingRect.__defineGetter__("width", () => scrollWidth);
  79. boundingRect.__defineGetter__("right", () => scrollWidth + boundingRect.left);
  80. }
  81. return boundingRect;
  82. };
  83. window._singleFile_innerHeight = window.innerHeight;
  84. window._singleFile_innerWidth = window.innerWidth;
  85. window.__defineGetter__("innerHeight", () => scrollHeight);
  86. window.__defineGetter__("innerWidth", () => scrollWidth);
  87. }
  88. if (!window._singleFileImage) {
  89. const Image = window.Image;
  90. window._singleFileImage = window.Image;
  91. window.__defineGetter__("Image", function () {
  92. return function () {
  93. const image = new Image(...arguments);
  94. const result = new Image(...arguments);
  95. result.__defineSetter__("src", function (value) {
  96. image.src = value;
  97. dispatchEvent.call(window, new CustomEvent(LOAD_IMAGE_EVENT, { detail: image.src }));
  98. });
  99. result.__defineGetter__("src", function () {
  100. return image.src;
  101. });
  102. result.__defineSetter__("srcset", function (value) {
  103. dispatchEvent.call(window, new CustomEvent(LOAD_IMAGE_EVENT));
  104. image.srcset = value;
  105. });
  106. result.__defineGetter__("srcset", function () {
  107. return image.srcset;
  108. });
  109. image.onload = image.onloadend = image.onerror = event => {
  110. dispatchEvent.call(window, new CustomEvent(IMAGE_LOADED_EVENT, { detail: image.src }));
  111. result.dispatchEvent(new UIEvent(event.type, event));
  112. };
  113. if (image.decode) {
  114. result.decode = () => image.decode();
  115. }
  116. return result;
  117. };
  118. });
  119. }
  120. const zoomFactorX = (clientHeight + window.scrollY) / scrollHeight;
  121. const zoomFactorY = (clientWidth + window.scrollX) / scrollWidth;
  122. const zoomFactor = Math.min(zoomFactorX, zoomFactorY);
  123. if (zoomFactor < 1) {
  124. const transform = document.documentElement.style.getPropertyValue("transform");
  125. const transformPriority = document.documentElement.style.getPropertyPriority("transform");
  126. const transformOrigin = document.documentElement.style.getPropertyValue("transform-origin");
  127. const transformOriginPriority = document.documentElement.style.getPropertyPriority("transform-origin");
  128. document.documentElement.style.setProperty("transform-origin", (zoomFactorX < 1 ? "50%" : "0") + " " + (zoomFactorY < 1 ? "50%" : "0") + " 0", "important");
  129. document.documentElement.style.setProperty("transform", "scale3d(" + zoomFactor + ", " + zoomFactor + ", 1)", "important");
  130. dispatchEvent.call(window, new UIEvent("resize"));
  131. dispatchEvent.call(window, new UIEvent("scroll"));
  132. document.documentElement.style.setProperty("transform", transform, transformPriority);
  133. document.documentElement.style.setProperty("transform-origin", transformOrigin, transformOriginPriority);
  134. }
  135. dispatchEvent.call(window, new UIEvent("resize"));
  136. dispatchEvent.call(window, new UIEvent("scroll"));
  137. const docBoundingRect = scrollingElement.getBoundingClientRect();
  138. [...observers].forEach(([intersectionObserver, observer]) => {
  139. const rootBoundingRect = observer.options && observer.options.root && observer.options.root.getBoundingClientRect();
  140. const targetElements = observedElements.get(intersectionObserver);
  141. if (targetElements) {
  142. observer.callback(targetElements.map(target => {
  143. const boundingClientRect = target.getBoundingClientRect();
  144. const isIntersecting = true;
  145. const intersectionRatio = 1;
  146. const rootBounds = observer.options && observer.options.root ? rootBoundingRect : docBoundingRect;
  147. const time = 0;
  148. return { target, intersectionRatio, boundingClientRect, intersectionRect: boundingClientRect, isIntersecting, rootBounds, time };
  149. }), intersectionObserver);
  150. }
  151. });
  152. });
  153. addEventListener.call(window, LOAD_DEFERRED_IMAGES_END_EVENT, () => {
  154. const scrollingElement = document.scrollingElement || document.documentElement;
  155. document.querySelectorAll("[" + LAZY_LOAD_ATTRIBUTE + "]").forEach(element => {
  156. element.loading = "lazy";
  157. element.removeAttribute(LAZY_LOAD_ATTRIBUTE);
  158. });
  159. delete scrollingElement.clientHeight;
  160. delete scrollingElement.clientWidth;
  161. delete screen.height;
  162. delete screen.width;
  163. if (window._singleFile_getBoundingClientRect) {
  164. Element.prototype.getBoundingClientRect = window._singleFile_getBoundingClientRect;
  165. window.innerHeight = window._singleFile_innerHeight;
  166. window.innerWidth = window._singleFile_innerWidth;
  167. delete window._singleFile_getBoundingClientRect;
  168. delete window._singleFile_innerHeight;
  169. delete window._singleFile_innerWidth;
  170. }
  171. if (window._singleFileImage) {
  172. delete window.Image;
  173. window.Image = window._singleFileImage;
  174. delete window._singleFileImage;
  175. }
  176. dispatchEvent.call(window, new UIEvent("resize"));
  177. dispatchEvent.call(window, new UIEvent("scroll"));
  178. });
  179. addEventListener.call(window, BLOCK_COOKIES_START_EVENT, () => {
  180. try {
  181. document.__defineGetter__("cookie", () => { throw new Error("document.cookie temporary blocked by SingleFile"); });
  182. } catch (error) {
  183. // ignored
  184. }
  185. });
  186. addEventListener.call(window, BLOCK_COOKIES_END_EVENT, () => {
  187. delete document.cookie;
  188. });
  189. addEventListener.call(window, BLOCK_STORAGE_START_EVENT, () => {
  190. if (!window._singleFile_localStorage) {
  191. window._singleFile_localStorage = window.localStorage;
  192. window.__defineGetter__("localStorage", () => { throw new Error("localStorage temporary blocked by SingleFile"); });
  193. }
  194. if (!window._singleFile_indexedDB) {
  195. window._singleFile_indexedDB = window.indexedDB;
  196. window.__defineGetter__("indexedDB", () => { throw new Error("indexedDB temporary blocked by SingleFile"); });
  197. }
  198. });
  199. addEventListener.call(window, BLOCK_STORAGE_END_EVENT, () => {
  200. if (window._singleFile_localStorage) {
  201. delete window.localStorage;
  202. window.localStorage = window._singleFile_localStorage;
  203. delete window._singleFile_localStorage;
  204. }
  205. if (!window._singleFile_indexedDB) {
  206. delete window.indexedDB;
  207. window.indexedDB = window._singleFile_indexedDB;
  208. delete window._singleFile_indexedDB;
  209. }
  210. });
  211. if (window.FontFace) {
  212. const FontFace = window.FontFace;
  213. let warningFontFaceDisplayed;
  214. window.FontFace = function () {
  215. if (!warningFontFaceDisplayed) {
  216. warn.call(console, "SingleFile is hooking the FontFace constructor to get font URLs."); // eslint-disable-line no-console
  217. warningFontFaceDisplayed = true;
  218. }
  219. const detail = {};
  220. detail["font-family"] = arguments[0];
  221. detail.src = arguments[1];
  222. const descriptors = arguments[2];
  223. if (descriptors) {
  224. Object.keys(descriptors).forEach(descriptor => {
  225. if (FONT_STYLE_PROPERTIES[descriptor]) {
  226. detail[FONT_STYLE_PROPERTIES[descriptor]] = descriptors[descriptor];
  227. }
  228. });
  229. }
  230. if (detail.src instanceof ArrayBuffer) {
  231. const reader = new FileReader();
  232. reader.readAsDataURL(new Blob([detail.src]));
  233. reader.addEventListener("load", () => {
  234. detail.src = "url(" + reader.result + ")";
  235. dispatchEvent.call(window, new CustomEvent(NEW_FONT_FACE_EVENT, { detail }));
  236. });
  237. } else {
  238. dispatchEvent.call(window, new CustomEvent(NEW_FONT_FACE_EVENT, { detail }));
  239. }
  240. return new FontFace(...arguments);
  241. };
  242. window.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
  243. }
  244. if (window.IntersectionObserver) {
  245. const IntersectionObserver = window.IntersectionObserver;
  246. let warningIntersectionObserverDisplayed;
  247. window.IntersectionObserver = function () {
  248. if (!warningIntersectionObserverDisplayed) {
  249. warn.call(console, "SingleFile is hooking the IntersectionObserver API to detect and load deferred images."); // eslint-disable-line no-console
  250. warningIntersectionObserverDisplayed = true;
  251. }
  252. const intersectionObserver = new IntersectionObserver(...arguments);
  253. const observeIntersection = IntersectionObserver.prototype.observe || intersectionObserver.observe;
  254. const unobserveIntersection = IntersectionObserver.prototype.unobserve || intersectionObserver.unobserve;
  255. const callback = arguments[0];
  256. const options = arguments[1];
  257. if (observeIntersection) {
  258. intersectionObserver.observe = function (targetElement) {
  259. let targetElements = observedElements.get(intersectionObserver);
  260. if (!targetElements) {
  261. targetElements = [];
  262. observedElements.set(intersectionObserver, targetElements);
  263. }
  264. targetElements.push(targetElement);
  265. return observeIntersection.call(intersectionObserver, targetElement);
  266. };
  267. }
  268. if (unobserveIntersection) {
  269. intersectionObserver.unobserve = function (targetElement) {
  270. let targetElements = observedElements.get(intersectionObserver);
  271. if (targetElements) {
  272. targetElements = targetElements.filter(element => element != targetElement);
  273. if (targetElements.length) {
  274. observedElements.set(intersectionObserver, targetElements);
  275. } else {
  276. observedElements.delete(intersectionObserver);
  277. observers.delete(intersectionObserver);
  278. }
  279. }
  280. return unobserveIntersection.call(intersectionObserver, targetElement);
  281. };
  282. }
  283. observers.set(intersectionObserver, { callback, options });
  284. return intersectionObserver;
  285. };
  286. window.IntersectionObserver.prototype = IntersectionObserver.prototype;
  287. window.IntersectionObserver.toString = function () { return "function IntersectionObserver() { [native code] }"; };
  288. }
  289. })();