fetch.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2010-2020 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. const referrers = new Map();
  25. const REQUEST_ID_HEADER_NAME = "x-single-file-request-id";
  26. export {
  27. REQUEST_ID_HEADER_NAME,
  28. referrers
  29. };
  30. browser.runtime.onMessage.addListener((message, sender) => {
  31. if (message.method && message.method.startsWith("singlefile.fetch")) {
  32. return new Promise(resolve => {
  33. onRequest(message, sender)
  34. .then(resolve)
  35. .catch(error => resolve({ error: error && error.toString() }));
  36. });
  37. }
  38. });
  39. function onRequest(message, sender) {
  40. if (message.method == "singlefile.fetch") {
  41. return fetchResource(message.url, { referrer: message.referrer, headers: message.headers });
  42. } else if (message.method == "singlefile.fetchFrame") {
  43. return browser.tabs.sendMessage(sender.tab.id, message);
  44. }
  45. }
  46. function fetchResource(url, options = {}, includeRequestId) {
  47. return new Promise((resolve, reject) => {
  48. const xhrRequest = new XMLHttpRequest();
  49. xhrRequest.withCredentials = true;
  50. xhrRequest.responseType = "arraybuffer";
  51. xhrRequest.onerror = event => reject(new Error(event.detail));
  52. xhrRequest.onreadystatechange = () => {
  53. if (xhrRequest.readyState == XMLHttpRequest.DONE) {
  54. if (xhrRequest.status || xhrRequest.response.byteLength) {
  55. if ((xhrRequest.status == 401 || xhrRequest.status == 403 || xhrRequest.status == 404) && !includeRequestId) {
  56. fetchResource(url, options, true)
  57. .then(resolve)
  58. .catch(reject);
  59. } else {
  60. resolve({
  61. array: Array.from(new Uint8Array(xhrRequest.response)),
  62. headers: { "content-type": xhrRequest.getResponseHeader("Content-Type") },
  63. status: xhrRequest.status
  64. });
  65. }
  66. } else {
  67. reject();
  68. }
  69. }
  70. };
  71. xhrRequest.open("GET", url, true);
  72. if (options.headers) {
  73. for (const entry of Object.entries(options.headers)) {
  74. xhrRequest.setRequestHeader(entry[0], entry[1]);
  75. }
  76. }
  77. if (includeRequestId) {
  78. const randomId = String(Math.random()).substring(2);
  79. setReferrer(randomId, options.referrer);
  80. xhrRequest.setRequestHeader(REQUEST_ID_HEADER_NAME, randomId);
  81. }
  82. xhrRequest.send();
  83. });
  84. }
  85. function setReferrer(requestId, referrer) {
  86. referrers.set(requestId, referrer);
  87. }