1
0

download.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.truncated) {
  29. let partialContent = partialContents.get(sender.tab.id);
  30. if (!partialContent) {
  31. partialContent = [];
  32. partialContents.set(sender.tab.id, partialContent);
  33. }
  34. partialContent.push(message.content);
  35. if (message.finished) {
  36. partialContents.delete(sender.tab.id);
  37. if (message.saveToClipboard) {
  38. message.content = partialContent.join("");
  39. } else {
  40. message.url = URL.createObjectURL(new Blob(partialContent, { type: "text/html" }));
  41. }
  42. } else {
  43. return Promise.resolve({});
  44. }
  45. } else if (message.content && !message.saveToClipboard) {
  46. message.url = URL.createObjectURL(new Blob([message.content], { type: "text/html" }));
  47. }
  48. if (message.saveToClipboard) {
  49. saveToClipboard(message);
  50. } else {
  51. return downloadPage(message, { confirmFilename: message.confirmFilename, incognito: sender.tab.incognito, filenameConflictAction: message.filenameConflictAction })
  52. .catch(error => {
  53. if (error.message) {
  54. if (error.message.includes("'incognito'")) {
  55. return downloadPage(message, { confirmFilename: message.confirmFilename, filenameConflictAction: message.filenameConflictAction });
  56. } else if (error.message == "conflictAction prompt not yet implemented") {
  57. return downloadPage(message, { confirmFilename: message.confirmFilename });
  58. } else if (error.message.includes("illegal characters")) {
  59. message.filename = message.filename.replace(/,/g, "_");
  60. return downloadPage(message, { confirmFilename: message.confirmFilename, incognito: sender.tab.incognito, filenameConflictAction: message.filenameConflictAction });
  61. } else {
  62. throw error;
  63. }
  64. } else {
  65. throw error;
  66. }
  67. });
  68. }
  69. }
  70. async function downloadPage(page, options) {
  71. const downloadInfo = {
  72. url: page.url,
  73. saveAs: options.confirmFilename,
  74. filename: page.filename,
  75. conflictAction: options.filenameConflictAction
  76. };
  77. if (options.incognito) {
  78. downloadInfo.incognito = true;
  79. }
  80. let downloadId;
  81. try {
  82. downloadId = await browser.downloads.download(downloadInfo);
  83. } catch (error) {
  84. if (error.message) {
  85. if (!error.message.toLowerCase().includes("canceled")) {
  86. throw error;
  87. }
  88. } else {
  89. throw error;
  90. }
  91. }
  92. return new Promise((resolve, reject) => {
  93. browser.downloads.onChanged.addListener(onChanged);
  94. function onChanged(event) {
  95. if (event.id == downloadId && event.state) {
  96. if (event.state.current == "complete") {
  97. URL.revokeObjectURL(page.url);
  98. resolve({});
  99. browser.downloads.onChanged.removeListener(onChanged);
  100. }
  101. if (event.state.current == "interrupted" && (!event.error || event.error.current != "USER_CANCELED")) {
  102. URL.revokeObjectURL(page.url);
  103. reject(new Error(event.state.current));
  104. browser.downloads.onChanged.removeListener(onChanged);
  105. }
  106. }
  107. }
  108. });
  109. }
  110. function saveToClipboard(page) {
  111. const command = "copy";
  112. document.addEventListener(command, listener);
  113. document.execCommand(command);
  114. document.removeEventListener(command, listener);
  115. function listener(event) {
  116. event.clipboardData.setData("text/html", page.content);
  117. event.clipboardData.setData("text/plain", page.content);
  118. event.preventDefault();
  119. }
  120. }
  121. })();