download.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global browser, singlefile, Blob, URL */
  21. singlefile.download = (() => {
  22. const partialContents = new Map();
  23. return {
  24. onMessage,
  25. downloadPage
  26. };
  27. function onMessage(message, sender) {
  28. if (message.truncated) {
  29. let partialContent = partialContents.get(sender.tab.id);
  30. if (!partialContent) {
  31. partialContent = [];
  32. partialContents.set(sender.tab.id, partialContent);
  33. }
  34. partialContent.push(message.content);
  35. if (message.finished) {
  36. partialContents.delete(sender.tab.id);
  37. message.url = URL.createObjectURL(new Blob(partialContent, { type: "text/html" }));
  38. } else {
  39. return Promise.resolve({});
  40. }
  41. } else if (message.content) {
  42. message.url = URL.createObjectURL(new Blob([message.content], { type: "text/html" }));
  43. }
  44. return downloadPage(message, { confirmFilename: message.confirmFilename, incognito: sender.tab.incognito, filenameConflictAction: message.filenameConflictAction })
  45. .catch(error => {
  46. if (error.message) {
  47. if (error.message.includes("'incognito'")) {
  48. return downloadPage(message, { confirmFilename: message.confirmFilename, filenameConflictAction: message.filenameConflictAction });
  49. } else if (error.message == "conflictAction prompt not yet implemented") {
  50. return downloadPage(message, { confirmFilename: message.confirmFilename });
  51. } else if (error.message.includes("illegal characters")) {
  52. message.filename = message.filename.replace(/,/g, "_");
  53. return downloadPage(message, { confirmFilename: message.confirmFilename, incognito: sender.tab.incognito, filenameConflictAction: message.filenameConflictAction });
  54. } else {
  55. throw error;
  56. }
  57. } else {
  58. throw error;
  59. }
  60. });
  61. }
  62. async function downloadPage(page, options) {
  63. const downloadInfo = {
  64. url: page.url,
  65. saveAs: options.confirmFilename,
  66. filename: page.filename,
  67. conflictAction: options.filenameConflictAction
  68. };
  69. if (options.incognito) {
  70. downloadInfo.incognito = true;
  71. }
  72. let downloadId;
  73. try {
  74. downloadId = await browser.downloads.download(downloadInfo);
  75. } catch (error) {
  76. if (error.message) {
  77. if (!error.message.toLowerCase().includes("canceled")) {
  78. throw error;
  79. }
  80. } else {
  81. throw error;
  82. }
  83. }
  84. return new Promise((resolve, reject) => {
  85. browser.downloads.onChanged.addListener(onChanged);
  86. function onChanged(event) {
  87. if (event.id == downloadId && event.state) {
  88. if (event.state.current == "complete") {
  89. URL.revokeObjectURL(page.url);
  90. resolve({});
  91. browser.downloads.onChanged.removeListener(onChanged);
  92. }
  93. if (event.state.current == "interrupted" && (!event.error || event.error.current != "USER_CANCELED")) {
  94. URL.revokeObjectURL(page.url);
  95. reject(new Error(event.state.current));
  96. browser.downloads.onChanged.removeListener(onChanged);
  97. }
  98. }
  99. }
  100. });
  101. }
  102. })();