download.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file 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 GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global browser, document, URL, Blob, MouseEvent, setTimeout, open */
  24. const MAX_CONTENT_SIZE = 32 * (1024 * 1024);
  25. export {
  26. downloadPage
  27. };
  28. async function downloadPage(pageData, options) {
  29. if (options.includeBOM) {
  30. pageData.content = "\ufeff" + pageData.content;
  31. }
  32. if (options.backgroundSave || options.openEditor || options.saveToGDrive || options.saveToGitHub || options.saveWithCompanion || options.saveWithWebDAV) {
  33. const blobURL = URL.createObjectURL(new Blob([pageData.content], { type: "text/html" }));
  34. const message = {
  35. method: "downloads.download",
  36. taskId: options.taskId,
  37. confirmFilename: options.confirmFilename,
  38. filenameConflictAction: options.filenameConflictAction,
  39. filename: pageData.filename,
  40. saveToClipboard: options.saveToClipboard,
  41. saveToGDrive: options.saveToGDrive,
  42. saveWithWebDAV: options.saveWithWebDAV,
  43. webDAVURL: options.webDAVURL,
  44. webDAVUser: options.webDAVUser,
  45. webDAVPassword: options.webDAVPassword,
  46. saveToGitHub: options.saveToGitHub,
  47. githubToken: options.githubToken,
  48. githubUser: options.githubUser,
  49. githubRepository: options.githubRepository,
  50. githubBranch: options.githubBranch,
  51. saveWithCompanion: options.saveWithCompanion,
  52. forceWebAuthFlow: options.forceWebAuthFlow,
  53. filenameReplacementCharacter: options.filenameReplacementCharacter,
  54. openEditor: options.openEditor,
  55. openSavedPage: options.openSavedPage,
  56. compressHTML: options.compressHTML,
  57. backgroundSave: options.backgroundSave,
  58. bookmarkId: options.bookmarkId,
  59. replaceBookmarkURL: options.replaceBookmarkURL,
  60. applySystemTheme: options.applySystemTheme,
  61. defaultEditorMode: options.defaultEditorMode,
  62. includeInfobar: options.includeInfobar,
  63. warnUnsavedPage: options.warnUnsavedPage,
  64. blobURL
  65. };
  66. const result = await browser.runtime.sendMessage(message);
  67. URL.revokeObjectURL(blobURL);
  68. if (result.error) {
  69. message.blobURL = null;
  70. for (let blockIndex = 0; blockIndex * MAX_CONTENT_SIZE < pageData.content.length; blockIndex++) {
  71. message.truncated = pageData.content.length > MAX_CONTENT_SIZE;
  72. if (message.truncated) {
  73. message.finished = (blockIndex + 1) * MAX_CONTENT_SIZE > pageData.content.length;
  74. message.content = pageData.content.substring(blockIndex * MAX_CONTENT_SIZE, (blockIndex + 1) * MAX_CONTENT_SIZE);
  75. } else {
  76. message.content = pageData.content;
  77. }
  78. await browser.runtime.sendMessage(message);
  79. }
  80. }
  81. } else {
  82. if (options.saveToClipboard) {
  83. saveToClipboard(pageData);
  84. } else {
  85. await downloadPageForeground(pageData);
  86. }
  87. if (options.openSavedPage) {
  88. open(URL.createObjectURL(new Blob([pageData.content], { type: "text/html" })));
  89. }
  90. browser.runtime.sendMessage({ method: "ui.processEnd" });
  91. }
  92. await browser.runtime.sendMessage({ method: "downloads.end", taskId: options.taskId, hash: pageData.hash, woleetKey: options.woleetKey });
  93. }
  94. async function downloadPageForeground(pageData) {
  95. if (pageData.filename && pageData.filename.length) {
  96. const link = document.createElement("a");
  97. link.download = pageData.filename;
  98. link.href = URL.createObjectURL(new Blob([pageData.content], { type: "text/html" }));
  99. link.dispatchEvent(new MouseEvent("click"));
  100. setTimeout(() => URL.revokeObjectURL(link.href), 1000);
  101. }
  102. return new Promise(resolve => setTimeout(resolve, 1));
  103. }
  104. function saveToClipboard(page) {
  105. const command = "copy";
  106. document.addEventListener(command, listener);
  107. document.execCommand(command);
  108. document.removeEventListener(command, listener);
  109. function listener(event) {
  110. event.clipboardData.setData("text/html", page.content);
  111. event.clipboardData.setData("text/plain", page.content);
  112. event.preventDefault();
  113. }
  114. }