1
0

download.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. 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 Promise.resolve({});
  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. return downloadPage(message, { confirmFilename: message.confirmFilename, incognito: sender.tab.incognito, filenameConflictAction: message.filenameConflictAction })
  53. .catch(error => {
  54. if (error.message) {
  55. const errorMessage = error.message.toLowerCase();
  56. if (errorMessage.includes("'incognito'") || errorMessage.includes("\"incognito\"")) {
  57. return downloadPage(message, { confirmFilename: message.confirmFilename, filenameConflictAction: message.filenameConflictAction });
  58. } else if (errorMessage == "conflictAction prompt not yet implemented") {
  59. return downloadPage(message, { confirmFilename: message.confirmFilename });
  60. } else if (errorMessage.includes("illegal characters") || errorMessage.includes("invalid filename")) {
  61. if (message.filename.includes(",")) {
  62. message.filename = message.filename.replace(/,/g, "_");
  63. }
  64. if (message.filename.startsWith(".")) {
  65. message.filename = "_" + message.filename;
  66. }
  67. return downloadPage(message, { confirmFilename: message.confirmFilename, incognito: sender.tab.incognito, filenameConflictAction: message.filenameConflictAction });
  68. } else {
  69. throw error;
  70. }
  71. } else {
  72. throw error;
  73. }
  74. });
  75. }
  76. }
  77. }
  78. async function downloadPage(page, options) {
  79. const downloadInfo = {
  80. url: page.url,
  81. saveAs: options.confirmFilename,
  82. filename: page.filename,
  83. conflictAction: options.filenameConflictAction
  84. };
  85. if (options.incognito) {
  86. downloadInfo.incognito = true;
  87. }
  88. let downloadId;
  89. try {
  90. downloadId = await browser.downloads.download(downloadInfo);
  91. } catch (error) {
  92. if (error.message) {
  93. if (!error.message.toLowerCase().includes("canceled")) {
  94. throw error;
  95. }
  96. } else {
  97. throw error;
  98. }
  99. }
  100. return new Promise((resolve, reject) => {
  101. browser.downloads.onChanged.addListener(onChanged);
  102. function onChanged(event) {
  103. if (event.id == downloadId && event.state) {
  104. if (event.state.current == "complete") {
  105. URL.revokeObjectURL(page.url);
  106. resolve({});
  107. browser.downloads.onChanged.removeListener(onChanged);
  108. }
  109. if (event.state.current == "interrupted") {
  110. URL.revokeObjectURL(page.url);
  111. if (event.error && event.error.current == "USER_CANCELED") {
  112. resolve({});
  113. } else {
  114. reject(new Error(event.state.current));
  115. }
  116. browser.downloads.onChanged.removeListener(onChanged);
  117. }
  118. }
  119. }
  120. });
  121. }
  122. function saveToClipboard(page) {
  123. const command = "copy";
  124. document.addEventListener(command, listener);
  125. document.execCommand(command);
  126. document.removeEventListener(command, listener);
  127. function listener(event) {
  128. event.clipboardData.setData("text/html", page.content);
  129. event.clipboardData.setData("text/plain", page.content);
  130. event.preventDefault();
  131. }
  132. }
  133. })();