messages.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 */
  24. import * as config from "./config.js";
  25. import * as autosave from "./autosave.js";
  26. import * as bookmarks from "./bookmarks.js";
  27. import * as companion from "./companion.js";
  28. import * as devtools from "./devtools.js";
  29. import * as downloads from "./downloads.js";
  30. import * as editor from "./editor.js";
  31. import * as requests from "./requests.js";
  32. import * as tabsData from "./tabs-data.js";
  33. import * as tabs from "./tabs.js";
  34. import * as ui from "./../../ui/bg/index.js";
  35. browser.runtime.onMessage.addListener((message, sender) => {
  36. if (message.method.startsWith("tabs.")) {
  37. return tabs.onMessage(message, sender);
  38. }
  39. if (message.method.startsWith("downloads.")) {
  40. return downloads.onMessage(message, sender);
  41. }
  42. if (message.method.startsWith("autosave.")) {
  43. return autosave.onMessage(message, sender);
  44. }
  45. if (message.method.startsWith("ui.")) {
  46. return ui.onMessage(message, sender);
  47. }
  48. if (message.method.startsWith("config.")) {
  49. return config.onMessage(message, sender);
  50. }
  51. if (message.method.startsWith("tabsData.")) {
  52. return tabsData.onMessage(message, sender);
  53. }
  54. if (message.method.startsWith("devtools.")) {
  55. return devtools.onMessage(message, sender);
  56. }
  57. if (message.method.startsWith("editor.")) {
  58. return editor.onMessage(message, sender);
  59. }
  60. if (message.method.startsWith("bookmarks.")) {
  61. return bookmarks.onMessage(message, sender);
  62. }
  63. if (message.method.startsWith("companion.")) {
  64. return companion.onMessage(message, sender);
  65. }
  66. if (message.method.startsWith("requests.")) {
  67. return requests.onMessage(message, sender);
  68. }
  69. });
  70. if (browser.runtime.onMessageExternal) {
  71. browser.runtime.onMessageExternal.addListener(async (message, sender) => {
  72. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  73. const currentTab = tabs[0];
  74. if (currentTab) {
  75. return autosave.onMessageExternal(message, currentTab, sender);
  76. } else {
  77. return false;
  78. }
  79. });
  80. }