autosave.js 5.7 KB

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