single-file-extension-frames.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. (function () {
  2. 'use strict';
  3. /*
  4. * Copyright 2010-2020 Gildas Lormeau
  5. * contact : gildas.lormeau <at> gmail.com
  6. *
  7. * This file is part of SingleFile.
  8. *
  9. * The code in this file is free software: you can redistribute it and/or
  10. * modify it under the terms of the GNU Affero General Public License
  11. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  12. * of the License, or (at your option) any later version.
  13. *
  14. * The code in this file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  17. * General Public License for more details.
  18. *
  19. * As additional permission under GNU AGPL version 3 section 7, you may
  20. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  21. * AGPL normally required by section 4, provided you include this license
  22. * notice and a URL through which recipients can access the Corresponding
  23. * Source.
  24. */
  25. /* global globalThis, window */
  26. const document$1 = globalThis.document;
  27. const Document$1 = globalThis.Document;
  28. if (document$1 instanceof Document$1) {
  29. let scriptElement = document$1.createElement("script");
  30. scriptElement.src = "data:," + "(" + injectedScript.toString() + ")()";
  31. (document$1.documentElement || document$1).appendChild(scriptElement);
  32. scriptElement.remove();
  33. scriptElement = document$1.createElement("script");
  34. scriptElement.textContent = "(" + injectedScript.toString() + ")()";
  35. (document$1.documentElement || document$1).appendChild(scriptElement);
  36. scriptElement.remove();
  37. }
  38. function injectedScript() {
  39. if (typeof globalThis == "undefined") {
  40. window.globalThis = window;
  41. }
  42. const document = globalThis.document;
  43. const CustomEvent = globalThis.CustomEvent;
  44. const FileReader = globalThis.FileReader;
  45. const Blob = globalThis.Blob;
  46. const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
  47. const DELETE_FONT_EVENT = "single-file-delete-font";
  48. const CLEAR_FONTS_EVENT = "single-file-clear-fonts";
  49. const FONT_STYLE_PROPERTIES = {
  50. family: "font-family",
  51. style: "font-style",
  52. weight: "font-weight",
  53. stretch: "font-stretch",
  54. unicodeRange: "unicode-range",
  55. variant: "font-variant",
  56. featureSettings: "font-feature-settings"
  57. };
  58. if (globalThis.FontFace) {
  59. const FontFace = globalThis.FontFace;
  60. globalThis.FontFace = function () {
  61. getDetailObject(...arguments).then(detail => document.dispatchEvent(new CustomEvent(NEW_FONT_FACE_EVENT, { detail })));
  62. return new FontFace(...arguments);
  63. };
  64. globalThis.FontFace.prototype = FontFace.prototype;
  65. globalThis.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
  66. const deleteFont = document.fonts.delete;
  67. document.fonts.delete = function (fontFace) {
  68. getDetailObject(fontFace.family).then(detail => document.dispatchEvent(new CustomEvent(DELETE_FONT_EVENT, { detail })));
  69. return deleteFont.call(document.fonts, fontFace);
  70. };
  71. document.fonts.delete.toString = function () { return "function delete() { [native code] }"; };
  72. const clearFonts = document.fonts.clear;
  73. document.fonts.clear = function () {
  74. document.dispatchEvent(new CustomEvent(CLEAR_FONTS_EVENT));
  75. return clearFonts.call(document.fonts);
  76. };
  77. document.fonts.clear.toString = function () { return "function clear() { [native code] }"; };
  78. }
  79. async function getDetailObject(fontFamily, src, descriptors) {
  80. const detail = {};
  81. detail["font-family"] = fontFamily;
  82. detail.src = src;
  83. if (descriptors) {
  84. Object.keys(descriptors).forEach(descriptor => {
  85. if (FONT_STYLE_PROPERTIES[descriptor]) {
  86. detail[FONT_STYLE_PROPERTIES[descriptor]] = descriptors[descriptor];
  87. }
  88. });
  89. }
  90. return new Promise(resolve => {
  91. if (detail.src instanceof ArrayBuffer) {
  92. const reader = new FileReader();
  93. reader.readAsDataURL(new Blob([detail.src]));
  94. reader.addEventListener("load", () => {
  95. detail.src = "url(" + reader.result + ")";
  96. resolve(detail);
  97. });
  98. } else {
  99. resolve(detail);
  100. }
  101. });
  102. }
  103. }
  104. /*
  105. * Copyright 2010-2020 Gildas Lormeau
  106. * contact : gildas.lormeau <at> gmail.com
  107. *
  108. * This file is part of SingleFile.
  109. *
  110. * The code in this file is free software: you can redistribute it and/or
  111. * modify it under the terms of the GNU Affero General Public License
  112. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  113. * of the License, or (at your option) any later version.
  114. *
  115. * The code in this file is distributed in the hope that it will be useful,
  116. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  117. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  118. * General Public License for more details.
  119. *
  120. * As additional permission under GNU AGPL version 3 section 7, you may
  121. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  122. * AGPL normally required by section 4, provided you include this license
  123. * notice and a URL through which recipients can access the Corresponding
  124. * Source.
  125. */
  126. /* global globalThis */
  127. const browser$1 = globalThis.browser;
  128. const document = globalThis.document;
  129. const Document = globalThis.Document;
  130. if (document instanceof Document && browser$1 && browser$1.runtime && browser$1.runtime.getURL) {
  131. const scriptElement = document.createElement("script");
  132. scriptElement.src = browser$1.runtime.getURL("/lib/single-file-hooks-frames.js");
  133. scriptElement.async = false;
  134. (document.documentElement || document).appendChild(scriptElement);
  135. scriptElement.remove();
  136. }
  137. /*
  138. * Copyright 2010-2020 Gildas Lormeau
  139. * contact : gildas.lormeau <at> gmail.com
  140. *
  141. * This file is part of SingleFile.
  142. *
  143. * The code in this file is free software: you can redistribute it and/or
  144. * modify it under the terms of the GNU Affero General Public License
  145. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  146. * of the License, or (at your option) any later version.
  147. *
  148. * The code in this file is distributed in the hope that it will be useful,
  149. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  150. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  151. * General Public License for more details.
  152. *
  153. * As additional permission under GNU AGPL version 3 section 7, you may
  154. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  155. * AGPL normally required by section 4, provided you include this license
  156. * notice and a URL through which recipients can access the Corresponding
  157. * Source.
  158. */
  159. const fetch = (url, options) => window.fetch(url, options);
  160. let pendingResponses = new Map();
  161. browser.runtime.onMessage.addListener(message => {
  162. if (message.method == "singlefile.fetchFrame" && window.frameId && window.frameId == message.frameId) {
  163. return onFetchFrame(message);
  164. }
  165. if (message.method == "singlefile.fetchResponse") {
  166. return onFetchResponse(message);
  167. }
  168. });
  169. async function onFetchFrame(message) {
  170. try {
  171. const response = await fetch(message.url, { cache: "force-cache", headers: message.headers });
  172. return {
  173. status: response.status,
  174. headers: [...response.headers],
  175. array: Array.from(new Uint8Array(await response.arrayBuffer()))
  176. };
  177. } catch (error) {
  178. return {
  179. error: error && error.toString()
  180. };
  181. }
  182. }
  183. async function onFetchResponse(message) {
  184. const pendingResponse = pendingResponses.get(message.requestId);
  185. if (pendingResponse) {
  186. if (message.error) {
  187. pendingResponse.reject(new Error(message.error));
  188. pendingResponses.delete(message.requestId);
  189. } else {
  190. if (message.truncated) {
  191. if (pendingResponse.array) {
  192. pendingResponse.array = pendingResponse.array.concat(message.array);
  193. } else {
  194. pendingResponse.array = message.array;
  195. pendingResponses.set(message.requestId, pendingResponse);
  196. }
  197. if (message.finished) {
  198. message.array = pendingResponse.array;
  199. }
  200. }
  201. if (!message.truncated || message.finished) {
  202. pendingResponse.resolve({
  203. status: message.status,
  204. headers: { get: headerName => message.headers && message.headers[headerName] },
  205. arrayBuffer: async () => new Uint8Array(message.array).buffer
  206. });
  207. pendingResponses.delete(message.requestId);
  208. }
  209. }
  210. }
  211. return {};
  212. }
  213. })();