annotate background.js @ 29:b1b1f2737249 version-8

Release version 8
author Guido Berhoerster <guido+tab-mover@berhoerster.name>
date Mon, 01 Mar 2021 08:28:25 +0100
parents 8279a650da6b
children aaed574396b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
1 /*
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
2 * Copyright (C) 2018 Guido Berhoerster <guido+tab-mover@berhoerster.name>
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
3 *
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
4 * This Source Code Form is subject to the terms of the Mozilla Public
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
7 */
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
8
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
9 'use strict';
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
10
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
11 const ALLOWED_PROTOCOLS = new Set(['http:', 'https:', 'ftp:']);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
12 var windowMenuIds = [];
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
13 var lastMenuInstanceId = 0;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
14 var nextMenuInstanceId = 1;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
15
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
16 function createMenuItem(createProperties) {
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
17 return new Promise((resolve, reject) => {
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
18 let id = browser.menus.create(createProperties, () => {
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
19 if (browser.runtime.lastError) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
20 reject(browser.runtime.lastError);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
21 } else {
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
22 resolve(id);
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
23 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
24 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
25 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
26 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
27
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
28 async function moveTabs(tab, targetWindowId) {
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
29 // if the current tab is part of a highlighted group then move the whole
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
30 // group
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
31 let selectedTabs = (tab.highlighted) ? await browser.tabs.query({
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
32 highlighted: true,
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
33 windowId: tab.windowId
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
34 }) : [tab];
28
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
35 let activeTab = selectedTabs.find(tab => tab.active);
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
36 await browser.tabs.move(selectedTabs.map(selectedTab => selectedTab.id),
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
37 {windowId: targetWindowId, index: -1});
28
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
38
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
39 // mark the previously active tab active again before highlighting other
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
40 // tabs since this resets the selected tabs
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
41 await browser.tabs.update(activeTab.id, {active: true});
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
42 for (let tab of selectedTabs) {
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
43 if (tab.id !== activeTab.id) {
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
44 browser.tabs.update(tab.id, {active: false, highlighted: true});
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
45 }
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
46 }
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
47 }
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
48
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
49 async function reopenTabs(tab, targetWindowId) {
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
50 // if the current tab is part of a highlighted group then reopen the whole
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
51 // group
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
52 let selectedTabs = (tab.highlighted) ? await browser.tabs.query({
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
53 highlighted: true,
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
54 windowId: tab.windowId
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
55 }) : [tab];
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
56 // filter out privileged tabs which cannot be reopened
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
57 selectedTabs = selectedTabs.filter(selectedTab =>
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
58 ALLOWED_PROTOCOLS.has(new URL(selectedTab.url).protocol));
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
59 if (selectedTabs.length === 0) {
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
60 return;
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
61 }
28
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
62 let activeTab = selectedTabs.find(tab => tab.active);
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
63 // the actually active tab may have been filtered out above, fall back to
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
64 // the first highlighted one
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
65 if (typeof activeTab === 'undefined') {
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
66 activeTab = selectedTabs[0];
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
67 activeTab.active = true;
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
68 }
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
69
28
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
70 let newTabs = await Promise.all(selectedTabs.map(selectedTab => {
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
71 return browser.tabs.create({
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
72 url: selectedTab.url,
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
73 windowId: targetWindowId,
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
74 active: selectedTab.active
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
75 });
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
76 }));
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
77 // tabs can only be highlighted after they have been created
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
78 for (let tab of newTabs) {
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
79 if (!tab.active) {
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
80 browser.tabs.update(tab.id, {active: false, highlighted: true});
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
81 }
8279a650da6b Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 24
diff changeset
82 }
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
83 browser.tabs.remove(selectedTabs.map(selectedTab => selectedTab.id));
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
84 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
85
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
86 async function onMenuShown(info, tab) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
87 let menuInstanceId = nextMenuInstanceId++;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
88 lastMenuInstanceId = menuInstanceId;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
89 let targetWindows = await browser.windows.getAll({
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
90 populate: true,
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
91 windowTypes: ['normal']
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
92 });
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
93 let creatingMenus = [];
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
94 let moveMenuItems = 0;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
95 let reopenMenuItems = 0;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
96 for (let targetWindow of targetWindows) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
97 if (targetWindow.id === tab.windowId) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
98 // ignore active window
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
99 continue;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
100 }
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
101 if (tab.incognito === targetWindow.incognito) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
102 creatingMenus.push(createMenuItem({
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
103 onclick: (info, tab) => moveTabs(tab, targetWindow.id),
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
104 parentId: 'move-menu',
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
105 title: targetWindow.title
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
106 }));
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
107 moveMenuItems++;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
108 } else {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
109 creatingMenus.push(createMenuItem({
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
110 onclick: (info, tab) => reopenTabs(tab, targetWindow.id),
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
111 parentId: 'reopen-menu',
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
112 title: targetWindow.title
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
113 }));
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
114 reopenMenuItems++;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
115 }
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
116 }
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
117 let updatingMenus = [
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
118 browser.menus.update('move-menu', {enabled: moveMenuItems > 0}),
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
119 browser.menus.update('reopen-menu', {enabled: reopenMenuItems > 0})
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
120 ];
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
121 await Promise.all([...creatingMenus, ...updatingMenus]);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
122 let newWindowMenuIds = await Promise.all(creatingMenus);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
123 if (menuInstanceId !== lastMenuInstanceId) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
124 // menu has been closed and opened again, remove the items of this
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
125 // instance again
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
126 for (let menuId of newWindowMenuIds) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
127 browser.menus.remove(menuId);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
128 }
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
129 return;
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
130 }
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
131 windowMenuIds = newWindowMenuIds;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
132 browser.menus.refresh();
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
133 }
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
134
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
135 async function onMenuHidden() {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
136 lastMenuInstanceId = 0;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
137 browser.menus.update('move-menu', {enabled: false});
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
138 browser.menus.update('reopen-menu', {enabled: false});
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
139 for (let menuId of windowMenuIds) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
140 browser.menus.remove(menuId);
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
141 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
142 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
143
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
144 (async () => {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
145 await Promise.all([
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
146 // create submenus
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
147 createMenuItem({
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
148 id: 'move-menu',
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
149 title: browser.i18n.getMessage('moveToWindowMenu'),
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
150 enabled: false,
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
151 contexts: ['tab']
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
152 }),
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
153 createMenuItem({
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
154 id: 'reopen-menu',
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
155 title: browser.i18n.getMessage('reopenInWindowMenu'),
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
156 enabled: false,
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
157 contexts: ['tab']
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
158 })
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
159 ]);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
160 browser.menus.onShown.addListener(onMenuShown);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
161 browser.menus.onHidden.addListener(onMenuHidden);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
162 })();