Browse Source

avoid injecting twice hooks

Gildas 7 years ago
parent
commit
90ec2b9335
1 changed files with 49 additions and 43 deletions
  1. 49 43
      lib/hooks/hooks-frame.js

+ 49 - 43
lib/hooks/hooks-frame.js

@@ -75,55 +75,61 @@ this.hooksFrame = this.hooksFrame || (() => {
 			const clientWidth = document.documentElement.clientWidth;
 			const scrollHeight = Math.max(document.documentElement.scrollHeight - (clientHeight * .5), clientHeight);
 			const scrollWidth = Math.max(document.documentElement.scrollWidth - (clientWidth * .5), clientWidth);
-			window._singleFile_innerHeight = window.innerHeight;
-			window._singleFile_innerWidth = window.innerWidth;
-			window.__defineGetter__("innerHeight", () => scrollHeight);
-			window.__defineGetter__("innerWidth", () => scrollWidth);
 			document.documentElement.__defineGetter__("clientHeight", () => scrollHeight);
 			document.documentElement.__defineGetter__("clientWidth", () => scrollWidth);
 			screen.__defineGetter__("height", () => scrollHeight);
 			screen.__defineGetter__("width", () => scrollWidth);
-			window._singleFile_getBoundingClientRect = Element.prototype.getBoundingClientRect;
-			Element.prototype.getBoundingClientRect = function () {
-				const boundingRect = window._singleFile_getBoundingClientRect.call(this);
-				if (this == document.documentElement) {
-					boundingRect.__defineGetter__("height", () => scrollHeight);
-					boundingRect.__defineGetter__("bottom", () => scrollHeight + boundingRect.top);
-					boundingRect.__defineGetter__("width", () => scrollWidth);
-					boundingRect.__defineGetter__("right", () => scrollWidth + boundingRect.left);
-				}
-				return boundingRect;
-			};
-			window._singleFile_localStorage = window.localStorage;
-			window.__defineGetter__("localStorage", () => { throw new Error("localStorage temporary blocked by SingleFile"); });
 			document.__defineGetter__("cookie", () => { throw new Error("document.cookie temporary blocked by SingleFile"); });
-			const Image = window.Image;
-			window._singleFileImage = window.Image;
-			window.__defineGetter__("Image", function () {
-				return function () {
-					const image = new Image(...arguments);
-					const result = new Image(...arguments);
-					result.__defineSetter__("src", function (value) {
-						dispatchEvent(new CustomEvent(LOAD_IMAGE_EVENT));
-						image.src = value;
-					});
-					result.__defineGetter__("src", function () {
-						return image.src;
-					});
-					result.__defineSetter__("srcset", function (value) {
-						dispatchEvent(new CustomEvent(LOAD_IMAGE_EVENT));
-						image.srcset = value;
-					});
-					result.__defineGetter__("srcset", function () {
-						return image.srcset;
-					});
-					image.onload = image.onprogress = image.onloadend = image.onerror = event => {
-						dispatchEvent(new CustomEvent(IMAGE_LOADED_EVENT));
-						result.dispatchEvent(new UIEvent(event.type, event));
-					};
-					return result;
+			if (!window._singleFile_getBoundingClientRect) {
+				window._singleFile_getBoundingClientRect = Element.prototype.getBoundingClientRect;
+				Element.prototype.getBoundingClientRect = function () {
+					const boundingRect = window._singleFile_getBoundingClientRect.call(this);
+					if (this == document.documentElement) {
+						boundingRect.__defineGetter__("height", () => scrollHeight);
+						boundingRect.__defineGetter__("bottom", () => scrollHeight + boundingRect.top);
+						boundingRect.__defineGetter__("width", () => scrollWidth);
+						boundingRect.__defineGetter__("right", () => scrollWidth + boundingRect.left);
+					}
+					return boundingRect;
 				};
-			});
+				window._singleFile_innerHeight = window.innerHeight;
+				window._singleFile_innerWidth = window.innerWidth;
+				window.__defineGetter__("innerHeight", () => scrollHeight);
+				window.__defineGetter__("innerWidth", () => scrollWidth);
+			}
+			if (!window._singleFile_localStorage) {
+				window._singleFile_localStorage = window.localStorage;
+				window.__defineGetter__("localStorage", () => { throw new Error("localStorage temporary blocked by SingleFile"); });
+			}
+			if (!window._singleFileImage) {
+				const Image = window.Image;
+				window._singleFileImage = window.Image;
+				window.__defineGetter__("Image", function () {
+					return function () {
+						const image = new Image(...arguments);
+						const result = new Image(...arguments);
+						result.__defineSetter__("src", function (value) {
+							dispatchEvent(new CustomEvent(LOAD_IMAGE_EVENT));
+							image.src = value;
+						});
+						result.__defineGetter__("src", function () {
+							return image.src;
+						});
+						result.__defineSetter__("srcset", function (value) {
+							dispatchEvent(new CustomEvent(LOAD_IMAGE_EVENT));
+							image.srcset = value;
+						});
+						result.__defineGetter__("srcset", function () {
+							return image.srcset;
+						});
+						image.onload = image.onprogress = image.onloadend = image.onerror = event => {
+							dispatchEvent(new CustomEvent(IMAGE_LOADED_EVENT));
+							result.dispatchEvent(new UIEvent(event.type, event));
+						};
+						return result;
+					};
+				});
+			}
 			dispatchEvent(new UIEvent("resize"));
 			dispatchEvent(new UIEvent("scroll"));
 			const docBoundingRect = document.documentElement.getBoundingClientRect();