tabs-data.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2010-2020 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 browser, setTimeout */
  24. let persistentData, temporaryData, cleanedUp;
  25. setTimeout(() => getPersistent().then(tabsData => persistentData = tabsData), 0);
  26. export {
  27. onMessage,
  28. getTemporary,
  29. getPersistent as get,
  30. setPersistent as set,
  31. remove
  32. };
  33. function onMessage(message) {
  34. if (message.method.endsWith(".get")) {
  35. return getPersistent();
  36. }
  37. if (message.method.endsWith(".set")) {
  38. return setPersistent(message.tabsData);
  39. }
  40. }
  41. async function remove(tabId) {
  42. if (temporaryData) {
  43. delete temporaryData[tabId];
  44. }
  45. const tabsData = await getPersistent();
  46. if (tabsData[tabId]) {
  47. const autoSave = tabsData[tabId].autoSave;
  48. tabsData[tabId] = { autoSave };
  49. await setPersistent(tabsData);
  50. }
  51. }
  52. function getTemporary(desiredTabId) {
  53. if (!temporaryData) {
  54. temporaryData = {};
  55. }
  56. if (desiredTabId !== undefined && !temporaryData[desiredTabId]) {
  57. temporaryData[desiredTabId] = {};
  58. }
  59. return temporaryData;
  60. }
  61. async function getPersistent(desiredTabId) {
  62. if (!persistentData) {
  63. const config = await browser.storage.local.get();
  64. persistentData = config.tabsData || {};
  65. }
  66. cleanup();
  67. if (desiredTabId !== undefined && !persistentData[desiredTabId]) {
  68. persistentData[desiredTabId] = {};
  69. }
  70. return persistentData;
  71. }
  72. async function setPersistent(tabsData) {
  73. persistentData = tabsData;
  74. await browser.storage.local.set({ tabsData });
  75. }
  76. async function cleanup() {
  77. if (!cleanedUp) {
  78. cleanedUp = true;
  79. const tabs = await browser.tabs.query({ currentWindow: true, highlighted: true });
  80. Object.keys(persistentData).filter(key => {
  81. if (key != "autoSaveAll" && key != "autoSaveUnpinned" && key != "profileName") {
  82. return !tabs.find(tab => tab.id == key);
  83. }
  84. }).forEach(tabId => delete persistentData[tabId]);
  85. await browser.storage.local.set({ tabsData: persistentData });
  86. }
  87. }