download.js 4.3 KB

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