autosave.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 */
  21. singlefile.autosave = (() => {
  22. browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
  23. const [options, tabsData] = await Promise.all([singlefile.config.getDefaultConfig(), singlefile.tabsData.get()]);
  24. if ((options.autoSaveLoad || options.autoSaveLoadOrUnload) && (tabsData.autoSaveAll || (tabsData.autoSaveUnpinned && !tab.pinned) || (tabsData[tab.id] && tabsData[tab.id].autoSave))) {
  25. if (changeInfo.status == "complete") {
  26. singlefile.ui.saveTab(tab, { autoSave: true });
  27. }
  28. }
  29. });
  30. browser.runtime.onMessage.addListener((message, sender) => {
  31. if (message.isAutoSaveEnabled) {
  32. return isAutoSaveEnabled(sender.tab.id);
  33. }
  34. });
  35. if (browser.runtime.onMessageExternal) {
  36. browser.runtime.onMessageExternal.addListener(async message => {
  37. if (message.method == "enableAutoSave") {
  38. enableActiveTab(message.enabled);
  39. }
  40. if (message.method == "isAutoSaveEnabled") {
  41. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  42. const tab = tabs[0];
  43. if (tab && singlefile.core.isAllowedURL(tab.url)) {
  44. const tabId = tab.id;
  45. const tabsData = await singlefile.tabsData.get();
  46. return tabsData.autoSaveAll || (tabsData.autoSaveUnpinned && !tab.pinned) || (tabsData[tabId] && tabsData[tabId].autoSave);
  47. }
  48. return false;
  49. }
  50. });
  51. }
  52. return {
  53. enabled,
  54. refresh
  55. };
  56. async function enableActiveTab(enabled) {
  57. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  58. const tab = tabs[0];
  59. if (tab) {
  60. const tabId = tab.id;
  61. const tabsData = await singlefile.tabsData.get();
  62. if (!tabsData[tabId]) {
  63. tabsData[tabId] = {};
  64. }
  65. tabsData[tabId].autoSave = enabled;
  66. await singlefile.tabsData.set(tabsData);
  67. singlefile.ui.button.refresh(tabId, { autoSave: enabled });
  68. }
  69. }
  70. async function isAutoSaveEnabled(tabId) {
  71. const [options, autoSaveEnabled] = await Promise.all([singlefile.config.getDefaultConfig(), enabled(tabId)]);
  72. return { autoSaveEnabled, options };
  73. }
  74. async function enabled(tabId) {
  75. const tabsData = await singlefile.tabsData.get();
  76. return Boolean(tabsData.autoSaveAll || tabsData.autoSaveUnpinned || (tabsData[tabId] && tabsData[tabId].autoSave));
  77. }
  78. async function refresh() {
  79. const tabs = await browser.tabs.query({});
  80. return Promise.all(tabs.map(async tab => {
  81. try {
  82. const [autoSaveEnabled, options] = await Promise.all([enabled(tab.id), singlefile.config.getDefaultConfig()]);
  83. await browser.tabs.sendMessage(tab.id, { autoSaveUnloadEnabled: true, autoSaveEnabled, options });
  84. } catch (error) {
  85. /* ignored */
  86. }
  87. }));
  88. }
  89. })();