download.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global browser, singlefile, Blob, URL, document */
  21. singlefile.download = (() => {
  22. const partialContents = new Map();
  23. return {
  24. onMessage,
  25. downloadPage
  26. };
  27. async function onMessage(message, sender) {
  28. if (message.method.endsWith(".download")) {
  29. if (message.truncated) {
  30. let partialContent = partialContents.get(sender.tab.id);
  31. if (!partialContent) {
  32. partialContent = [];
  33. partialContents.set(sender.tab.id, partialContent);
  34. }
  35. partialContent.push(message.content);
  36. if (message.finished) {
  37. partialContents.delete(sender.tab.id);
  38. if (message.saveToClipboard) {
  39. message.content = partialContent.join("");
  40. } else {
  41. message.url = URL.createObjectURL(new Blob(partialContent, { type: "text/html" }));
  42. }
  43. } else {
  44. return {};
  45. }
  46. } else if (message.content && !message.saveToClipboard) {
  47. message.url = URL.createObjectURL(new Blob([message.content], { type: "text/html" }));
  48. }
  49. if (message.saveToClipboard) {
  50. saveToClipboard(message);
  51. } else {
  52. try {
  53. const tab = sender.tab;
  54. return await downloadPage(message, { confirmFilename: message.confirmFilename, incognito: tab.incognito, filenameConflictAction: message.filenameConflictAction });
  55. } catch (error) {
  56. console.error(error); // eslint-disable-line no-console
  57. singlefile.ui.onError(sender.tab.id, {});
  58. return {};
  59. }
  60. }
  61. }
  62. }
  63. async function downloadPage(page, options) {
  64. const downloadInfo = {
  65. url: page.url,
  66. saveAs: options.confirmFilename,
  67. filename: page.filename,
  68. conflictAction: options.filenameConflictAction
  69. };
  70. if (options.incognito) {
  71. downloadInfo.incognito = true;
  72. }
  73. let downloadId;
  74. try {
  75. downloadId = await browser.downloads.download(downloadInfo);
  76. } catch (error) {
  77. if (error.message) {
  78. const errorMessage = error.message.toLowerCase();
  79. const invalidFilename = errorMessage.includes("illegal characters") || errorMessage.includes("invalid filename");
  80. if (invalidFilename && page.filename.startsWith(".")) {
  81. page.filename = "_" + page.filename;
  82. return downloadPage(page, { confirmFilename: options.confirmFilename, incognito: options.incognito, filenameConflictAction: options.filenameConflictAction });
  83. } else if (invalidFilename && page.filename.includes(",")) {
  84. page.filename = page.filename.replace(/,/g, "_");
  85. return downloadPage(page, { confirmFilename: options.confirmFilename, incognito: options.incognito, filenameConflictAction: options.filenameConflictAction });
  86. } else if ((errorMessage.includes("'incognito'") || errorMessage.includes("\"incognito\"")) && options.incognito) {
  87. return downloadPage(page, { confirmFilename: options.confirmFilename, filenameConflictAction: options.filenameConflictAction });
  88. } else if (errorMessage == "conflictAction prompt not yet implemented" && options.filenameConflictAction) {
  89. return downloadPage(page, { confirmFilename: options.confirmFilename });
  90. } else if (!errorMessage.includes("canceled")) {
  91. throw error;
  92. }
  93. } else {
  94. throw error;
  95. }
  96. }
  97. return new Promise((resolve, reject) => {
  98. browser.downloads.onChanged.addListener(onChanged);
  99. function onChanged(event) {
  100. if (event.id == downloadId && event.state) {
  101. if (event.state.current == "complete") {
  102. URL.revokeObjectURL(page.url);
  103. resolve({});
  104. browser.downloads.onChanged.removeListener(onChanged);
  105. }
  106. if (event.state.current == "interrupted") {
  107. URL.revokeObjectURL(page.url);
  108. if (event.error && event.error.current == "USER_CANCELED") {
  109. resolve({});
  110. } else {
  111. reject(new Error(event.state.current));
  112. }
  113. browser.downloads.onChanged.removeListener(onChanged);
  114. }
  115. }
  116. }
  117. });
  118. }
  119. function saveToClipboard(page) {
  120. const command = "copy";
  121. document.addEventListener(command, listener);
  122. document.execCommand(command);
  123. document.removeEventListener(command, listener);
  124. function listener(event) {
  125. event.clipboardData.setData("text/html", page.content);
  126. event.clipboardData.setData("text/plain", page.content);
  127. event.preventDefault();
  128. }
  129. }
  130. })();