autosave.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global browser, singlefile, SingleFileBrowser, URL, Blob */
  21. singlefile.autosave = (() => {
  22. browser.runtime.onMessage.addListener((message, sender) => {
  23. if (message.isAutoSaveEnabled) {
  24. return isAutoSaveEnabled(sender.tab);
  25. }
  26. if (message.autoSaveContent) {
  27. saveContent(message, sender.tab);
  28. }
  29. });
  30. if (browser.runtime.onMessageExternal) {
  31. browser.runtime.onMessageExternal.addListener(async message => {
  32. if (message.method == "enableAutoSave") {
  33. enableActiveTab(message.enabled);
  34. }
  35. if (message.method == "isAutoSaveEnabled") {
  36. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  37. const tab = tabs[0];
  38. if (tab && singlefile.core.isAllowedURL(tab.url)) {
  39. const tabId = tab.id;
  40. const tabsData = await singlefile.tabsData.get();
  41. return tabsData.autoSaveAll || (tabsData.autoSaveUnpinned && !tab.pinned) || (tabsData[tabId] && tabsData[tabId].autoSave);
  42. }
  43. return false;
  44. }
  45. });
  46. }
  47. return {
  48. onTabUpdated,
  49. enabled,
  50. refresh
  51. };
  52. async function onTabUpdated(tabId, changeInfo, tab) {
  53. const tabsData = await singlefile.tabsData.get();
  54. const options = await singlefile.config.getOptions(tabsData.profileName, tab.url, true);
  55. if (options && ((options.autoSaveLoad || options.autoSaveLoadOrUnload) && (tabsData.autoSaveAll || (tabsData.autoSaveUnpinned && !tab.pinned) || (tabsData[tab.id] && tabsData[tab.id].autoSave)))) {
  56. if (changeInfo.status == "complete") {
  57. singlefile.ui.saveTab(tab, { autoSave: true });
  58. }
  59. }
  60. }
  61. async function saveContent(message, tab) {
  62. const tabsData = await singlefile.tabsData.get();
  63. const options = await singlefile.config.getOptions(tabsData.profileName, tab.url, true);
  64. const tabId = tab.id;
  65. options.content = message.content;
  66. options.url = message.url;
  67. options.framesData = message.framesData;
  68. options.canvasData = message.canvasData;
  69. options.fontsData = message.fontsData;
  70. options.stylesheetContents = message.stylesheetContents;
  71. options.imageData = message.imageData;
  72. options.postersData = message.postersData;
  73. options.usedFonts = message.usedFonts;
  74. options.shadowRootContents = message.shadowRootContents;
  75. options.insertSingleFileComment = true;
  76. options.insertFaviconLink = true;
  77. options.backgroundTab = true;
  78. options.autoSave = true;
  79. options.incognito = tab.incognito;
  80. options.tabId = tabId;
  81. options.sessionId = 0;
  82. let index = 0, maxIndex = 0;
  83. options.onprogress = async event => {
  84. if (event.type == event.RESOURCES_INITIALIZED) {
  85. maxIndex = event.detail.max;
  86. singlefile.ui.onProgress(tabId, index, maxIndex, { autoSave: true });
  87. }
  88. if (event.type == event.RESOURCE_LOADED) {
  89. index++;
  90. singlefile.ui.onProgress(tabId, index, maxIndex, { autoSave: true });
  91. } else if (event.type == event.PAGE_ENDED) {
  92. singlefile.ui.onEnd(tabId, { autoSave: true });
  93. }
  94. };
  95. const processor = new (SingleFileBrowser.getClass())(options);
  96. await processor.initialize();
  97. await processor.run();
  98. const page = await processor.getPageData();
  99. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  100. return singlefile.download.downloadPage(page, options);
  101. }
  102. async function enableActiveTab(enabled) {
  103. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  104. const tab = tabs[0];
  105. if (tab) {
  106. const tabId = tab.id;
  107. const tabsData = await singlefile.tabsData.get();
  108. if (!tabsData[tabId]) {
  109. tabsData[tabId] = {};
  110. }
  111. tabsData[tabId].autoSave = enabled;
  112. await singlefile.tabsData.set(tabsData);
  113. singlefile.ui.refresh(tab);
  114. }
  115. }
  116. async function isAutoSaveEnabled(tab) {
  117. const tabsData = await singlefile.tabsData.get();
  118. const [options, autoSaveEnabled] = await Promise.all([singlefile.config.getOptions(tabsData.profileName, tab.url, true), enabled(tab.id)]);
  119. return { autoSaveEnabled, options };
  120. }
  121. async function enabled(tabId) {
  122. const tabsData = await singlefile.tabsData.get();
  123. return Boolean(tabsData.autoSaveAll || tabsData.autoSaveUnpinned || (tabsData[tabId] && tabsData[tabId].autoSave));
  124. }
  125. async function refresh() {
  126. const tabs = await browser.tabs.query({});
  127. return Promise.all(tabs.map(async tab => {
  128. try {
  129. const tabsData = await singlefile.tabsData.get();
  130. const [options, autoSaveEnabled] = await Promise.all([singlefile.config.getOptions(tabsData.profileName, tab.url, true), enabled(tab.id)]);
  131. await browser.tabs.sendMessage(tab.id, { autoSaveUnloadEnabled: true, autoSaveEnabled, options });
  132. } catch (error) {
  133. /* ignored */
  134. }
  135. }));
  136. }
  137. })();