autosave.js 4.6 KB

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