tabs.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import * as config from "./config.js";
  25. import * as autosave from "./autosave.js";
  26. import * as business from "./business.js";
  27. import * as editor from "./editor.js";
  28. import * as tabsData from "./tabs-data.js";
  29. import * as ui from "./../../ui/bg/index.js";
  30. const DELAY_MAYBE_INIT = 1500;
  31. browser.tabs.onCreated.addListener(tab => onTabCreated(tab));
  32. browser.tabs.onActivated.addListener(activeInfo => onTabActivated(activeInfo));
  33. browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
  34. browser.tabs.onUpdated.addListener((tabId, changeInfo) => onTabUpdated(tabId, changeInfo));
  35. browser.tabs.onReplaced.addListener((addedTabId, removedTabId) => onTabReplaced(addedTabId, removedTabId));
  36. export {
  37. onMessage
  38. };
  39. async function onMessage(message, sender) {
  40. if (message.method.endsWith(".init")) {
  41. await onInit(sender.tab, message);
  42. ui.onInit(sender.tab);
  43. business.onInit(sender.tab);
  44. autosave.onInit(sender.tab);
  45. }
  46. if (message.method.endsWith(".getOptions")) {
  47. return config.getOptions(message.url);
  48. }
  49. if (message.method.endsWith(".activate")) {
  50. await browser.tabs.update(message.tabId, { active: true });
  51. }
  52. }
  53. async function onInit(tab, options) {
  54. await tabsData.remove(tab.id);
  55. const allTabsData = await tabsData.get(tab.id);
  56. allTabsData[tab.id].savedPageDetected = options.savedPageDetected;
  57. await tabsData.set(allTabsData);
  58. }
  59. async function onTabUpdated(tabId, changeInfo) {
  60. if (changeInfo.status == "complete") {
  61. setTimeout(async () => {
  62. try {
  63. await browser.tabs.sendMessage(tabId, { method: "content.maybeInit" });
  64. }
  65. catch (error) {
  66. // ignored
  67. }
  68. }, DELAY_MAYBE_INIT);
  69. autosave.onTabUpdated(tabId);
  70. const tab = await browser.tabs.get(tabId);
  71. if (editor.isEditor(tab)) {
  72. const allTabsData = await tabsData.get(tab.id);
  73. allTabsData[tab.id].editorDetected = true;
  74. await tabsData.set(allTabsData);
  75. ui.onTabActivated(tab);
  76. }
  77. }
  78. if (changeInfo.discarded) {
  79. autosave.onTabDiscarded(tabId);
  80. }
  81. }
  82. function onTabReplaced(addedTabId, removedTabId) {
  83. tabsData.onTabReplaced(addedTabId, removedTabId);
  84. autosave.onTabReplaced(addedTabId, removedTabId);
  85. business.onTabReplaced(addedTabId, removedTabId);
  86. }
  87. function onTabCreated(tab) {
  88. ui.onTabCreated(tab);
  89. }
  90. async function onTabActivated(activeInfo) {
  91. const tab = await browser.tabs.get(activeInfo.tabId);
  92. ui.onTabActivated(tab);
  93. }
  94. function onTabRemoved(tabId) {
  95. tabsData.remove(tabId);
  96. editor.onTabRemoved(tabId);
  97. business.onTabRemoved(tabId);
  98. autosave.onTabRemoved(tabId);
  99. }