single-file-extension-frames.js 9.2 KB

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