download.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.toLowerCase().includes("canceled")) {
  48. if (error.message.includes("'incognito'")) {
  49. return downloadPage(message, { confirmFilename: message.confirmFilename, filenameConflictAction: message.filenameConflictAction });
  50. } else if (error.message == "conflictAction prompt not yet implemented") {
  51. return downloadPage(message, { confirmFilename: message.confirmFilename });
  52. } else {
  53. throw error;
  54. }
  55. }
  56. }
  57. });
  58. }
  59. async function downloadPage(page, options) {
  60. const downloadInfo = {
  61. url: page.url,
  62. saveAs: options.confirmFilename,
  63. filename: page.filename,
  64. conflictAction: options.filenameConflictAction
  65. };
  66. if (options.incognito) {
  67. downloadInfo.incognito = true;
  68. }
  69. let downloadId;
  70. try {
  71. downloadId = await browser.downloads.download(downloadInfo);
  72. } catch (error) {
  73. if (error.message) {
  74. if (!error.message.toLowerCase().includes("canceled")) {
  75. if (error.message.includes("'incognito'")) {
  76. return downloadPage(page, { confirmFilename: options.confirmFilename, filenameConflictAction: options.filenameConflictAction });
  77. } else {
  78. throw error;
  79. }
  80. }
  81. } else {
  82. throw error;
  83. }
  84. }
  85. return new Promise((resolve, reject) => {
  86. browser.downloads.onChanged.addListener(onChanged);
  87. function onChanged(event) {
  88. if (event.id == downloadId && event.state) {
  89. if (event.state.current == "complete") {
  90. URL.revokeObjectURL(page.url);
  91. resolve({});
  92. browser.downloads.onChanged.removeListener(onChanged);
  93. }
  94. if (event.state.current == "interrupted" && (!event.error || event.error.current != "USER_CANCELED")) {
  95. URL.revokeObjectURL(page.url);
  96. reject(new Error(event.state.current));
  97. browser.downloads.onChanged.removeListener(onChanged);
  98. }
  99. }
  100. }
  101. });
  102. }
  103. })();