download.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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]+$/)) {
  90. page.filename = page.filename.replace(/[^\x00-\x7F]/g, "_");
  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. throw error;
  97. }
  98. } else {
  99. throw error;
  100. }
  101. }
  102. return new Promise((resolve, reject) => {
  103. browser.downloads.onChanged.addListener(onChanged);
  104. function onChanged(event) {
  105. if (event.id == downloadId && event.state) {
  106. if (event.state.current == "complete") {
  107. URL.revokeObjectURL(page.url);
  108. resolve({});
  109. browser.downloads.onChanged.removeListener(onChanged);
  110. }
  111. if (event.state.current == "interrupted") {
  112. URL.revokeObjectURL(page.url);
  113. if (event.error && event.error.current == "USER_CANCELED") {
  114. resolve({});
  115. } else {
  116. reject(new Error(event.state.current));
  117. }
  118. browser.downloads.onChanged.removeListener(onChanged);
  119. }
  120. }
  121. }
  122. });
  123. }
  124. function saveToClipboard(page) {
  125. const command = "copy";
  126. document.addEventListener(command, listener);
  127. document.execCommand(command);
  128. document.removeEventListener(command, listener);
  129. function listener(event) {
  130. event.clipboardData.setData("text/html", page.content);
  131. event.clipboardData.setData("text/plain", page.content);
  132. event.preventDefault();
  133. }
  134. }
  135. })();