download.js 5.3 KB

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