content-hooks-frames.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 globalThis, window */
  24. const LOAD_DEFERRED_IMAGES_START_EVENT = "single-file-load-deferred-images-start";
  25. const LOAD_DEFERRED_IMAGES_END_EVENT = "single-file-load-deferred-images-end";
  26. const LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_START_EVENT = "single-file-load-deferred-images-keep-zoom-level-start";
  27. const LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_END_EVENT = "single-file-load-deferred-images-keep-zoom-level-end";
  28. const LOAD_DEFERRED_IMAGES_RESET_ZOOM_LEVEL_EVENT = "single-file-load-deferred-images-keep-zoom-level-reset";
  29. const LOAD_DEFERRED_IMAGES_RESET_EVENT = "single-file-load-deferred-images-reset";
  30. const BLOCK_COOKIES_START_EVENT = "single-file-block-cookies-start";
  31. const BLOCK_COOKIES_END_EVENT = "single-file-block-cookies-end";
  32. const BLOCK_STORAGE_START_EVENT = "single-file-block-storage-start";
  33. const BLOCK_STORAGE_END_EVENT = "single-file-block-storage-end";
  34. const LOAD_IMAGE_EVENT = "single-file-load-image";
  35. const IMAGE_LOADED_EVENT = "single-file-image-loaded";
  36. const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
  37. const DELETE_FONT_EVENT = "single-file-delete-font";
  38. const CLEAR_FONTS_EVENT = "single-file-clear-fonts";
  39. const browser = globalThis.browser;
  40. const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
  41. const dispatchEvent = event => globalThis.dispatchEvent(event);
  42. const CustomEvent = globalThis.CustomEvent;
  43. const document = globalThis.document;
  44. const HTMLDocument = globalThis.HTMLDocument;
  45. let fontFaces;
  46. if (window._singleFile_fontFaces) {
  47. fontFaces = window._singleFile_fontFaces;
  48. } else {
  49. fontFaces = window._singleFile_fontFaces = [];
  50. }
  51. if (document instanceof HTMLDocument) {
  52. if (browser && browser.runtime && browser.runtime.getURL) {
  53. addEventListener(NEW_FONT_FACE_EVENT, event => {
  54. const detail = event.detail;
  55. if (!fontFaces.find(fontFace => JSON.stringify(fontFace) == JSON.stringify(detail))) {
  56. fontFaces.push(event.detail);
  57. }
  58. });
  59. addEventListener(DELETE_FONT_EVENT, event => {
  60. const detail = event.detail;
  61. const indexFontFace = fontFaces.findIndex(fontFace => fontFace["family-name"] == detail["family-name"]);
  62. if (indexFontFace != -1) {
  63. fontFaces.splice(indexFontFace, 1);
  64. }
  65. });
  66. addEventListener(CLEAR_FONTS_EVENT, () => fontFaces = []);
  67. let scriptElement = document.createElement("script");
  68. scriptElement.src = "data:," + "(" + injectedScript.toString() + ")()";
  69. (document.documentElement || document).appendChild(scriptElement);
  70. scriptElement.remove();
  71. scriptElement = document.createElement("script");
  72. scriptElement.src = browser.runtime.getURL("/dist/web/hooks/hooks-frames-web.js");
  73. scriptElement.async = false;
  74. (document.documentElement || document).appendChild(scriptElement);
  75. scriptElement.remove();
  76. }
  77. }
  78. export {
  79. getFontsData,
  80. loadDeferredImagesStart,
  81. loadDeferredImagesEnd,
  82. loadDeferredImagesResetZoomLevel,
  83. LOAD_IMAGE_EVENT,
  84. IMAGE_LOADED_EVENT
  85. };
  86. function getFontsData() {
  87. return fontFaces;
  88. }
  89. function loadDeferredImagesStart(options) {
  90. if (options.loadDeferredImagesBlockCookies) {
  91. dispatchEvent(new CustomEvent(BLOCK_COOKIES_START_EVENT));
  92. }
  93. if (options.loadDeferredImagesBlockStorage) {
  94. dispatchEvent(new CustomEvent(BLOCK_STORAGE_START_EVENT));
  95. }
  96. if (options.loadDeferredImagesKeepZoomLevel) {
  97. dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_START_EVENT));
  98. } else {
  99. dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_START_EVENT));
  100. }
  101. }
  102. function loadDeferredImagesEnd(options) {
  103. if (options.loadDeferredImagesBlockCookies) {
  104. dispatchEvent(new CustomEvent(BLOCK_COOKIES_END_EVENT));
  105. }
  106. if (options.loadDeferredImagesBlockStorage) {
  107. dispatchEvent(new CustomEvent(BLOCK_STORAGE_END_EVENT));
  108. }
  109. if (options.loadDeferredImagesKeepZoomLevel) {
  110. dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_END_EVENT));
  111. } else {
  112. dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_END_EVENT));
  113. }
  114. }
  115. function loadDeferredImagesResetZoomLevel(options) {
  116. if (options.loadDeferredImagesKeepZoomLevel) {
  117. dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_RESET_ZOOM_LEVEL_EVENT));
  118. } else {
  119. dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_RESET_EVENT));
  120. }
  121. }
  122. function injectedScript() {
  123. if (typeof globalThis == "undefined") {
  124. window.globalThis = window;
  125. }
  126. const document = globalThis.document;
  127. const console = globalThis.console;
  128. const dispatchEvent = event => globalThis.dispatchEvent(event);
  129. const CustomEvent = globalThis.CustomEvent;
  130. const FileReader = globalThis.FileReader;
  131. const Blob = globalThis.Blob;
  132. const warn = (console && console.warn && ((...args) => console.warn(...args))) || (() => { });
  133. const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
  134. const DELETE_FONT_EVENT = "single-file-delete-font";
  135. const CLEAR_FONTS_EVENT = "single-file-clear-fonts";
  136. const FONT_STYLE_PROPERTIES = {
  137. family: "font-family",
  138. style: "font-style",
  139. weight: "font-weight",
  140. stretch: "font-stretch",
  141. unicodeRange: "unicode-range",
  142. variant: "font-variant",
  143. featureSettings: "font-feature-settings"
  144. };
  145. if (globalThis.FontFace) {
  146. const FontFace = globalThis.FontFace;
  147. let warningFontFaceDisplayed;
  148. globalThis.FontFace = function () {
  149. if (!warningFontFaceDisplayed) {
  150. warn("SingleFile is hooking the FontFace constructor, document.fonts.delete and document.fonts.clear to handle dynamically loaded fonts.");
  151. warningFontFaceDisplayed = true;
  152. }
  153. getDetailObject(...arguments).then(detail => dispatchEvent(new CustomEvent(NEW_FONT_FACE_EVENT, { detail })));
  154. return new FontFace(...arguments);
  155. };
  156. globalThis.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
  157. const deleteFont = document.fonts.delete;
  158. document.fonts.delete = function (fontFace) {
  159. getDetailObject(fontFace.family).then(detail => dispatchEvent(new CustomEvent(DELETE_FONT_EVENT, { detail })));
  160. return deleteFont.call(document.fonts, fontFace);
  161. };
  162. document.fonts.delete.toString = function () { return "function delete() { [native code] }"; };
  163. const clearFonts = document.fonts.clear;
  164. document.fonts.clear = function () {
  165. dispatchEvent(new CustomEvent(CLEAR_FONTS_EVENT));
  166. return clearFonts.call(document.fonts);
  167. };
  168. document.fonts.clear.toString = function () { return "function clear() { [native code] }"; };
  169. }
  170. async function getDetailObject(fontFamily, src, descriptors) {
  171. const detail = {};
  172. detail["font-family"] = fontFamily;
  173. detail.src = src;
  174. if (descriptors) {
  175. Object.keys(descriptors).forEach(descriptor => {
  176. if (FONT_STYLE_PROPERTIES[descriptor]) {
  177. detail[FONT_STYLE_PROPERTIES[descriptor]] = descriptors[descriptor];
  178. }
  179. });
  180. }
  181. return new Promise(resolve => {
  182. if (detail.src instanceof ArrayBuffer) {
  183. const reader = new FileReader();
  184. reader.readAsDataURL(new Blob([detail.src]));
  185. reader.addEventListener("load", () => {
  186. detail.src = "url(" + reader.result + ")";
  187. resolve(detail);
  188. });
  189. } else {
  190. resolve(detail);
  191. }
  192. });
  193. }
  194. }