download.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. try {
  29. if (message.truncated) {
  30. let partialContent = partialContents.get(sender.tab.id);
  31. if (!partialContent) {
  32. partialContent = [];
  33. partialContents.set(sender.tab.id, partialContent);
  34. }
  35. partialContent.push(message.content);
  36. if (message.finished) {
  37. partialContents.delete(sender.tab.id);
  38. message.url = URL.createObjectURL(new Blob(partialContent, { type: "text/html" }));
  39. } else {
  40. return Promise.resolve({});
  41. }
  42. } else if (message.content) {
  43. message.url = URL.createObjectURL(new Blob([message.content], { type: "text/html" }));
  44. }
  45. return downloadPage(message, { confirmFilename: message.confirmFilename, incognito: sender.tab.incognito, filenameConflictAction: message.filenameConflictAction })
  46. .catch(error => {
  47. if (error.message) {
  48. if (!error.message.toLowerCase().includes("canceled")) {
  49. if (error.message.includes("'incognito'")) {
  50. return downloadPage(message, { confirmFilename: message.confirmFilename, filenameConflictAction: message.filenameConflictAction });
  51. } else {
  52. return { notSupported: true };
  53. }
  54. }
  55. }
  56. });
  57. } catch (error) {
  58. return Promise.resolve({ notSupported: true });
  59. }
  60. }
  61. async function downloadPage(page, options) {
  62. const downloadInfo = {
  63. url: page.url,
  64. saveAs: options.confirmFilename,
  65. filename: page.filename,
  66. conflictAction: options.filenameConflictAction
  67. };
  68. if (options.incognito) {
  69. downloadInfo.incognito = true;
  70. }
  71. const downloadId = await browser.downloads.download(downloadInfo);
  72. return new Promise((resolve, reject) => {
  73. browser.downloads.onChanged.addListener(onChanged);
  74. function onChanged(event) {
  75. if (event.id == downloadId && event.state) {
  76. if (event.state.current == "complete") {
  77. URL.revokeObjectURL(page.url);
  78. resolve({});
  79. browser.downloads.onChanged.removeListener(onChanged);
  80. }
  81. if (event.state.current == "interrupted" && (!event.error || event.error.current != "USER_CANCELED")) {
  82. URL.revokeObjectURL(page.url);
  83. reject(new Error(event.state.current));
  84. browser.downloads.onChanged.removeListener(onChanged);
  85. }
  86. }
  87. }
  88. });
  89. }
  90. })();