ui-menus.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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, URL */
  24. import * as config from "./../../core/bg/config.js";
  25. import * as tabs from "./../../core/bg/tabs.js";
  26. import * as tabsData from "./../../core/bg/tabs-data.js";
  27. import * as business from "./../../core/bg/business.js";
  28. import * as autosave from "./../../core/bg/autosave.js";
  29. import * as button from "./ui-button.js";
  30. const menus = browser.menus || browser.contextMenus;
  31. const BROWSER_MENUS_API_SUPPORTED = menus && menus.onClicked && menus.create && menus.update && menus.removeAll;
  32. const MENU_ID_SAVE_PAGE = "save-page";
  33. const MENU_ID_EDIT_AND_SAVE_PAGE = "edit-and-save-page";
  34. const MENU_ID_SAVE_WITH_PROFILE = "save-with-profile";
  35. const MENU_ID_SAVE_SELECTED_LINKS = "save-selected-links";
  36. const MENU_ID_VIEW_PENDINGS = "view-pendings";
  37. const MENU_ID_SELECT_PROFILE = "select-profile";
  38. const MENU_ID_SAVE_WITH_PROFILE_PREFIX = "wasve-with-profile-";
  39. const MENU_ID_SELECT_PROFILE_PREFIX = "select-profile-";
  40. const MENU_ID_ASSOCIATE_WITH_PROFILE = "associate-with-profile";
  41. const MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX = "associate-with-profile-";
  42. const MENU_ID_SAVE_SELECTED = "save-selected";
  43. const MENU_ID_SAVE_FRAME = "save-frame";
  44. const MENU_ID_SAVE_TABS = "save-tabs";
  45. const MENU_ID_SAVE_SELECTED_TABS = "save-selected-tabs";
  46. const MENU_ID_SAVE_UNPINNED_TABS = "save-unpinned-tabs";
  47. const MENU_ID_SAVE_ALL_TABS = "save-all-tabs";
  48. const MENU_ID_BUTTON_SAVE_SELECTED_TABS = "button-" + MENU_ID_SAVE_SELECTED_TABS;
  49. const MENU_ID_BUTTON_SAVE_UNPINNED_TABS = "button-" + MENU_ID_SAVE_UNPINNED_TABS;
  50. const MENU_ID_BUTTON_SAVE_ALL_TABS = "button-" + MENU_ID_SAVE_ALL_TABS;
  51. const MENU_ID_AUTO_SAVE = "auto-save";
  52. const MENU_ID_AUTO_SAVE_DISABLED = "auto-save-disabled";
  53. const MENU_ID_AUTO_SAVE_TAB = "auto-save-tab";
  54. const MENU_ID_AUTO_SAVE_UNPINNED = "auto-save-unpinned";
  55. const MENU_ID_AUTO_SAVE_ALL = "auto-save-all";
  56. const MENU_CREATE_DOMAIN_RULE_MESSAGE = browser.i18n.getMessage("menuCreateDomainRule");
  57. const MENU_UPDATE_RULE_MESSAGE = browser.i18n.getMessage("menuUpdateRule");
  58. const MENU_SAVE_PAGE_MESSAGE = browser.i18n.getMessage("menuSavePage");
  59. const MENU_SAVE_WITH_PROFILE = browser.i18n.getMessage("menuSaveWithProfile");
  60. const MENU_SAVE_SELECTED_LINKS = browser.i18n.getMessage("menuSaveSelectedLinks");
  61. const MENU_EDIT_PAGE_MESSAGE = browser.i18n.getMessage("menuEditPage");
  62. const MENU_EDIT_AND_SAVE_PAGE_MESSAGE = browser.i18n.getMessage("menuEditAndSavePage");
  63. const MENU_VIEW_PENDINGS_MESSAGE = browser.i18n.getMessage("menuViewPendingSaves");
  64. const MENU_SAVE_SELECTION_MESSAGE = browser.i18n.getMessage("menuSaveSelection");
  65. const MENU_SAVE_FRAME_MESSAGE = browser.i18n.getMessage("menuSaveFrame");
  66. const MENU_SAVE_TABS_MESSAGE = browser.i18n.getMessage("menuSaveTabs");
  67. const MENU_SAVE_SELECTED_TABS_MESSAGE = browser.i18n.getMessage("menuSaveSelectedTabs");
  68. const MENU_SAVE_UNPINNED_TABS_MESSAGE = browser.i18n.getMessage("menuSaveUnpinnedTabs");
  69. const MENU_SAVE_ALL_TABS_MESSAGE = browser.i18n.getMessage("menuSaveAllTabs");
  70. const MENU_SELECT_PROFILE_MESSAGE = browser.i18n.getMessage("menuSelectProfile");
  71. const PROFILE_DEFAULT_SETTINGS_MESSAGE = browser.i18n.getMessage("profileDefaultSettings");
  72. const MENU_AUTOSAVE_MESSAGE = browser.i18n.getMessage("menuAutoSave");
  73. const MENU_AUTOSAVE_DISABLED_MESSAGE = browser.i18n.getMessage("menuAutoSaveDisabled");
  74. const MENU_AUTOSAVE_TAB_MESSAGE = browser.i18n.getMessage("menuAutoSaveTab");
  75. const MENU_AUTOSAVE_UNPINNED_TABS_MESSAGE = browser.i18n.getMessage("menuAutoSaveUnpinnedTabs");
  76. const MENU_AUTOSAVE_ALL_TABS_MESSAGE = browser.i18n.getMessage("menuAutoSaveAllTabs");
  77. const MENU_TOP_VISIBLE_ENTRIES = [
  78. MENU_ID_EDIT_AND_SAVE_PAGE,
  79. MENU_ID_SAVE_SELECTED_LINKS,
  80. MENU_ID_SAVE_SELECTED,
  81. MENU_ID_SAVE_FRAME,
  82. MENU_ID_AUTO_SAVE,
  83. MENU_ID_ASSOCIATE_WITH_PROFILE
  84. ];
  85. const menusCheckedState = new Map();
  86. const menusTitleState = new Map();
  87. let contextMenuVisibleState = true;
  88. let allMenuVisibleState = true;
  89. let profileIndexes = new Map();
  90. let menusCreated, pendingRefresh;
  91. Promise.resolve().then(initialize);
  92. export {
  93. onMessage,
  94. refreshTab as onTabCreated,
  95. refreshTab as onTabActivated,
  96. refreshTab as onInit,
  97. createMenus as refreshTab
  98. };
  99. function onMessage(message) {
  100. if (message.method.endsWith("refreshMenu")) {
  101. createMenus();
  102. return Promise.resolve({});
  103. }
  104. }
  105. async function createMenus(tab) {
  106. const [profiles, allTabsData] = await Promise.all([config.getProfiles(), tabsData.get()]);
  107. const options = await config.getOptions(tab && tab.url);
  108. if (BROWSER_MENUS_API_SUPPORTED && options) {
  109. const pageContextsEnabled = ["page", "frame", "image", "link", "video", "audio", "selection"];
  110. const defaultContextsDisabled = [];
  111. if (options.browserActionMenuEnabled) {
  112. defaultContextsDisabled.push("browser_action");
  113. }
  114. if (options.tabMenuEnabled) {
  115. try {
  116. menus.create({
  117. id: "temporary-id",
  118. contexts: ["tab"],
  119. title: "title"
  120. });
  121. defaultContextsDisabled.push("tab");
  122. } catch (error) {
  123. options.tabMenuEnabled = false;
  124. }
  125. }
  126. await menus.removeAll();
  127. const defaultContextsEnabled = defaultContextsDisabled.concat(...pageContextsEnabled);
  128. const defaultContexts = options.contextMenuEnabled ? defaultContextsEnabled : defaultContextsDisabled;
  129. menus.create({
  130. id: MENU_ID_SAVE_PAGE,
  131. contexts: defaultContexts,
  132. title: MENU_SAVE_PAGE_MESSAGE
  133. });
  134. menus.create({
  135. id: MENU_ID_EDIT_AND_SAVE_PAGE,
  136. contexts: defaultContexts,
  137. title: MENU_EDIT_AND_SAVE_PAGE_MESSAGE
  138. });
  139. menus.create({
  140. id: MENU_ID_SAVE_SELECTED_LINKS,
  141. contexts: options.contextMenuEnabled ? defaultContextsDisabled.concat(["selection"]) : defaultContextsDisabled,
  142. title: MENU_SAVE_SELECTED_LINKS
  143. });
  144. if (Object.keys(profiles).length > 1) {
  145. menus.create({
  146. id: MENU_ID_SAVE_WITH_PROFILE,
  147. contexts: defaultContexts,
  148. title: MENU_SAVE_WITH_PROFILE
  149. });
  150. }
  151. if (options.contextMenuEnabled) {
  152. menus.create({
  153. id: "separator-1",
  154. contexts: pageContextsEnabled,
  155. type: "separator"
  156. });
  157. }
  158. menus.create({
  159. id: MENU_ID_SAVE_SELECTED,
  160. contexts: defaultContexts,
  161. title: MENU_SAVE_SELECTION_MESSAGE
  162. });
  163. if (options.contextMenuEnabled) {
  164. menus.create({
  165. id: MENU_ID_SAVE_FRAME,
  166. contexts: ["frame"],
  167. title: MENU_SAVE_FRAME_MESSAGE
  168. });
  169. }
  170. menus.create({
  171. id: MENU_ID_SAVE_TABS,
  172. contexts: defaultContextsDisabled,
  173. title: MENU_SAVE_TABS_MESSAGE
  174. });
  175. menus.create({
  176. id: MENU_ID_BUTTON_SAVE_SELECTED_TABS,
  177. contexts: defaultContextsDisabled,
  178. title: MENU_SAVE_SELECTED_TABS_MESSAGE,
  179. parentId: MENU_ID_SAVE_TABS
  180. });
  181. menus.create({
  182. id: MENU_ID_BUTTON_SAVE_UNPINNED_TABS,
  183. contexts: defaultContextsDisabled,
  184. title: MENU_SAVE_UNPINNED_TABS_MESSAGE,
  185. parentId: MENU_ID_SAVE_TABS
  186. });
  187. menus.create({
  188. id: MENU_ID_BUTTON_SAVE_ALL_TABS,
  189. contexts: defaultContextsDisabled,
  190. title: MENU_SAVE_ALL_TABS_MESSAGE,
  191. parentId: MENU_ID_SAVE_TABS
  192. });
  193. if (options.contextMenuEnabled) {
  194. menus.create({
  195. id: MENU_ID_SAVE_SELECTED_TABS,
  196. contexts: pageContextsEnabled,
  197. title: MENU_SAVE_SELECTED_TABS_MESSAGE
  198. });
  199. menus.create({
  200. id: MENU_ID_SAVE_UNPINNED_TABS,
  201. contexts: pageContextsEnabled,
  202. title: MENU_SAVE_UNPINNED_TABS_MESSAGE
  203. });
  204. menus.create({
  205. id: MENU_ID_SAVE_ALL_TABS,
  206. contexts: pageContextsEnabled,
  207. title: MENU_SAVE_ALL_TABS_MESSAGE
  208. });
  209. menus.create({
  210. id: "separator-2",
  211. contexts: pageContextsEnabled,
  212. type: "separator"
  213. });
  214. }
  215. if (Object.keys(profiles).length > 1) {
  216. menus.create({
  217. id: MENU_ID_SELECT_PROFILE,
  218. title: MENU_SELECT_PROFILE_MESSAGE,
  219. contexts: defaultContexts,
  220. });
  221. menus.create({
  222. id: MENU_ID_SAVE_WITH_PROFILE_PREFIX + "default",
  223. contexts: defaultContexts,
  224. title: PROFILE_DEFAULT_SETTINGS_MESSAGE,
  225. parentId: MENU_ID_SAVE_WITH_PROFILE
  226. });
  227. const defaultProfileId = MENU_ID_SELECT_PROFILE_PREFIX + "default";
  228. const defaultProfileChecked = !allTabsData.profileName || allTabsData.profileName == config.DEFAULT_PROFILE_NAME;
  229. menus.create({
  230. id: defaultProfileId,
  231. type: "radio",
  232. contexts: defaultContexts,
  233. title: PROFILE_DEFAULT_SETTINGS_MESSAGE,
  234. checked: defaultProfileChecked,
  235. parentId: MENU_ID_SELECT_PROFILE
  236. });
  237. menusCheckedState.set(defaultProfileId, defaultProfileChecked);
  238. menus.create({
  239. id: MENU_ID_ASSOCIATE_WITH_PROFILE,
  240. title: MENU_CREATE_DOMAIN_RULE_MESSAGE,
  241. contexts: defaultContexts,
  242. });
  243. menusTitleState.set(MENU_ID_ASSOCIATE_WITH_PROFILE, MENU_CREATE_DOMAIN_RULE_MESSAGE);
  244. let rule;
  245. if (tab && tab.url) {
  246. rule = await config.getRule(tab.url, true);
  247. }
  248. const currentProfileId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "current";
  249. const currentProfileChecked = !rule || (rule.profile == config.CURRENT_PROFILE_NAME);
  250. menus.create({
  251. id: currentProfileId,
  252. type: "radio",
  253. contexts: defaultContexts,
  254. title: config.CURRENT_PROFILE_NAME,
  255. checked: currentProfileChecked,
  256. parentId: MENU_ID_ASSOCIATE_WITH_PROFILE
  257. });
  258. menusCheckedState.set(currentProfileId, currentProfileChecked);
  259. const associatedDefaultProfileId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "default";
  260. const associatedDefaultProfileChecked = Boolean(rule) && (rule.profile == config.DEFAULT_PROFILE_NAME);
  261. menus.create({
  262. id: associatedDefaultProfileId,
  263. type: "radio",
  264. contexts: defaultContexts,
  265. title: PROFILE_DEFAULT_SETTINGS_MESSAGE,
  266. checked: associatedDefaultProfileChecked,
  267. parentId: MENU_ID_ASSOCIATE_WITH_PROFILE
  268. });
  269. menusCheckedState.set(associatedDefaultProfileId, associatedDefaultProfileChecked);
  270. profileIndexes = new Map();
  271. Object.keys(profiles).forEach((profileName, profileIndex) => {
  272. if (profileName != config.DEFAULT_PROFILE_NAME) {
  273. let profileId = MENU_ID_SAVE_WITH_PROFILE_PREFIX + profileIndex;
  274. menus.create({
  275. id: profileId,
  276. contexts: defaultContexts,
  277. title: profileName,
  278. parentId: MENU_ID_SAVE_WITH_PROFILE
  279. });
  280. profileId = MENU_ID_SELECT_PROFILE_PREFIX + profileIndex;
  281. let profileChecked = allTabsData.profileName == profileName;
  282. menus.create({
  283. id: profileId,
  284. type: "radio",
  285. contexts: defaultContexts,
  286. title: profileName,
  287. checked: profileChecked,
  288. parentId: MENU_ID_SELECT_PROFILE
  289. });
  290. menusCheckedState.set(profileId, profileChecked);
  291. profileId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + profileIndex;
  292. profileChecked = Boolean(rule) && rule.profile == profileName;
  293. menus.create({
  294. id: profileId,
  295. type: "radio",
  296. contexts: defaultContexts,
  297. title: profileName,
  298. checked: profileChecked,
  299. parentId: MENU_ID_ASSOCIATE_WITH_PROFILE
  300. });
  301. menusCheckedState.set(profileId, profileChecked);
  302. profileIndexes.set(profileName, profileIndex);
  303. }
  304. });
  305. if (options.contextMenuEnabled) {
  306. menus.create({
  307. id: "separator-3",
  308. contexts: pageContextsEnabled,
  309. type: "separator"
  310. });
  311. }
  312. }
  313. menus.create({
  314. id: MENU_ID_AUTO_SAVE,
  315. contexts: defaultContexts,
  316. title: MENU_AUTOSAVE_MESSAGE
  317. });
  318. menus.create({
  319. id: MENU_ID_AUTO_SAVE_DISABLED,
  320. type: "radio",
  321. title: MENU_AUTOSAVE_DISABLED_MESSAGE,
  322. contexts: defaultContexts,
  323. checked: true,
  324. parentId: MENU_ID_AUTO_SAVE
  325. });
  326. menusCheckedState.set(MENU_ID_AUTO_SAVE_DISABLED, true);
  327. menus.create({
  328. id: MENU_ID_AUTO_SAVE_TAB,
  329. type: "radio",
  330. title: MENU_AUTOSAVE_TAB_MESSAGE,
  331. contexts: defaultContexts,
  332. checked: false,
  333. parentId: MENU_ID_AUTO_SAVE
  334. });
  335. menusCheckedState.set(MENU_ID_AUTO_SAVE_TAB, false);
  336. menus.create({
  337. id: MENU_ID_AUTO_SAVE_UNPINNED,
  338. type: "radio",
  339. title: MENU_AUTOSAVE_UNPINNED_TABS_MESSAGE,
  340. contexts: defaultContexts,
  341. checked: false,
  342. parentId: MENU_ID_AUTO_SAVE
  343. });
  344. menusCheckedState.set(MENU_ID_AUTO_SAVE_UNPINNED, false);
  345. menus.create({
  346. id: MENU_ID_AUTO_SAVE_ALL,
  347. type: "radio",
  348. title: MENU_AUTOSAVE_ALL_TABS_MESSAGE,
  349. contexts: defaultContexts,
  350. checked: false,
  351. parentId: MENU_ID_AUTO_SAVE
  352. });
  353. menusCheckedState.set(MENU_ID_AUTO_SAVE_ALL, false);
  354. menus.create({
  355. id: "separator-4",
  356. contexts: defaultContexts,
  357. type: "separator"
  358. });
  359. menus.create({
  360. id: MENU_ID_VIEW_PENDINGS,
  361. contexts: defaultContexts,
  362. title: MENU_VIEW_PENDINGS_MESSAGE
  363. });
  364. }
  365. menusCreated = true;
  366. if (pendingRefresh) {
  367. pendingRefresh = false;
  368. (await tabs.get({})).forEach(async tab => await refreshTab(tab));
  369. }
  370. }
  371. async function initialize() {
  372. if (BROWSER_MENUS_API_SUPPORTED) {
  373. createMenus();
  374. menus.onClicked.addListener(async (event, tab) => {
  375. if (event.menuItemId == MENU_ID_SAVE_PAGE) {
  376. if (event.linkUrl) {
  377. business.saveUrls([event.linkUrl]);
  378. } else {
  379. business.saveTabs([tab]);
  380. }
  381. }
  382. if (event.menuItemId == MENU_ID_EDIT_AND_SAVE_PAGE) {
  383. const allTabsData = await tabsData.get(tab.id);
  384. if (allTabsData[tab.id].savedPageDetected) {
  385. business.openEditor(tab);
  386. } else {
  387. if (event.linkUrl) {
  388. business.saveUrls([event.linkUrl], { openEditor: true });
  389. } else {
  390. business.saveTabs([tab], { openEditor: true });
  391. }
  392. }
  393. }
  394. if (event.menuItemId == MENU_ID_SAVE_SELECTED_LINKS) {
  395. business.saveSelectedLinks(tab);
  396. }
  397. if (event.menuItemId == MENU_ID_VIEW_PENDINGS) {
  398. await tabs.create({ active: true, url: "/extension/ui/pages/pendings.html" });
  399. }
  400. if (event.menuItemId == MENU_ID_SAVE_SELECTED) {
  401. business.saveTabs([tab], { selected: true });
  402. }
  403. if (event.menuItemId == MENU_ID_SAVE_FRAME) {
  404. business.saveTabs([tab], { frameId: event.frameId });
  405. }
  406. if (event.menuItemId == MENU_ID_SAVE_SELECTED_TABS || event.menuItemId == MENU_ID_BUTTON_SAVE_SELECTED_TABS) {
  407. const allTabs = await tabs.get({ currentWindow: true, highlighted: true });
  408. business.saveTabs(allTabs);
  409. }
  410. if (event.menuItemId == MENU_ID_SAVE_UNPINNED_TABS || event.menuItemId == MENU_ID_BUTTON_SAVE_UNPINNED_TABS) {
  411. const allTabs = await tabs.get({ currentWindow: true, pinned: false });
  412. business.saveTabs(allTabs);
  413. }
  414. if (event.menuItemId == MENU_ID_SAVE_ALL_TABS || event.menuItemId == MENU_ID_BUTTON_SAVE_ALL_TABS) {
  415. const allTabs = await tabs.get({ currentWindow: true });
  416. business.saveTabs(allTabs);
  417. }
  418. if (event.menuItemId == MENU_ID_AUTO_SAVE_TAB) {
  419. const allTabsData = await tabsData.get(tab.id);
  420. allTabsData[tab.id].autoSave = true;
  421. await tabsData.set(allTabsData);
  422. refreshExternalComponents(tab);
  423. }
  424. if (event.menuItemId == MENU_ID_AUTO_SAVE_DISABLED) {
  425. const allTabsData = await tabsData.get();
  426. Object.keys(allTabsData).forEach(tabId => {
  427. if (typeof allTabsData[tabId] == "object" && allTabsData[tabId].autoSave) {
  428. allTabsData[tabId].autoSave = false;
  429. }
  430. });
  431. allTabsData.autoSaveUnpinned = allTabsData.autoSaveAll = false;
  432. await tabsData.set(allTabsData);
  433. refreshExternalComponents(tab);
  434. }
  435. if (event.menuItemId == MENU_ID_AUTO_SAVE_ALL) {
  436. const allTabsData = await tabsData.get();
  437. allTabsData.autoSaveAll = event.checked;
  438. await tabsData.set(allTabsData);
  439. refreshExternalComponents(tab);
  440. }
  441. if (event.menuItemId == MENU_ID_AUTO_SAVE_UNPINNED) {
  442. const allTabsData = await tabsData.get();
  443. allTabsData.autoSaveUnpinned = event.checked;
  444. await tabsData.set(allTabsData);
  445. refreshExternalComponents(tab);
  446. }
  447. if (event.menuItemId.startsWith(MENU_ID_SAVE_WITH_PROFILE_PREFIX)) {
  448. const profiles = await config.getProfiles();
  449. const profileId = event.menuItemId.split(MENU_ID_SAVE_WITH_PROFILE_PREFIX)[1];
  450. let profileName;
  451. if (profileId == "default") {
  452. profileName = config.DEFAULT_PROFILE_NAME;
  453. } else {
  454. const profileIndex = Number(profileId);
  455. profileName = Object.keys(profiles)[profileIndex];
  456. }
  457. profiles[profileName].profileName = profileName;
  458. business.saveTabs([tab], profiles[profileName]);
  459. }
  460. if (event.menuItemId.startsWith(MENU_ID_SELECT_PROFILE_PREFIX)) {
  461. const [profiles, allTabsData] = await Promise.all([config.getProfiles(), tabsData.get()]);
  462. const profileId = event.menuItemId.split(MENU_ID_SELECT_PROFILE_PREFIX)[1];
  463. if (profileId == "default") {
  464. allTabsData.profileName = config.DEFAULT_PROFILE_NAME;
  465. } else {
  466. const profileIndex = Number(profileId);
  467. allTabsData.profileName = Object.keys(profiles)[profileIndex];
  468. }
  469. await tabsData.set(allTabsData);
  470. refreshExternalComponents(tab);
  471. }
  472. if (event.menuItemId.startsWith(MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX)) {
  473. const [profiles, rule] = await Promise.all([config.getProfiles(), config.getRule(tab.url, true)]);
  474. const profileId = event.menuItemId.split(MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX)[1];
  475. let profileName;
  476. if (profileId == "default") {
  477. profileName = config.DEFAULT_PROFILE_NAME;
  478. } else if (profileId == "current") {
  479. profileName = config.CURRENT_PROFILE_NAME;
  480. } else {
  481. const profileIndex = Number(profileId);
  482. profileName = Object.keys(profiles)[profileIndex];
  483. }
  484. if (rule) {
  485. await config.updateRule(rule.url, rule.url, profileName, profileName);
  486. } else {
  487. await updateTitleValue(MENU_ID_ASSOCIATE_WITH_PROFILE, MENU_UPDATE_RULE_MESSAGE);
  488. await config.addRule(new URL(tab.url).hostname, profileName, profileName);
  489. }
  490. }
  491. });
  492. if (menusCreated) {
  493. pendingRefresh = true;
  494. } else {
  495. (await tabs.get({})).forEach(async tab => await refreshTab(tab));
  496. }
  497. }
  498. }
  499. async function refreshExternalComponents(tab) {
  500. const allTabsData = await tabsData.get(tab.id);
  501. await autosave.refreshTabs();
  502. await button.refreshTab(tab);
  503. try {
  504. await browser.runtime.sendMessage({ method: "options.refresh", profileName: allTabsData.profileName });
  505. } catch (error) {
  506. // ignored
  507. }
  508. }
  509. async function refreshTab(tab) {
  510. if (BROWSER_MENUS_API_SUPPORTED && menusCreated) {
  511. const promises = [];
  512. const allTabsData = await tabsData.get(tab.id);
  513. if (allTabsData[tab.id].editorDetected) {
  514. updateAllVisibleValues(false);
  515. } else {
  516. updateAllVisibleValues(true);
  517. promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_DISABLED, !allTabsData[tab.id].autoSave));
  518. promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_TAB, allTabsData[tab.id].autoSave));
  519. promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_UNPINNED, Boolean(allTabsData.autoSaveUnpinned)));
  520. promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_ALL, Boolean(allTabsData.autoSaveAll)));
  521. if (tab && tab.url) {
  522. const options = await config.getOptions(tab.url);
  523. promises.push(updateVisibleValue(tab, options.contextMenuEnabled));
  524. promises.push(updateTitleValue(MENU_ID_EDIT_AND_SAVE_PAGE, allTabsData[tab.id].savedPageDetected ? MENU_EDIT_PAGE_MESSAGE : MENU_EDIT_AND_SAVE_PAGE_MESSAGE));
  525. promises.push(menus.update(MENU_ID_SAVE_SELECTED, { visible: !options.saveRawPage }));
  526. promises.push(menus.update(MENU_ID_EDIT_AND_SAVE_PAGE, { visible: !options.openEditor || allTabsData[tab.id].savedPageDetected }));
  527. let selectedEntryId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "default";
  528. let title = MENU_CREATE_DOMAIN_RULE_MESSAGE;
  529. const [profiles, rule] = await Promise.all([config.getProfiles(), config.getRule(tab.url)]);
  530. if (rule) {
  531. const profileIndex = profileIndexes.get(rule.profile);
  532. if (profileIndex) {
  533. selectedEntryId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + profileIndex;
  534. title = MENU_UPDATE_RULE_MESSAGE;
  535. }
  536. }
  537. if (Object.keys(profiles).length > 1) {
  538. Object.keys(profiles).forEach((profileName, profileIndex) => {
  539. if (profileName == config.DEFAULT_PROFILE_NAME) {
  540. promises.push(updateCheckedValue(MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "default", selectedEntryId == MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "default"));
  541. } else {
  542. promises.push(updateCheckedValue(MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + profileIndex, selectedEntryId == MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + profileIndex));
  543. }
  544. });
  545. promises.push(updateTitleValue(MENU_ID_ASSOCIATE_WITH_PROFILE, title));
  546. }
  547. }
  548. }
  549. await Promise.all(promises);
  550. }
  551. }
  552. async function updateAllVisibleValues(visible) {
  553. const lastVisibleState = allMenuVisibleState;
  554. allMenuVisibleState = visible;
  555. if (lastVisibleState === undefined || lastVisibleState != visible) {
  556. const promises = [];
  557. try {
  558. MENU_TOP_VISIBLE_ENTRIES.forEach(id => promises.push(menus.update(id, { visible })));
  559. await Promise.all(promises);
  560. } catch (error) {
  561. // ignored
  562. }
  563. }
  564. }
  565. async function updateVisibleValue(tab, visible) {
  566. const lastVisibleState = contextMenuVisibleState;
  567. contextMenuVisibleState = visible;
  568. if (lastVisibleState === undefined || lastVisibleState != visible) {
  569. await createMenus(tab);
  570. }
  571. }
  572. function updateTitleValue(id, title) {
  573. const lastTitleValue = menusTitleState.get(id);
  574. menusTitleState.set(id, title);
  575. if (lastTitleValue === undefined) {
  576. return menus.update(id, { title });
  577. } else if (lastTitleValue != title) {
  578. return menus.update(id, { title });
  579. }
  580. }
  581. async function updateCheckedValue(id, checked) {
  582. checked = Boolean(checked);
  583. menusCheckedState.set(id, checked);
  584. await menus.update(id, { checked });
  585. }