fetch.js 2.9 KB

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