annotate background.js @ 25:fa504a211b26 version-7

Release version 7
author Guido Berhoerster <guido+tab-mover@berhoerster.name>
date Sun, 25 Nov 2018 14:04:26 +0100
parents f418a6305f17
children 8279a650da6b
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];
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
35 browser.tabs.move(selectedTabs.map(selectedTab => selectedTab.id),
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
36 {windowId: targetWindowId, index: -1});
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
37 }
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
38
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
39 async function reopenTabs(tab, targetWindowId) {
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
40 // 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
41 // group
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
42 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
43 highlighted: true,
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
44 windowId: tab.windowId
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
45 }) : [tab];
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
46 // 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
47 selectedTabs = selectedTabs.filter(selectedTab =>
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
48 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
49 if (selectedTabs.length === 0) {
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
50 return;
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
51 }
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
52
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
53 await Promise.all(selectedTabs.map(selectedTab => browser.tabs.create({
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
54 url: selectedTab.url,
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
55 windowId: targetWindowId
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
56 })));
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
57 browser.tabs.remove(selectedTabs.map(selectedTab => selectedTab.id));
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
58 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
59
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
60 async function onMenuShown(info, tab) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
61 let menuInstanceId = nextMenuInstanceId++;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
62 lastMenuInstanceId = menuInstanceId;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
63 let targetWindows = await browser.windows.getAll({
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
64 populate: true,
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
65 windowTypes: ['normal']
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
66 });
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
67 let creatingMenus = [];
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
68 let moveMenuItems = 0;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
69 let reopenMenuItems = 0;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
70 for (let targetWindow of targetWindows) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
71 if (targetWindow.id === tab.windowId) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
72 // ignore active window
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
73 continue;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
74 }
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
75 if (tab.incognito === targetWindow.incognito) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
76 creatingMenus.push(createMenuItem({
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
77 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
78 parentId: 'move-menu',
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
79 title: targetWindow.title
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
80 }));
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
81 moveMenuItems++;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
82 } else {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
83 creatingMenus.push(createMenuItem({
24
f418a6305f17 Allow moving multiple highlighted tabs
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 23
diff changeset
84 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
85 parentId: 'reopen-menu',
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
86 title: targetWindow.title
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
87 }));
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
88 reopenMenuItems++;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
89 }
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
90 }
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
91 let updatingMenus = [
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
92 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
93 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
94 ];
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
95 await Promise.all([...creatingMenus, ...updatingMenus]);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
96 let newWindowMenuIds = await Promise.all(creatingMenus);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
97 if (menuInstanceId !== lastMenuInstanceId) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
98 // 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
99 // instance again
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
100 for (let menuId of newWindowMenuIds) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
101 browser.menus.remove(menuId);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
102 }
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
103 return;
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
104 }
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
105 windowMenuIds = newWindowMenuIds;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
106 browser.menus.refresh();
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
107 }
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
108
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
109 async function onMenuHidden() {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
110 lastMenuInstanceId = 0;
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
111 browser.menus.update('move-menu', {enabled: false});
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
112 browser.menus.update('reopen-menu', {enabled: false});
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
113 for (let menuId of windowMenuIds) {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
114 browser.menus.remove(menuId);
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
115 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
116 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
117
23
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
118 (async () => {
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
119 await Promise.all([
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
120 // create submenus
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
121 createMenuItem({
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
122 id: 'move-menu',
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
123 title: browser.i18n.getMessage('moveToWindowMenu'),
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
124 enabled: false,
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
125 contexts: ['tab']
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 createMenuItem({
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
128 id: 'reopen-menu',
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
129 title: browser.i18n.getMessage('reopenInWindowMenu'),
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
130 enabled: false,
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
131 contexts: ['tab']
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
132 })
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
133 ]);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
134 browser.menus.onShown.addListener(onMenuShown);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
135 browser.menus.onHidden.addListener(onMenuHidden);
4704e5216412 Create menus on-the-fly
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 17
diff changeset
136 })();