hooks-web.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2010-2019 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, addEventListener, dispatchEvent, CustomEvent, document, screen, Element, UIEvent */
  24. this.hooksFrame = this.hooksFrame || (() => {
  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 LOAD_IMAGE_EVENT = "single-file-load-image";
  28. const IMAGE_LOADED_EVENT = "single-file-image-loaded";
  29. addEventListener(LOAD_DEFERRED_IMAGES_START_EVENT, () => {
  30. const clientHeight = document.documentElement.clientHeight;
  31. const clientWidth = document.documentElement.clientWidth;
  32. const scrollHeight = Math.max(document.documentElement.scrollHeight - (clientHeight * .5), clientHeight);
  33. const scrollWidth = Math.max(document.documentElement.scrollWidth - (clientWidth * .5), clientWidth);
  34. document.documentElement.__defineGetter__("clientHeight", () => scrollHeight);
  35. document.documentElement.__defineGetter__("clientWidth", () => scrollWidth);
  36. screen.__defineGetter__("height", () => scrollHeight);
  37. screen.__defineGetter__("width", () => scrollWidth);
  38. try {
  39. document.__defineGetter__("cookie", () => { throw new Error("document.cookie temporary blocked by SingleFile"); });
  40. } catch (error) {
  41. // ignored
  42. }
  43. if (!window._singleFile_getBoundingClientRect) {
  44. window._singleFile_getBoundingClientRect = Element.prototype.getBoundingClientRect;
  45. Element.prototype.getBoundingClientRect = function () {
  46. const boundingRect = window._singleFile_getBoundingClientRect.call(this);
  47. if (this == document.documentElement) {
  48. boundingRect.__defineGetter__("height", () => scrollHeight);
  49. boundingRect.__defineGetter__("bottom", () => scrollHeight + boundingRect.top);
  50. boundingRect.__defineGetter__("width", () => scrollWidth);
  51. boundingRect.__defineGetter__("right", () => scrollWidth + boundingRect.left);
  52. }
  53. return boundingRect;
  54. };
  55. window._singleFile_innerHeight = window.innerHeight;
  56. window._singleFile_innerWidth = window.innerWidth;
  57. window.__defineGetter__("innerHeight", () => scrollHeight);
  58. window.__defineGetter__("innerWidth", () => scrollWidth);
  59. }
  60. if (!window._singleFile_localStorage) {
  61. window._singleFile_localStorage = window.localStorage;
  62. window.__defineGetter__("localStorage", () => { throw new Error("localStorage temporary blocked by SingleFile"); });
  63. }
  64. if (!window._singleFile_indexedDB) {
  65. window._singleFile_indexedDB = window.indexedDB;
  66. window.__defineGetter__("indexedDB", () => { throw new Error("indexedDB temporary blocked by SingleFile"); });
  67. }
  68. if (!window._singleFileImage) {
  69. const Image = window.Image;
  70. window._singleFileImage = window.Image;
  71. window.__defineGetter__("Image", function () {
  72. return function () {
  73. const image = new Image(...arguments);
  74. const result = new Image(...arguments);
  75. result.__defineSetter__("src", function (value) {
  76. image.src = value;
  77. dispatchEvent(new CustomEvent(LOAD_IMAGE_EVENT, { detail: image.src }));
  78. });
  79. result.__defineGetter__("src", function () {
  80. return image.src;
  81. });
  82. result.__defineSetter__("srcset", function (value) {
  83. dispatchEvent(new CustomEvent(LOAD_IMAGE_EVENT));
  84. image.srcset = value;
  85. });
  86. result.__defineGetter__("srcset", function () {
  87. return image.srcset;
  88. });
  89. image.onload = image.onloadend = image.onerror = event => {
  90. dispatchEvent(new CustomEvent(IMAGE_LOADED_EVENT, { detail: image.src }));
  91. result.dispatchEvent(new UIEvent(event.type, event));
  92. };
  93. return result;
  94. };
  95. });
  96. }
  97. const zoomFactorX = (clientHeight + window.scrollY) / scrollHeight;
  98. const zoomFactorY = (clientWidth + window.scrollX) / scrollWidth;
  99. const zoomFactor = Math.min(zoomFactorX, zoomFactorY);
  100. if (zoomFactor < 1) {
  101. const transform = document.documentElement.style.getPropertyValue("transform");
  102. const transformPriority = document.documentElement.style.getPropertyPriority("transform");
  103. const transformOrigin = document.documentElement.style.getPropertyValue("transform-origin");
  104. const transformOriginPriority = document.documentElement.style.getPropertyPriority("transform-origin");
  105. document.documentElement.style.setProperty("transform-origin", (zoomFactorX < 1 ? "50%" : "0") + " " + (zoomFactorY < 1 ? "50%" : "0") + " 0", "important");
  106. document.documentElement.style.setProperty("transform", "scale3d(" + zoomFactor + ", " + zoomFactor + ", 1)", "important");
  107. dispatchEvent(new UIEvent("resize"));
  108. dispatchEvent(new UIEvent("scroll"));
  109. document.documentElement.style.setProperty("transform", transform, transformPriority);
  110. document.documentElement.style.setProperty("transform-origin", transformOrigin, transformOriginPriority);
  111. }
  112. dispatchEvent(new UIEvent("resize"));
  113. dispatchEvent(new UIEvent("scroll"));
  114. });
  115. addEventListener(LOAD_DEFERRED_IMAGES_END_EVENT, () => {
  116. delete document.documentElement.clientHeight;
  117. delete document.documentElement.clientWidth;
  118. delete screen.height;
  119. delete screen.width;
  120. delete document.cookie;
  121. if (window._singleFile_getBoundingClientRect) {
  122. Element.prototype.getBoundingClientRect = window._singleFile_getBoundingClientRect;
  123. window.innerHeight = window._singleFile_innerHeight;
  124. window.innerWidth = window._singleFile_innerWidth;
  125. delete window._singleFile_getBoundingClientRect;
  126. delete window._singleFile_innerHeight;
  127. delete window._singleFile_innerWidth;
  128. }
  129. if (window._singleFile_localStorage) {
  130. delete window.localStorage;
  131. window.localStorage = window._singleFile_localStorage;
  132. delete window._singleFile_localStorage;
  133. }
  134. if (!window._singleFile_indexedDB) {
  135. delete window.indexedDB;
  136. window.indexedDB = window._singleFile_indexedDB;
  137. delete window._singleFile_indexedDB;
  138. }
  139. if (window._singleFileImage) {
  140. delete window.Image;
  141. window.Image = window._singleFileImage;
  142. delete window._singleFileImage;
  143. }
  144. dispatchEvent(new UIEvent("resize"));
  145. dispatchEvent(new UIEvent("scroll"));
  146. });
  147. })();