download.js 5.3 KB

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