fetch.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. (() => {
  25. const MAX_CONTENT_SIZE = 8 * (1024 * 1024);
  26. const pendingRequests = [];
  27. browser.runtime.onMessage.addListener((message, sender) => {
  28. if (message.method == "singlefile.fetchRequest"
  29. || message.method == "singlefile.fetchFrameRequest"
  30. || message.method == "singlefile.bgFetchFrameResponse") {
  31. return onMessage(message, sender);
  32. }
  33. });
  34. async function onMessage(message, sender) {
  35. if (message.method == "singlefile.fetchRequest") {
  36. return onRequest(message, sender);
  37. } else if (message.method == "singlefile.fetchFrameRequest") {
  38. message.bgId = pendingRequests.length;
  39. browser.tabs.sendMessage(sender.tab.id, message);
  40. pendingRequests.push({ tabId: sender.tab.id });
  41. } else if (message.method == "singlefile.bgFetchFrameResponse") {
  42. onFrameResponse(message);
  43. }
  44. }
  45. async function onRequest(message, sender) {
  46. if (message.method == "singlefile.fetchRequest") {
  47. try {
  48. const response = await fetchResource(message.url);
  49. const id = message.id;
  50. for (let blockIndex = 0; blockIndex * MAX_CONTENT_SIZE < response.array.length; blockIndex++) {
  51. const message = {
  52. method: "singlefile.fetchResponse",
  53. id
  54. };
  55. message.truncated = response.array.length > MAX_CONTENT_SIZE;
  56. if (message.truncated) {
  57. message.finished = (blockIndex + 1) * MAX_CONTENT_SIZE > response.array.length;
  58. message.array = Array.from(response.array.slice(blockIndex * MAX_CONTENT_SIZE, (blockIndex + 1) * MAX_CONTENT_SIZE));
  59. } else {
  60. message.array = Array.from(response.array);
  61. }
  62. if (!message.truncated || message.finished) {
  63. message.headers = response.headers;
  64. message.status = response.status;
  65. }
  66. browser.tabs.sendMessage(sender.tab.id, message);
  67. }
  68. } catch (error) {
  69. await browser.tabs.sendMessage(sender.tab.id, {
  70. method: "singlefile.fetchResponse",
  71. id: message.id,
  72. error: error.toString()
  73. });
  74. }
  75. }
  76. }
  77. function onFrameResponse(message) {
  78. const pendingRequest = pendingRequests[message.bgId];
  79. if (pendingRequest) {
  80. if (message.error) {
  81. browser.tabs.sendMessage(pendingRequest.tabId, message);
  82. pendingRequests[message.bgId] = null;
  83. } else {
  84. message.method = "singlefile.fetchFrameResponse";
  85. browser.tabs.sendMessage(pendingRequest.tabId, message);
  86. if (!message.truncated || message.finished) {
  87. pendingRequests[message.bgId] = null;
  88. }
  89. }
  90. }
  91. }
  92. function fetchResource(url) {
  93. return new Promise((resolve, reject) => {
  94. const xhrRequest = new XMLHttpRequest();
  95. xhrRequest.withCredentials = true;
  96. xhrRequest.responseType = "arraybuffer";
  97. xhrRequest.onerror = event => reject(new Error(event.detail));
  98. xhrRequest.onreadystatechange = () => {
  99. if (xhrRequest.readyState == XMLHttpRequest.DONE) {
  100. if (xhrRequest.status || xhrRequest.response.byteLength) {
  101. resolve({
  102. array: new Uint8Array(xhrRequest.response),
  103. headers: { "content-type": xhrRequest.getResponseHeader("Content-Type") },
  104. status: xhrRequest.status
  105. });
  106. } else {
  107. reject();
  108. }
  109. }
  110. };
  111. xhrRequest.open("GET", url, true);
  112. xhrRequest.send();
  113. });
  114. }
  115. })();