fetch-resources.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2010-2019 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 singlefile, browser, XMLHttpRequest */
  24. singlefile.extension.lib.fetch.bg.resources = (() => {
  25. const responses = new Map();
  26. let requestId = 1;
  27. browser.runtime.onMessage.addListener((message, sender) => {
  28. if (message.method.startsWith("fetch")) {
  29. return new Promise(resolve => {
  30. onRequest(message, sender)
  31. .then(resolve)
  32. .catch(error => resolve({ error: error.toString() }));
  33. });
  34. }
  35. });
  36. return {};
  37. async function onRequest(message, sender) {
  38. if (message.method == "fetch") {
  39. const responseId = requestId;
  40. requestId = requestId + 1;
  41. const response = await fetchResource(message.url);
  42. responses.set(responseId, response);
  43. response.responseId = responseId;
  44. return { responseId, headers: response.headers };
  45. } else if (message.method == "fetch.array") {
  46. const response = responses.get(message.requestId);
  47. responses.delete(response.requestId);
  48. return new Promise((resolve, reject) => {
  49. response.xhrRequest.onerror = event => reject(new Error(event.detail));
  50. if (response.xhrRequest.readyState == XMLHttpRequest.DONE) {
  51. resolve(getResponse(response.xhrRequest));
  52. } else {
  53. response.xhrRequest.onload = () => resolve(getResponse(response.xhrRequest));
  54. }
  55. });
  56. } else if (message.method == "fetch.frame") {
  57. const response = await browser.tabs.sendMessage(sender.tab.id, message);
  58. if (response.error) {
  59. throw response.error;
  60. } else {
  61. return response;
  62. }
  63. }
  64. }
  65. function getResponse(xhrRequest) {
  66. return { array: Array.from(new Uint8Array(xhrRequest.response)) };
  67. }
  68. async function fetchResource(url) {
  69. return new Promise((resolve, reject) => {
  70. const xhrRequest = new XMLHttpRequest();
  71. xhrRequest.withCredentials = true;
  72. xhrRequest.responseType = "arraybuffer";
  73. xhrRequest.onerror = event => reject(new Error(event.detail));
  74. xhrRequest.onreadystatechange = () => {
  75. if (xhrRequest.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
  76. const headers = {};
  77. headers["content-type"] = xhrRequest.getResponseHeader("Content-Type");
  78. resolve({ xhrRequest, headers, status: xhrRequest.status });
  79. }
  80. };
  81. xhrRequest.open("GET", url, true);
  82. xhrRequest.send();
  83. });
  84. }
  85. })();