autosave.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2010-2019 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 */
  24. singlefile.extension.core.bg.autosave = (() => {
  25. return {
  26. onMessage,
  27. onMessageExternal,
  28. onTabUpdated,
  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.autoClose) {
  42. singlefile.extension.core.bg.tabs.remove(tabId);
  43. }
  44. ui.onStart(tabId, 1, true);
  45. await saveContent(message, sender.tab);
  46. ui.onEnd(tabId, true);
  47. return {};
  48. }
  49. }
  50. async function onMessageExternal(message, currentTab) {
  51. if (message.method == "enableAutoSave") {
  52. const tabsData = await singlefile.extension.core.bg.tabsData.get(currentTab.id);
  53. tabsData[currentTab.id].autoSave = message.enabled;
  54. await singlefile.extension.core.bg.tabsData.set(tabsData);
  55. singlefile.extension.ui.bg.main.refreshTab(currentTab);
  56. }
  57. if (message.method == "isAutoSaveEnabled") {
  58. return isEnabled(currentTab);
  59. }
  60. }
  61. async function onTabUpdated(tabId, changeInfo, tab) {
  62. const [options, autoSaveEnabled] = await Promise.all([singlefile.extension.core.bg.config.getOptions(tab.url, true), isEnabled(tab)]);
  63. if (options && ((options.autoSaveLoad || options.autoSaveLoadOrUnload) && autoSaveEnabled)) {
  64. if (changeInfo.status == "complete") {
  65. singlefile.extension.core.bg.business.saveTab(tab, { autoSave: true });
  66. }
  67. }
  68. }
  69. async function isEnabled(tab) {
  70. const config = singlefile.extension.core.bg.config;
  71. if (tab) {
  72. const [tabsData, rule] = await Promise.all([singlefile.extension.core.bg.tabsData.get(), config.getRule(tab.url)]);
  73. return Boolean(tabsData.autoSaveAll ||
  74. (tabsData.autoSaveUnpinned && !tab.pinned) ||
  75. (tabsData[tab.id] && tabsData[tab.id].autoSave)) &&
  76. (!rule || rule.autoSaveProfile != config.DISABLED_PROFILE_NAME);
  77. }
  78. }
  79. async function refreshTabs() {
  80. const tabs = singlefile.extension.core.bg.tabs;
  81. const allTabs = (await singlefile.extension.core.bg.tabs.get({}));
  82. return Promise.all(allTabs.map(async tab => {
  83. const [options, autoSaveEnabled] = await Promise.all([singlefile.extension.core.bg.config.getOptions(tab.url, true), isEnabled(tab)]);
  84. try {
  85. await tabs.sendMessage(tab.id, { method: "content.init", autoSaveEnabled, options });
  86. } catch (error) {
  87. // ignored
  88. }
  89. }));
  90. }
  91. async function saveContent(message, tab) {
  92. const options = await singlefile.extension.core.bg.config.getOptions(tab.url, true);
  93. const tabId = tab.id;
  94. options.content = message.content;
  95. options.url = message.url;
  96. options.frames = message.frames;
  97. options.canvases = message.canvases;
  98. options.fonts = message.fonts;
  99. options.stylesheets = message.stylesheets;
  100. options.images = message.images;
  101. options.posters = message.posters;
  102. options.usedFonts = message.usedFonts;
  103. options.shadowRoots = message.shadowRoots;
  104. options.imports = message.imports;
  105. options.referrer = message.referrer;
  106. options.updatedResources = message.updatedResources;
  107. options.insertSingleFileComment = true;
  108. options.insertFaviconLink = true;
  109. options.backgroundTab = true;
  110. options.autoSave = true;
  111. options.incognito = tab.incognito;
  112. options.tabId = tabId;
  113. options.tabIndex = tab.index;
  114. const processor = new (singlefile.lib.SingleFile.getClass())(options);
  115. await processor.run();
  116. const pageData = await processor.getPageData();
  117. if (options.includeInfobar) {
  118. await singlefile.common.ui.content.infobar.includeScript(pageData);
  119. }
  120. pageData.url = URL.createObjectURL(new Blob([pageData.content], { type: "text/html" }));
  121. try {
  122. await singlefile.extension.core.bg.downloads.downloadPage(pageData, options);
  123. } finally {
  124. URL.revokeObjectURL(pageData.url);
  125. }
  126. }
  127. })();