single-file-extension-core.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.extension = {}));
  5. })(this, (function (exports) { 'use strict';
  6. /*
  7. * Copyright 2010-2020 Gildas Lormeau
  8. * contact : gildas.lormeau <at> gmail.com
  9. *
  10. * This file is part of SingleFile.
  11. *
  12. * The code in this file is free software: you can redistribute it and/or
  13. * modify it under the terms of the GNU Affero General Public License
  14. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  15. * of the License, or (at your option) any later version.
  16. *
  17. * The code in this file is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  20. * General Public License for more details.
  21. *
  22. * As additional permission under GNU AGPL version 3 section 7, you may
  23. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  24. * AGPL normally required by section 4, provided you include this license
  25. * notice and a URL through which recipients can access the Corresponding
  26. * Source.
  27. */
  28. /* global browser, fetch, TextDecoder */
  29. let contentScript, frameScript;
  30. const contentScriptFiles = [
  31. "lib/chrome-browser-polyfill.js",
  32. "lib/single-file.js"
  33. ];
  34. const frameScriptFiles = [
  35. "lib/chrome-browser-polyfill.js",
  36. "lib/single-file-frames.js"
  37. ];
  38. const basePath = "../../../";
  39. async function inject(tabId, options) {
  40. await initScripts(options);
  41. let scriptsInjected;
  42. if (!options.removeFrames) {
  43. try {
  44. await browser.tabs.executeScript(tabId, { code: frameScript, allFrames: true, matchAboutBlank: true, runAt: "document_start" });
  45. } catch (error) {
  46. // ignored
  47. }
  48. }
  49. try {
  50. await browser.tabs.executeScript(tabId, { code: contentScript, allFrames: false, runAt: "document_idle" });
  51. scriptsInjected = true;
  52. } catch (error) {
  53. // ignored
  54. }
  55. if (scriptsInjected) {
  56. if (options.frameId) {
  57. await browser.tabs.executeScript(tabId, { code: "document.documentElement.dataset.requestedFrameId = true", frameId: options.frameId, matchAboutBlank: true, runAt: "document_start" });
  58. }
  59. }
  60. return scriptsInjected;
  61. }
  62. async function initScripts(options) {
  63. const extensionScriptFiles = options.extensionScriptFiles || [];
  64. if (!contentScript && !frameScript) {
  65. [contentScript, frameScript] = await Promise.all([
  66. getScript(contentScriptFiles.concat(extensionScriptFiles)),
  67. getScript(frameScriptFiles)
  68. ]);
  69. }
  70. }
  71. async function getScript(scriptFiles) {
  72. const scriptsPromises = scriptFiles.map(async scriptFile => {
  73. if (typeof scriptFile == "function") {
  74. return "(" + scriptFile.toString() + ")();";
  75. } else {
  76. const scriptResource = await fetch(browser.runtime.getURL(basePath + scriptFile));
  77. return new TextDecoder().decode(await scriptResource.arrayBuffer());
  78. }
  79. });
  80. let content = "";
  81. for (const scriptPromise of scriptsPromises) {
  82. content += await scriptPromise;
  83. }
  84. return content;
  85. }
  86. /*
  87. * Copyright 2010-2020 Gildas Lormeau
  88. * contact : gildas.lormeau <at> gmail.com
  89. *
  90. * This file is part of SingleFile.
  91. *
  92. * The code in this file is free software: you can redistribute it and/or
  93. * modify it under the terms of the GNU Affero General Public License
  94. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  95. * of the License, or (at your option) any later version.
  96. *
  97. * The code in this file is distributed in the hope that it will be useful,
  98. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  99. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  100. * General Public License for more details.
  101. *
  102. * As additional permission under GNU AGPL version 3 section 7, you may
  103. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  104. * AGPL normally required by section 4, provided you include this license
  105. * notice and a URL through which recipients can access the Corresponding
  106. * Source.
  107. */
  108. /* global browser, window, CustomEvent, setTimeout, clearTimeout */
  109. const FETCH_REQUEST_EVENT = "single-file-request-fetch";
  110. const FETCH_ACK_EVENT = "single-file-ack-fetch";
  111. const FETCH_RESPONSE_EVENT = "single-file-response-fetch";
  112. const ERR_HOST_FETCH = "Host fetch error (SingleFile)";
  113. const HOST_FETCH_MAX_DELAY = 2500;
  114. const USE_HOST_FETCH = Boolean(window.wrappedJSObject);
  115. const addEventListener = (type, listener, options) => window.addEventListener(type, listener, options);
  116. const dispatchEvent = event => window.dispatchEvent(event);
  117. const removeEventListener = (type, listener, options) => window.removeEventListener(type, listener, options);
  118. const fetch$1 = (url, options) => window.fetch(url, options);
  119. let requestId = 0, pendingResponses = new Map();
  120. browser.runtime.onMessage.addListener(message => {
  121. if (message.method == "singlefile.fetchFrame" && window.frameId && window.frameId == message.frameId) {
  122. return onFetchFrame(message);
  123. }
  124. if (message.method == "singlefile.fetchResponse") {
  125. return onFetchResponse(message);
  126. }
  127. });
  128. async function onFetchFrame(message) {
  129. try {
  130. const response = await fetch$1(message.url, { cache: "force-cache", headers: message.headers });
  131. return {
  132. status: response.status,
  133. headers: [...response.headers],
  134. array: Array.from(new Uint8Array(await response.arrayBuffer()))
  135. };
  136. } catch (error) {
  137. return {
  138. error: error && error.toString()
  139. };
  140. }
  141. }
  142. async function onFetchResponse(message) {
  143. const pendingResponse = pendingResponses.get(message.requestId);
  144. if (pendingResponse) {
  145. if (message.error) {
  146. pendingResponse.reject(new Error(message.error));
  147. pendingResponses.delete(message.requestId);
  148. } else {
  149. if (message.truncated) {
  150. if (pendingResponse.array) {
  151. pendingResponse.array = pendingResponse.array.concat(message.array);
  152. } else {
  153. pendingResponse.array = message.array;
  154. pendingResponses.set(message.requestId, pendingResponse);
  155. }
  156. if (message.finished) {
  157. message.array = pendingResponse.array;
  158. }
  159. }
  160. if (!message.truncated || message.finished) {
  161. pendingResponse.resolve({
  162. status: message.status,
  163. headers: { get: headerName => message.headers && message.headers[headerName] },
  164. arrayBuffer: async () => new Uint8Array(message.array).buffer
  165. });
  166. pendingResponses.delete(message.requestId);
  167. }
  168. }
  169. }
  170. return {};
  171. }
  172. async function hostFetch(url, options) {
  173. const result = new Promise((resolve, reject) => {
  174. dispatchEvent(new CustomEvent(FETCH_REQUEST_EVENT, { detail: JSON.stringify({ url, options }) }));
  175. addEventListener(FETCH_ACK_EVENT, onAckFetch, false);
  176. addEventListener(FETCH_RESPONSE_EVENT, onResponseFetch, false);
  177. const timeout = setTimeout(() => {
  178. removeListeners();
  179. reject(new Error(ERR_HOST_FETCH));
  180. }, HOST_FETCH_MAX_DELAY);
  181. function onResponseFetch(event) {
  182. if (event.detail) {
  183. if (event.detail.url == url) {
  184. removeListeners();
  185. if (event.detail.response) {
  186. resolve({
  187. status: event.detail.status,
  188. headers: new Map(event.detail.headers),
  189. arrayBuffer: async () => event.detail.response
  190. });
  191. } else {
  192. reject(event.detail.error);
  193. }
  194. }
  195. } else {
  196. reject();
  197. }
  198. }
  199. function onAckFetch() {
  200. clearTimeout(timeout);
  201. }
  202. function removeListeners() {
  203. removeEventListener(FETCH_RESPONSE_EVENT, onResponseFetch, false);
  204. removeEventListener(FETCH_ACK_EVENT, onAckFetch, false);
  205. }
  206. });
  207. try {
  208. return await result;
  209. } catch (error) {
  210. if (error && error.message == ERR_HOST_FETCH) {
  211. return fetch$1(url, options);
  212. } else {
  213. throw error;
  214. }
  215. }
  216. }
  217. async function fetchResource(url, options = {}) {
  218. try {
  219. const fetchOptions = { cache: "force-cache", headers: options.headers };
  220. return await (options.referrer && USE_HOST_FETCH ? hostFetch(url, fetchOptions) : fetch$1(url, fetchOptions));
  221. }
  222. catch (error) {
  223. requestId++;
  224. const promise = new Promise((resolve, reject) => pendingResponses.set(requestId, { resolve, reject }));
  225. await sendMessage({ method: "singlefile.fetch", url, requestId, referrer: options.referrer, headers: options.headers });
  226. return promise;
  227. }
  228. }
  229. async function frameFetch(url, options) {
  230. const response = await sendMessage({ method: "singlefile.fetchFrame", url, frameId: options.frameId, referrer: options.referrer, headers: options.headers });
  231. return {
  232. status: response.status,
  233. headers: new Map(response.headers),
  234. arrayBuffer: async () => new Uint8Array(response.array).buffer
  235. };
  236. }
  237. async function sendMessage(message) {
  238. const response = await browser.runtime.sendMessage(message);
  239. if (!response || response.error) {
  240. throw new Error(response && response.error && response.error.toString());
  241. } else {
  242. return response;
  243. }
  244. }
  245. /*
  246. * Copyright 2010-2020 Gildas Lormeau
  247. * contact : gildas.lormeau <at> gmail.com
  248. *
  249. * This file is part of SingleFile.
  250. *
  251. * The code in this file is free software: you can redistribute it and/or
  252. * modify it under the terms of the GNU Affero General Public License
  253. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  254. * of the License, or (at your option) any later version.
  255. *
  256. * The code in this file is distributed in the hope that it will be useful,
  257. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  258. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  259. * General Public License for more details.
  260. *
  261. * As additional permission under GNU AGPL version 3 section 7, you may
  262. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  263. * AGPL normally required by section 4, provided you include this license
  264. * notice and a URL through which recipients can access the Corresponding
  265. * Source.
  266. */
  267. function injectScript(tabId, options) {
  268. return inject(tabId, options);
  269. }
  270. function getPageData(options, doc, win, initOptions = { fetch: fetchResource, frameFetch }) {
  271. return globalThis.singlefile.getPageData(options, initOptions, doc, win);
  272. }
  273. exports.getPageData = getPageData;
  274. exports.injectScript = injectScript;
  275. Object.defineProperty(exports, '__esModule', { value: true });
  276. }));