autosave.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 singlefile, URL, Blob, XMLHttpRequest, woleet */
  24. singlefile.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 = singlefile.extension.ui.bg.main;
  34. if (message.method.endsWith(".init")) {
  35. const [options, autoSaveEnabled] = await Promise.all([singlefile.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 singlefile.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 singlefile.extension.core.bg.tabsData.get(currentTab.id);
  52. tabsData[currentTab.id].autoSave = message.enabled;
  53. await singlefile.extension.core.bg.tabsData.set(tabsData);
  54. singlefile.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([singlefile.extension.core.bg.config.getOptions(tab.url, true), isEnabled(tab)]);
  62. if (options && ((options.autoSaveLoad || options.autoSaveLoadOrUnload) && autoSaveEnabled)) {
  63. singlefile.extension.core.bg.business.saveTabs([tab], { autoSave: true });
  64. }
  65. }
  66. async function isEnabled(tab) {
  67. const config = singlefile.extension.core.bg.config;
  68. if (tab) {
  69. const [tabsData, rule] = await Promise.all([singlefile.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 = singlefile.extension.core.bg.tabs;
  78. const allTabs = (await singlefile.extension.core.bg.tabs.get({}));
  79. return Promise.all(allTabs.map(async tab => {
  80. const [options, autoSaveEnabled] = await Promise.all([singlefile.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 singlefile.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.backgroundTab = true;
  105. options.autoSave = true;
  106. options.incognito = tab.incognito;
  107. options.tabId = tabId;
  108. options.tabIndex = tab.index;
  109. let pageData;
  110. try {
  111. if (options.autoSaveExternalSave) {
  112. await singlefile.extension.core.bg.companion.save(options);
  113. } else {
  114. pageData = await singlefile.extension.getPageData(options, null, null, { fetch });
  115. if (options.includeInfobar) {
  116. await singlefile.common.ui.content.infobar.includeScript(pageData);
  117. }
  118. const blob = new Blob([pageData.content], { type: "text/html" });
  119. if (options.saveToGDrive) {
  120. await singlefile.extension.core.bg.downloads.uploadPage(message.taskId, pageData.filename, blob, options, {});
  121. } else {
  122. pageData.url = URL.createObjectURL(blob);
  123. await singlefile.extension.core.bg.downloads.downloadPage(pageData, options);
  124. }
  125. if (pageData.hash) {
  126. await woleet.anchor(pageData.hash);
  127. }
  128. }
  129. } finally {
  130. singlefile.extension.core.bg.business.onSaveEnd(message.taskId);
  131. if (pageData && pageData.url) {
  132. URL.revokeObjectURL(pageData.url);
  133. }
  134. }
  135. }
  136. function fetch(url) {
  137. return new Promise((resolve, reject) => {
  138. const xhrRequest = new XMLHttpRequest();
  139. xhrRequest.withCredentials = true;
  140. xhrRequest.responseType = "arraybuffer";
  141. xhrRequest.onerror = event => reject(new Error(event.detail));
  142. xhrRequest.onreadystatechange = () => {
  143. if (xhrRequest.readyState == XMLHttpRequest.DONE) {
  144. resolve({
  145. status: xhrRequest.status,
  146. headers: {
  147. get: name => xhrRequest.getResponseHeader(name)
  148. },
  149. arrayBuffer: async () => xhrRequest.response
  150. });
  151. }
  152. };
  153. xhrRequest.open("GET", url, true);
  154. xhrRequest.send();
  155. });
  156. }
  157. })();