download.js 3.1 KB

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