download.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if (errorMessage.includes("invalid filename") && page.filename.startsWith(".")) {
  80. page.filename = "_" + page.filename;
  81. return downloadPage(page, { confirmFilename: options.confirmFilename, incognito: options.incognito, filenameConflictAction: options.filenameConflictAction });
  82. } else if (errorMessage.includes("illegal characters") && page.filename.includes(",")) {
  83. page.filename = page.filename.replace(/,/g, "_");
  84. return downloadPage(page, { confirmFilename: options.confirmFilename, incognito: options.incognito, filenameConflictAction: options.filenameConflictAction });
  85. } else if ((errorMessage.includes("'incognito'") || errorMessage.includes("\"incognito\"")) && options.incognito) {
  86. return downloadPage(page, { confirmFilename: options.confirmFilename, filenameConflictAction: options.filenameConflictAction });
  87. } else if (errorMessage == "conflictAction prompt not yet implemented" && options.filenameConflictAction) {
  88. return downloadPage(page, { confirmFilename: options.confirmFilename });
  89. } else if (!errorMessage.includes("canceled")) {
  90. throw error;
  91. }
  92. } else {
  93. throw error;
  94. }
  95. }
  96. return new Promise((resolve, reject) => {
  97. browser.downloads.onChanged.addListener(onChanged);
  98. function onChanged(event) {
  99. if (event.id == downloadId && event.state) {
  100. if (event.state.current == "complete") {
  101. URL.revokeObjectURL(page.url);
  102. resolve({});
  103. browser.downloads.onChanged.removeListener(onChanged);
  104. }
  105. if (event.state.current == "interrupted") {
  106. URL.revokeObjectURL(page.url);
  107. if (event.error && event.error.current == "USER_CANCELED") {
  108. resolve({});
  109. } else {
  110. reject(new Error(event.state.current));
  111. }
  112. browser.downloads.onChanged.removeListener(onChanged);
  113. }
  114. }
  115. }
  116. });
  117. }
  118. function saveToClipboard(page) {
  119. const command = "copy";
  120. document.addEventListener(command, listener);
  121. document.execCommand(command);
  122. document.removeEventListener(command, listener);
  123. function listener(event) {
  124. event.clipboardData.setData("text/html", page.content);
  125. event.clipboardData.setData("text/plain", page.content);
  126. event.preventDefault();
  127. }
  128. }
  129. })();