autosave.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2018 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.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
  23. const tabsData = await singlefile.tabsData.get();
  24. const options = await singlefile.config.getOptions(tabsData.profileName, tab.url, true);
  25. if (options && ((options.autoSaveLoad || options.autoSaveLoadOrUnload) && (tabsData.autoSaveAll || (tabsData.autoSaveUnpinned && !tab.pinned) || (tabsData[tab.id] && tabsData[tab.id].autoSave)))) {
  26. if (changeInfo.status == "complete") {
  27. singlefile.ui.saveTab(tab, { autoSave: true });
  28. }
  29. }
  30. });
  31. browser.runtime.onMessage.addListener((message, sender) => {
  32. if (message.isAutoSaveEnabled) {
  33. return isAutoSaveEnabled(sender.tab);
  34. }
  35. if (message.autoSaveContent) {
  36. saveContent(message, sender.tab);
  37. }
  38. });
  39. if (browser.runtime.onMessageExternal) {
  40. browser.runtime.onMessageExternal.addListener(async message => {
  41. if (message.method == "enableAutoSave") {
  42. enableActiveTab(message.enabled);
  43. }
  44. if (message.method == "isAutoSaveEnabled") {
  45. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  46. const tab = tabs[0];
  47. if (tab && singlefile.core.isAllowedURL(tab.url)) {
  48. const tabId = tab.id;
  49. const tabsData = await singlefile.tabsData.get();
  50. return tabsData.autoSaveAll || (tabsData.autoSaveUnpinned && !tab.pinned) || (tabsData[tabId] && tabsData[tabId].autoSave);
  51. }
  52. return false;
  53. }
  54. });
  55. }
  56. return {
  57. enabled,
  58. refresh
  59. };
  60. async function saveContent(message, tab) {
  61. const tabsData = await singlefile.tabsData.get();
  62. const options = await singlefile.config.getOptions(tabsData.profileName, tab.url, true);
  63. const tabId = tab.id;
  64. options.content = message.content;
  65. options.url = message.url;
  66. options.framesData = message.framesData;
  67. options.canvasData = message.canvasData;
  68. options.fontsData = message.fontsData;
  69. options.stylesheetContents = message.stylesheetContents;
  70. options.imageData = message.imageData;
  71. options.postersData = message.postersData;
  72. options.usedFonts = message.usedFonts;
  73. options.shadowRootContents = message.shadowRootContents;
  74. options.insertSingleFileComment = true;
  75. options.insertFaviconLink = true;
  76. options.backgroundTab = true;
  77. options.autoSave = true;
  78. options.incognito = tab.incognito;
  79. options.tabId = tabId;
  80. options.sessionId = 0;
  81. let index = 0, maxIndex = 0;
  82. options.onprogress = async event => {
  83. if (event.type == event.RESOURCES_INITIALIZED) {
  84. maxIndex = event.detail.max;
  85. singlefile.ui.onProgress(tabId, index, maxIndex, { autoSave: true });
  86. }
  87. if (event.type == event.RESOURCE_LOADED) {
  88. index++;
  89. singlefile.ui.onProgress(tabId, index, maxIndex, { autoSave: true });
  90. } else if (event.type == event.PAGE_ENDED) {
  91. singlefile.ui.onEnd(tabId, { autoSave: true });
  92. }
  93. };
  94. const processor = new (SingleFileBrowser.getClass())(options);
  95. await processor.initialize();
  96. await processor.run();
  97. const page = await processor.getPageData();
  98. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  99. return singlefile.download.downloadPage(page, options);
  100. }
  101. async function enableActiveTab(enabled) {
  102. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  103. const tab = tabs[0];
  104. if (tab) {
  105. const tabId = tab.id;
  106. const tabsData = await singlefile.tabsData.get();
  107. if (!tabsData[tabId]) {
  108. tabsData[tabId] = {};
  109. }
  110. tabsData[tabId].autoSave = enabled;
  111. await singlefile.tabsData.set(tabsData);
  112. singlefile.ui.refresh(tab);
  113. }
  114. }
  115. async function isAutoSaveEnabled(tab) {
  116. const tabsData = await singlefile.tabsData.get();
  117. const [options, autoSaveEnabled] = await Promise.all([singlefile.config.getOptions(tabsData.profileName, tab.url, true), enabled(tab.id)]);
  118. return { autoSaveEnabled, options };
  119. }
  120. async function enabled(tabId) {
  121. const tabsData = await singlefile.tabsData.get();
  122. return Boolean(tabsData.autoSaveAll || tabsData.autoSaveUnpinned || (tabsData[tabId] && tabsData[tabId].autoSave));
  123. }
  124. async function refresh() {
  125. const tabs = await browser.tabs.query({});
  126. return Promise.all(tabs.map(async tab => {
  127. try {
  128. const tabsData = await singlefile.tabsData.get();
  129. const [options, autoSaveEnabled] = await Promise.all([singlefile.config.getOptions(tabsData.profileName, tab.url, true), enabled(tab.id)]);
  130. await browser.tabs.sendMessage(tab.id, { autoSaveUnloadEnabled: true, autoSaveEnabled, options });
  131. } catch (error) {
  132. /* ignored */
  133. }
  134. }));
  135. }
  136. })();