autosave.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 infobar, URL, Blob, XMLHttpRequest */
  24. import * as config from "./config.js";
  25. import * as business from "./business.js";
  26. import * as companion from "./companion.js";
  27. import * as downloads from "./downloads.js";
  28. import * as tabsData from "./tabs-data.js";
  29. import * as tabs from "./tabs.js";
  30. import * as ui from "./../../ui/bg/index.js";
  31. import { getPageData } from "./../../index.js";
  32. import * as woleet from "./../../lib/woleet/woleet.js";
  33. export {
  34. onMessage,
  35. onMessageExternal,
  36. onInit,
  37. isEnabled,
  38. refreshTabs
  39. };
  40. async function onMessage(message, sender) {
  41. if (message.method.endsWith(".init")) {
  42. const [options, autoSaveEnabled] = await Promise.all([config.getOptions(sender.tab.url, true), isEnabled(sender.tab)]);
  43. return { options, autoSaveEnabled };
  44. }
  45. if (message.method.endsWith(".save")) {
  46. const tabId = sender.tab.id;
  47. const options = await config.getOptions(sender.tab.url, true);
  48. if (options) {
  49. ui.onStart(tabId, 1, true);
  50. await saveContent(message, sender.tab);
  51. ui.onEnd(tabId, true);
  52. }
  53. return {};
  54. }
  55. }
  56. async function onMessageExternal(message, currentTab) {
  57. if (message.method == "enableAutoSave") {
  58. const allTabsData = await tabsData.get(currentTab.id);
  59. allTabsData[currentTab.id].autoSave = message.enabled;
  60. await tabsData.set(allTabsData);
  61. ui.refreshTab(currentTab);
  62. }
  63. if (message.method == "isAutoSaveEnabled") {
  64. return isEnabled(currentTab);
  65. }
  66. }
  67. async function onInit(tab) {
  68. const [options, autoSaveEnabled] = await Promise.all([config.getOptions(tab.url, true), isEnabled(tab)]);
  69. if (options && ((options.autoSaveLoad || options.autoSaveLoadOrUnload) && autoSaveEnabled)) {
  70. business.saveTabs([tab], { autoSave: true });
  71. }
  72. }
  73. async function isEnabled(tab) {
  74. if (tab) {
  75. const [allTabsData, rule] = await Promise.all([tabsData.get(), config.getRule(tab.url)]);
  76. return Boolean(allTabsData.autoSaveAll ||
  77. (allTabsData.autoSaveUnpinned && !tab.pinned) ||
  78. (allTabsData[tab.id] && allTabsData[tab.id].autoSave)) &&
  79. (!rule || rule.autoSaveProfile != config.DISABLED_PROFILE_NAME);
  80. }
  81. }
  82. async function refreshTabs() {
  83. const allTabs = (await tabs.get({}));
  84. return Promise.all(allTabs.map(async tab => {
  85. const [options, autoSaveEnabled] = await Promise.all([config.getOptions(tab.url, true), isEnabled(tab)]);
  86. try {
  87. await tabs.sendMessage(tab.id, { method: "content.init", autoSaveEnabled, options });
  88. } catch (error) {
  89. // ignored
  90. }
  91. }));
  92. }
  93. async function saveContent(message, tab) {
  94. const options = await config.getOptions(tab.url, true);
  95. const tabId = tab.id;
  96. options.content = message.content;
  97. options.url = message.url;
  98. options.frames = message.frames;
  99. options.canvases = message.canvases;
  100. options.fonts = message.fonts;
  101. options.stylesheets = message.stylesheets;
  102. options.images = message.images;
  103. options.posters = message.posters;
  104. options.usedFonts = message.usedFonts;
  105. options.shadowRoots = message.shadowRoots;
  106. options.imports = message.imports;
  107. options.referrer = message.referrer;
  108. options.updatedResources = message.updatedResources;
  109. options.visitDate = new Date(message.visitDate);
  110. options.backgroundTab = true;
  111. options.autoSave = true;
  112. options.incognito = tab.incognito;
  113. options.tabId = tabId;
  114. options.tabIndex = tab.index;
  115. let pageData;
  116. try {
  117. if (options.autoSaveExternalSave) {
  118. await companion.save(options);
  119. } else {
  120. pageData = await getPageData(options, null, null, { fetch });
  121. if (options.includeInfobar) {
  122. await infobar.includeScript(pageData);
  123. }
  124. const blob = new Blob([pageData.content], { type: "text/html" });
  125. if (options.saveToGDrive) {
  126. await downloads.uploadPage(message.taskId, pageData.filename, blob, options, {});
  127. } else {
  128. pageData.url = URL.createObjectURL(blob);
  129. await downloads.downloadPage(pageData, options);
  130. }
  131. if (pageData.hash) {
  132. await woleet.anchor(pageData.hash);
  133. }
  134. }
  135. } finally {
  136. business.onSaveEnd(message.taskId);
  137. if (pageData && pageData.url) {
  138. URL.revokeObjectURL(pageData.url);
  139. }
  140. }
  141. }
  142. function fetch(url) {
  143. return new Promise((resolve, reject) => {
  144. const xhrRequest = new XMLHttpRequest();
  145. xhrRequest.withCredentials = true;
  146. xhrRequest.responseType = "arraybuffer";
  147. xhrRequest.onerror = event => reject(new Error(event.detail));
  148. xhrRequest.onreadystatechange = () => {
  149. if (xhrRequest.readyState == XMLHttpRequest.DONE) {
  150. resolve({
  151. status: xhrRequest.status,
  152. headers: {
  153. get: name => xhrRequest.getResponseHeader(name)
  154. },
  155. arrayBuffer: async () => xhrRequest.response
  156. });
  157. }
  158. };
  159. xhrRequest.open("GET", url, true);
  160. xhrRequest.send();
  161. });
  162. }