fetch-resources.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 => {
  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. return {};
  37. async function onRequest(message) {
  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. }
  57. }
  58. function getResponse(xhrRequest) {
  59. return { array: Array.from(new Uint8Array(xhrRequest.response)) };
  60. }
  61. async function fetchResource(url) {
  62. return new Promise((resolve, reject) => {
  63. const xhrRequest = new XMLHttpRequest();
  64. xhrRequest.withCredentials = true;
  65. xhrRequest.responseType = "arraybuffer";
  66. xhrRequest.onerror = event => reject(new Error(event.detail));
  67. xhrRequest.onreadystatechange = () => {
  68. if (xhrRequest.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
  69. const headers = {};
  70. headers["content-type"] = xhrRequest.getResponseHeader("Content-Type");
  71. resolve({ xhrRequest, headers, status: xhrRequest.status });
  72. }
  73. };
  74. xhrRequest.open("GET", url, true);
  75. xhrRequest.send();
  76. });
  77. }
  78. })();