fetch.js 3.9 KB

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