1
0

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. } finally {
  63. URL.revokeObjectURL(message.url);
  64. }
  65. }
  66. }
  67. }
  68. async function downloadPage(page, options) {
  69. const downloadInfo = {
  70. url: page.url,
  71. saveAs: options.confirmFilename,
  72. filename: page.filename,
  73. conflictAction: options.filenameConflictAction
  74. };
  75. if (options.incognito) {
  76. downloadInfo.incognito = true;
  77. }
  78. let downloadId;
  79. try {
  80. downloadId = await browser.downloads.download(downloadInfo);
  81. } catch (error) {
  82. if (error.message) {
  83. const errorMessage = error.message.toLowerCase();
  84. const invalidFilename = errorMessage.includes("illegal characters") || errorMessage.includes("invalid filename");
  85. if (invalidFilename && page.filename.startsWith(".")) {
  86. page.filename = "_" + page.filename;
  87. return downloadPage(page, { confirmFilename: options.confirmFilename, incognito: options.incognito, filenameConflictAction: options.filenameConflictAction });
  88. } else if (invalidFilename && page.filename.includes(",")) {
  89. page.filename = page.filename.replace(/,/g, "_");
  90. return downloadPage(page, { confirmFilename: options.confirmFilename, incognito: options.incognito, filenameConflictAction: options.filenameConflictAction });
  91. } else if (invalidFilename && !page.filename.match(/^[\x00-\x7F]+$/)) { // eslint-disable-line no-control-regex
  92. page.filename = page.filename.replace(/[^\x00-\x7F]+/g, "_"); // eslint-disable-line no-control-regex
  93. } else if ((errorMessage.includes("'incognito'") || errorMessage.includes("\"incognito\"")) && options.incognito) {
  94. return downloadPage(page, { confirmFilename: options.confirmFilename, filenameConflictAction: options.filenameConflictAction });
  95. } else if (errorMessage == "conflictaction prompt not yet implemented" && options.filenameConflictAction) {
  96. return downloadPage(page, { confirmFilename: options.confirmFilename });
  97. } else if (errorMessage.includes("canceled")) {
  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. resolve({});
  112. browser.downloads.onChanged.removeListener(onChanged);
  113. }
  114. if (event.state.current == "interrupted") {
  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. })();