download.js 3.4 KB

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