autosave.js 5.1 KB

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