Mercurial > addons > firefox-addons > tab-mover
annotate background.js @ 34:a989d7edee02 default tip
Added tag version-9 for changeset df8ade6f65c4
author | Guido Berhoerster <guido+tab-mover@berhoerster.name> |
---|---|
date | Thu, 08 Apr 2021 12:26:04 +0200 |
parents | aaed574396b8 |
children |
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); |
32
aaed574396b8
Move pinned tabs correctly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
28
diff
changeset
|
36 |
aaed574396b8
Move pinned tabs correctly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
28
diff
changeset
|
37 // unpin tabs before moving, this matches the built-in behavior |
aaed574396b8
Move pinned tabs correctly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
28
diff
changeset
|
38 let unpinningTabs = selectedTabs.flatMap(tab => |
aaed574396b8
Move pinned tabs correctly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
28
diff
changeset
|
39 tab.pinned ? [browser.tabs.update(tab.id, {pinned: false})] : []); |
aaed574396b8
Move pinned tabs correctly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
28
diff
changeset
|
40 await Promise.all(unpinningTabs.map(p => p.catch(e => e))); |
aaed574396b8
Move pinned tabs correctly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
28
diff
changeset
|
41 |
28
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
42 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
|
43 {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
|
44 |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
45 // 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
|
46 // 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
|
47 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
|
48 for (let tab of selectedTabs) { |
32
aaed574396b8
Move pinned tabs correctly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
28
diff
changeset
|
49 if (typeof activeTab === 'undefined' || tab.id !== activeTab.id) { |
28
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
50 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
|
51 } |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
52 } |
23
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
53 } |
0
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
54 |
24
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
55 async function reopenTabs(tab, targetWindowId) { |
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
56 // 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
|
57 // group |
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
58 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
|
59 highlighted: true, |
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
60 windowId: tab.windowId |
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
61 }) : [tab]; |
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
62 // 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
|
63 selectedTabs = selectedTabs.filter(selectedTab => |
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
64 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
|
65 if (selectedTabs.length === 0) { |
23
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
66 return; |
0
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
67 } |
28
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
68 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
|
69 // 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
|
70 // the first highlighted one |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
71 if (typeof activeTab === 'undefined') { |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
72 activeTab = selectedTabs[0]; |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
73 activeTab.active = true; |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
74 } |
24
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
75 |
28
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
76 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
|
77 return browser.tabs.create({ |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
78 url: selectedTab.url, |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
79 windowId: targetWindowId, |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
80 active: selectedTab.active |
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 })); |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
83 // 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
|
84 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
|
85 if (!tab.active) { |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
86 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
|
87 } |
8279a650da6b
Preserve active and highlighted properties of moved tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
24
diff
changeset
|
88 } |
24
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
89 browser.tabs.remove(selectedTabs.map(selectedTab => selectedTab.id)); |
0
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
90 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
91 |
23
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
92 async function onMenuShown(info, tab) { |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
93 let menuInstanceId = nextMenuInstanceId++; |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
94 lastMenuInstanceId = menuInstanceId; |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
95 let targetWindows = await browser.windows.getAll({ |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
96 populate: true, |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
97 windowTypes: ['normal'] |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
98 }); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
99 let creatingMenus = []; |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
100 let moveMenuItems = 0; |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
101 let reopenMenuItems = 0; |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
102 for (let targetWindow of targetWindows) { |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
103 if (targetWindow.id === tab.windowId) { |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
104 // ignore active window |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
105 continue; |
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 if (tab.incognito === targetWindow.incognito) { |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
108 creatingMenus.push(createMenuItem({ |
24
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
109 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
|
110 parentId: 'move-menu', |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
111 title: targetWindow.title |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
112 })); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
113 moveMenuItems++; |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
114 } else { |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
115 creatingMenus.push(createMenuItem({ |
24
f418a6305f17
Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
23
diff
changeset
|
116 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
|
117 parentId: 'reopen-menu', |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
118 title: targetWindow.title |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
119 })); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
120 reopenMenuItems++; |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
121 } |
0
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
122 } |
23
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
123 let updatingMenus = [ |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
124 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
|
125 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
|
126 ]; |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
127 await Promise.all([...creatingMenus, ...updatingMenus]); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
128 let newWindowMenuIds = await Promise.all(creatingMenus); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
129 if (menuInstanceId !== lastMenuInstanceId) { |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
130 // 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
|
131 // instance again |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
132 for (let menuId of newWindowMenuIds) { |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
133 browser.menus.remove(menuId); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
134 } |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
135 return; |
0
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
136 } |
23
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
137 windowMenuIds = newWindowMenuIds; |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
138 browser.menus.refresh(); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
139 } |
0
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
140 |
23
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
141 async function onMenuHidden() { |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
142 lastMenuInstanceId = 0; |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
143 browser.menus.update('move-menu', {enabled: false}); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
144 browser.menus.update('reopen-menu', {enabled: false}); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
145 for (let menuId of windowMenuIds) { |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
146 browser.menus.remove(menuId); |
0
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
147 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
148 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
149 |
23
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
150 (async () => { |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
151 await Promise.all([ |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
152 // create submenus |
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: 'move-menu', |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
155 title: browser.i18n.getMessage('moveToWindowMenu'), |
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 createMenuItem({ |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
160 id: 'reopen-menu', |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
161 title: browser.i18n.getMessage('reopenInWindowMenu'), |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
162 enabled: false, |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
163 contexts: ['tab'] |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
164 }) |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
165 ]); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
166 browser.menus.onShown.addListener(onMenuShown); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
167 browser.menus.onHidden.addListener(onMenuHidden); |
4704e5216412
Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
17
diff
changeset
|
168 })(); |