download.js 5.0 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. * 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 ((errorMessage.includes("'incognito'") || errorMessage.includes("\"incognito\"")) && options.incognito) {
  90. return downloadPage(page, { confirmFilename: options.confirmFilename, filenameConflictAction: options.filenameConflictAction });
  91. } else if (errorMessage == "conflictaction prompt not yet implemented" && options.filenameConflictAction) {
  92. return downloadPage(page, { confirmFilename: options.confirmFilename });
  93. } else if (!errorMessage.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. })();