ui.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.ui = (() => {
  22. const FORBIDDEN_URLS = ["https://chrome.google.com", "https://addons.mozilla.org"];
  23. let persistentTabsData;
  24. let temporaryTabsData;
  25. getPersistentTabsData().then(tabsData => persistentTabsData = tabsData);
  26. browser.tabs.onRemoved.addListener(async tabId => {
  27. const tabsData = await getPersistentTabsData();
  28. delete tabsData[tabId];
  29. await browser.storage.local.set({ tabsData });
  30. });
  31. return {
  32. processTab,
  33. isAllowedURL,
  34. getTemporaryTabsData,
  35. getPersistentTabsData,
  36. setPersistentData
  37. };
  38. async function processTab(tab, options = {}) {
  39. const tabId = tab.id;
  40. try {
  41. singlefile.ui.button.onInitialize(tabId, options, 1);
  42. await singlefile.core.processTab(tab, options);
  43. singlefile.ui.button.onInitialize(tabId, options, 2);
  44. } catch (error) {
  45. console.log(error); // eslint-disable-line no-console
  46. singlefile.ui.button.onError(tabId, options);
  47. }
  48. }
  49. function isAllowedURL(url) {
  50. return url && (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !FORBIDDEN_URLS.find(storeUrl => url.startsWith(storeUrl));
  51. }
  52. function getTemporaryTabsData() {
  53. if (temporaryTabsData) {
  54. return temporaryTabsData;
  55. } else {
  56. return {};
  57. }
  58. }
  59. async function setPersistentData(tabsData) {
  60. await browser.storage.local.set({ tabsData });
  61. }
  62. async function getPersistentTabsData() {
  63. if (persistentTabsData) {
  64. return persistentTabsData;
  65. } else {
  66. const config = await browser.storage.local.get();
  67. persistentTabsData = config.tabsData || {};
  68. await cleanupPersistentTabsData();
  69. return persistentTabsData;
  70. }
  71. }
  72. async function cleanupPersistentTabsData() {
  73. if (persistentTabsData) {
  74. const tabs = await browser.tabs.query({});
  75. Object.keys(persistentTabsData).filter(tabId => !tabs.find(tab => tab.id == tabId)).forEach(tabId => delete persistentTabsData[tabId]);
  76. await browser.storage.local.set({ tabsData: persistentTabsData });
  77. }
  78. }
  79. })();