external-messages.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 autosave from "./autosave.js";
  25. import * as business from "./business.js";
  26. import "./../../lib/single-file/background.js";
  27. const ACTION_SAVE_PAGE = "save-page";
  28. const ACTION_EDIT_AND_SAVE_PAGE = "edit-and-save-page";
  29. const ACTION_SAVE_SELECTED_LINKS = "save-selected-links";
  30. const ACTION_SAVE_SELECTED = "save-selected-content";
  31. const ACTION_SAVE_SELECTED_TABS = "save-selected-tabs";
  32. const ACTION_SAVE_UNPINNED_TABS = "save-unpinned-tabs";
  33. const ACTION_SAVE_ALL_TABS = "save-all-tabs";
  34. export { onMessage };
  35. async function onMessage(message, sender) {
  36. if (message == ACTION_SAVE_PAGE) {
  37. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  38. tabs.length = 1;
  39. await business.saveTabs(tabs);
  40. } else if (message == ACTION_EDIT_AND_SAVE_PAGE) {
  41. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  42. tabs.length = 1;
  43. await business.saveTabs(tabs, { openEditor: true });
  44. } else if (message == ACTION_SAVE_SELECTED_LINKS) {
  45. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  46. await business.saveSelectedLinks(tabs[0]);
  47. } else if (message == ACTION_SAVE_SELECTED) {
  48. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  49. await business.saveTabs(tabs, { selected: true });
  50. } else if (message == ACTION_SAVE_SELECTED_TABS) {
  51. const tabs = await queryTabs({ currentWindow: true, highlighted: true });
  52. await business.saveTabs(tabs);
  53. } else if (message == ACTION_SAVE_UNPINNED_TABS) {
  54. const tabs = await queryTabs({ currentWindow: true, pinned: false });
  55. await business.saveTabs(tabs);
  56. } else if (message == ACTION_SAVE_ALL_TABS) {
  57. const tabs = await queryTabs({ currentWindow: true });
  58. await business.saveTabs(tabs);
  59. } else if (message.method) {
  60. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  61. const currentTab = tabs[0];
  62. if (currentTab) {
  63. return autosave.onMessageExternal(message, currentTab, sender);
  64. } else {
  65. return false;
  66. }
  67. }
  68. }
  69. async function queryTabs(options) {
  70. const tabs = await browser.tabs.query(options);
  71. return tabs.sort((tab1, tab2) => tab1.index - tab2.index);
  72. }