ui-menu.js 12 KB

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