content-hooks-frames.js 7.8 KB

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