download.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, conflictAction: message.filenameConflictAction })
  46. .catch(error => {
  47. if (error.message && error.message.includes("'incognito'")) {
  48. return downloadPage(message, { confirmFilename: message.confirmFilename, conflictAction: message.filenameConflictAction });
  49. } else {
  50. return { notSupported: true };
  51. }
  52. });
  53. } catch (error) {
  54. return Promise.resolve({ notSupported: true });
  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. const downloadId = await browser.downloads.download(downloadInfo);
  68. return new Promise((resolve, reject) => {
  69. browser.downloads.onChanged.addListener(onChanged);
  70. function onChanged(event) {
  71. if (event.id == downloadId && event.state) {
  72. if (event.state.current == "complete") {
  73. URL.revokeObjectURL(page.url);
  74. resolve({});
  75. browser.downloads.onChanged.removeListener(onChanged);
  76. }
  77. if (event.state.current == "interrupted" && (!event.error || event.error.current != "USER_CANCELED")) {
  78. URL.revokeObjectURL(page.url);
  79. reject(new Error(event.state.current));
  80. browser.downloads.onChanged.removeListener(onChanged);
  81. }
  82. }
  83. }
  84. });
  85. }
  86. })();