ui-menu.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.menu = (() => {
  22. const BROWSER_MENUS_API_SUPPORTED = browser.menus && browser.menus.onClicked && browser.menus.create && browser.menus.update && browser.menus.removeAll;
  23. const MENU_ID_SAVE_PAGE = "save-page";
  24. const MENU_ID_SELECT_PROFILE = "select-profile";
  25. const MENU_ID_SELECT_PROFILE_PREFIX = "select-profile-";
  26. const MENU_ID_SAVE_SELECTED = "save-selected";
  27. const MENU_ID_SAVE_FRAME = "save-frame";
  28. const MENU_ID_SAVE_SELECTED_TABS = "save-selected-tabs";
  29. const MENU_ID_SAVE_UNPINNED_TABS = "save-unpinned-tabs";
  30. const MENU_ID_SAVE_ALL_TABS = "save-tabs";
  31. const MENU_ID_AUTO_SAVE = "auto-save";
  32. const MENU_ID_AUTO_SAVE_DISABLED = "auto-save-disabled";
  33. const MENU_ID_AUTO_SAVE_TAB = "auto-save-tab";
  34. const MENU_ID_AUTO_SAVE_UNPINNED = "auto-save-unpinned";
  35. const MENU_ID_AUTO_SAVE_ALL = "auto-save-all";
  36. initialize();
  37. browser.tabs.onActivated.addListener(async activeInfo => {
  38. const tab = await browser.tabs.get(activeInfo.tabId);
  39. await refreshTab(tab);
  40. });
  41. browser.tabs.onCreated.addListener(refreshTab);
  42. return {
  43. refresh
  44. };
  45. async function refresh(tab) {
  46. const [profiles, tabsData] = await Promise.all([singlefile.config.getProfiles(), singlefile.tabsData.get()]);
  47. const options = await singlefile.config.getOptions(tabsData.profileName, tab && tab.url, true);
  48. if (BROWSER_MENUS_API_SUPPORTED) {
  49. const pageContextsEnabled = ["page", "frame", "image", "link", "video", "audio"];
  50. const defaultContextsDisabled = ["browser_action"];
  51. const defaultContextsEnabled = defaultContextsDisabled.concat(...pageContextsEnabled);
  52. const defaultContexts = options.contextMenuEnabled ? defaultContextsEnabled : defaultContextsDisabled;
  53. await browser.menus.removeAll();
  54. if (options.contextMenuEnabled) {
  55. browser.menus.create({
  56. id: MENU_ID_SAVE_PAGE,
  57. contexts: pageContextsEnabled,
  58. title: browser.i18n.getMessage("menuSavePage")
  59. });
  60. }
  61. if (options.contextMenuEnabled) {
  62. browser.menus.create({
  63. id: "separator-1",
  64. contexts: pageContextsEnabled,
  65. type: "separator"
  66. });
  67. }
  68. browser.menus.create({
  69. id: MENU_ID_SAVE_SELECTED,
  70. contexts: defaultContexts,
  71. title: browser.i18n.getMessage("menuSaveSelection")
  72. });
  73. if (options.contextMenuEnabled) {
  74. browser.menus.create({
  75. id: MENU_ID_SAVE_FRAME,
  76. contexts: ["frame"],
  77. title: browser.i18n.getMessage("menuSaveFrame")
  78. });
  79. browser.menus.create({
  80. id: MENU_ID_SAVE_SELECTED_TABS,
  81. contexts: pageContextsEnabled,
  82. title: browser.i18n.getMessage("menuSaveSelectedTabs")
  83. });
  84. }
  85. browser.menus.create({
  86. id: MENU_ID_SAVE_UNPINNED_TABS,
  87. contexts: defaultContexts,
  88. title: browser.i18n.getMessage("menuUnpinnedTabs")
  89. });
  90. browser.menus.create({
  91. id: MENU_ID_SAVE_ALL_TABS,
  92. contexts: defaultContexts,
  93. title: browser.i18n.getMessage("menuAllTabs")
  94. });
  95. if (options.contextMenuEnabled) {
  96. browser.menus.create({
  97. id: "separator-2",
  98. contexts: pageContextsEnabled,
  99. type: "separator"
  100. });
  101. }
  102. if (Object.keys(profiles).length > 1) {
  103. browser.menus.create({
  104. id: MENU_ID_SELECT_PROFILE,
  105. title: browser.i18n.getMessage("menuSelectProfile"),
  106. contexts: defaultContexts,
  107. });
  108. browser.menus.create({
  109. id: MENU_ID_SELECT_PROFILE_PREFIX + "default",
  110. type: "radio",
  111. contexts: defaultContexts,
  112. title: browser.i18n.getMessage("profileDefaultSettings"),
  113. checked: !tabsData.profileName || tabsData.profileName == singlefile.config.DEFAULT_PROFILE_NAME,
  114. parentId: MENU_ID_SELECT_PROFILE
  115. });
  116. Object.keys(profiles).forEach((profileName, profileIndex) => {
  117. if (profileName != singlefile.config.DEFAULT_PROFILE_NAME) {
  118. browser.menus.create({
  119. id: MENU_ID_SELECT_PROFILE_PREFIX + profileIndex,
  120. type: "radio",
  121. contexts: defaultContexts,
  122. title: profileName,
  123. checked: tabsData.profileName == profileName,
  124. parentId: MENU_ID_SELECT_PROFILE
  125. });
  126. }
  127. });
  128. }
  129. browser.menus.create({
  130. id: MENU_ID_AUTO_SAVE,
  131. contexts: defaultContexts,
  132. title: browser.i18n.getMessage("menuAutoSave")
  133. });
  134. browser.menus.create({
  135. id: MENU_ID_AUTO_SAVE_DISABLED,
  136. type: "radio",
  137. title: browser.i18n.getMessage("menuAutoSaveDisabled"),
  138. contexts: defaultContexts,
  139. checked: true,
  140. parentId: MENU_ID_AUTO_SAVE
  141. });
  142. browser.menus.create({
  143. id: MENU_ID_AUTO_SAVE_TAB,
  144. type: "radio",
  145. title: browser.i18n.getMessage("menuAutoSaveTab"),
  146. contexts: defaultContexts,
  147. checked: false,
  148. parentId: MENU_ID_AUTO_SAVE
  149. });
  150. browser.menus.create({
  151. id: MENU_ID_AUTO_SAVE_UNPINNED,
  152. type: "radio",
  153. title: browser.i18n.getMessage("menuAutoSaveUnpinnedTabs"),
  154. contexts: defaultContexts,
  155. checked: false,
  156. parentId: MENU_ID_AUTO_SAVE
  157. });
  158. browser.menus.create({
  159. id: MENU_ID_AUTO_SAVE_ALL,
  160. type: "radio",
  161. title: browser.i18n.getMessage("menuAutoSaveAllTabs"),
  162. contexts: defaultContexts,
  163. checked: false,
  164. parentId: MENU_ID_AUTO_SAVE
  165. });
  166. }
  167. }
  168. async function initialize() {
  169. if (BROWSER_MENUS_API_SUPPORTED) {
  170. refresh();
  171. browser.menus.onClicked.addListener(async (event, tab) => {
  172. if (event.menuItemId == MENU_ID_SAVE_PAGE) {
  173. singlefile.ui.saveTab(tab);
  174. }
  175. if (event.menuItemId == MENU_ID_SAVE_SELECTED) {
  176. singlefile.ui.saveTab(tab, { selected: true });
  177. }
  178. if (event.menuItemId == MENU_ID_SAVE_FRAME) {
  179. singlefile.ui.saveTab(tab, { frameId: event.frameId });
  180. }
  181. if (event.menuItemId == MENU_ID_SAVE_SELECTED_TABS) {
  182. const tabs = await browser.tabs.query({ currentWindow: true, highlighted: true });
  183. tabs.forEach(tab => singlefile.ui.isAllowedURL(tab.url) && singlefile.ui.saveTab(tab));
  184. }
  185. if (event.menuItemId == MENU_ID_SAVE_UNPINNED_TABS) {
  186. const tabs = await browser.tabs.query({ currentWindow: true, pinned: false });
  187. tabs.forEach(tab => singlefile.ui.isAllowedURL(tab.url) && singlefile.ui.saveTab(tab));
  188. }
  189. if (event.menuItemId == MENU_ID_SAVE_ALL_TABS) {
  190. const tabs = await browser.tabs.query({ currentWindow: true });
  191. tabs.forEach(tab => singlefile.ui.isAllowedURL(tab.url) && singlefile.ui.saveTab(tab));
  192. }
  193. if (event.menuItemId == MENU_ID_AUTO_SAVE_TAB) {
  194. const tabsData = await singlefile.tabsData.get();
  195. if (!tabsData[tab.id]) {
  196. tabsData[tab.id] = {};
  197. }
  198. tabsData[tab.id].autoSave = event.checked;
  199. await singlefile.tabsData.set(tabsData);
  200. refreshExternalComponents(tab.id, { autoSave: true });
  201. }
  202. if (event.menuItemId == MENU_ID_AUTO_SAVE_DISABLED) {
  203. const tabsData = await singlefile.tabsData.get();
  204. Object.keys(tabsData).forEach(tabId => tabsData[tabId].autoSave = false);
  205. tabsData.autoSaveUnpinned = tabsData.autoSaveAll = false;
  206. await singlefile.tabsData.set(tabsData);
  207. refreshExternalComponents(tab.id, { autoSave: false });
  208. }
  209. if (event.menuItemId == MENU_ID_AUTO_SAVE_ALL) {
  210. const tabsData = await singlefile.tabsData.get();
  211. tabsData.autoSaveAll = event.checked;
  212. await singlefile.tabsData.set(tabsData);
  213. refreshExternalComponents(tab.id, { autoSave: true });
  214. }
  215. if (event.menuItemId == MENU_ID_AUTO_SAVE_UNPINNED) {
  216. const tabsData = await singlefile.tabsData.get();
  217. tabsData.autoSaveUnpinned = event.checked;
  218. await singlefile.tabsData.set(tabsData);
  219. refreshExternalComponents(tab.id, { autoSave: true });
  220. }
  221. if (event.menuItemId.startsWith(MENU_ID_SELECT_PROFILE_PREFIX)) {
  222. const [profiles, tabsData] = await Promise.all([singlefile.config.getProfiles(), singlefile.tabsData.get()]);
  223. const profileId = event.menuItemId.split(MENU_ID_SELECT_PROFILE_PREFIX)[1];
  224. if (profileId == "default") {
  225. tabsData.profileName = singlefile.config.DEFAULT_PROFILE_NAME;
  226. } else {
  227. const profileIndex = Number(profileId);
  228. tabsData.profileName = Object.keys(profiles)[profileIndex];
  229. }
  230. await singlefile.tabsData.set(tabsData);
  231. refresh();
  232. refreshExternalComponents(tab.id, { autoSave: tabsData.autoSaveAll || tabsData.autoSaveUnpinned || (tabsData[tab.id] && tabsData[tab.id].autoSave) });
  233. }
  234. });
  235. const tabs = await browser.tabs.query({});
  236. tabs.forEach(tab => refreshTab(tab));
  237. }
  238. }
  239. async function refreshExternalComponents(tabId) {
  240. await singlefile.autosave.refresh();
  241. singlefile.ui.button.refresh(tabId);
  242. }
  243. async function refreshTab(tab) {
  244. const tabsData = await singlefile.tabsData.get();
  245. if (BROWSER_MENUS_API_SUPPORTED) {
  246. try {
  247. const disabled = Boolean(!tabsData[tab.id] || !tabsData[tab.id].autoSave);
  248. await browser.menus.update(MENU_ID_AUTO_SAVE_DISABLED, { checked: disabled });
  249. await browser.menus.update(MENU_ID_AUTO_SAVE_TAB, { checked: !disabled });
  250. await browser.menus.update(MENU_ID_AUTO_SAVE_UNPINNED, { checked: Boolean(tabsData.autoSaveUnpinned) });
  251. await browser.menus.update(MENU_ID_AUTO_SAVE_ALL, { checked: Boolean(tabsData.autoSaveAll) });
  252. } catch (error) {
  253. /* ignored */
  254. }
  255. }
  256. }
  257. })();