tabs.js 3.5 KB

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