Skip to content

Commit 04d52b7

Browse files
committed
address feedback
1 parent f54b6af commit 04d52b7

File tree

5 files changed

+60
-75
lines changed

5 files changed

+60
-75
lines changed

src/js/background/backgroundLogic.js

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* global MAC_CONSTANTS */
12
/* This Source Code Form is subject to the terms of the Mozilla Public
23
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
34
* You can obtain one at http://mozilla.org/MPL/2.0/. */
@@ -11,33 +12,35 @@ const backgroundLogic = {
1112
"about:home",
1213
"about:blank"
1314
]),
14-
NUMBER_OF_KEYBOARD_SHORTCUTS: 10,
15+
// Use shared constants for counts
16+
// NOTE: Keep in sync with MAC_CONSTANTS.NUMBER_OF_KEYBOARD_SHORTCUTS
1517
unhideQueue: [],
1618

1719
init() {
1820
browser.commands.onCommand.addListener(async function (command) {
1921
if (command === "sort_tabs") {
2022
backgroundLogic.sortTabs();
21-
return;
22-
}
23-
24-
for (let i=0; i < backgroundLogic.NUMBER_OF_KEYBOARD_SHORTCUTS; i++) {
25-
const key = "open_container_" + i;
26-
const reopenKey = "reopen_in_container_" + i;
27-
const cookieStoreId = identityState.keyboardShortcut[key];
28-
29-
if (cookieStoreId === "none") {
30-
continue;
23+
} else if (command.startsWith(MAC_CONSTANTS.OPEN_CONTAINER_PREFIX)) {
24+
for (let i = 0; i < MAC_CONSTANTS.NUMBER_OF_KEYBOARD_SHORTCUTS; i++) {
25+
const key = MAC_CONSTANTS.OPEN_CONTAINER_PREFIX + i;
26+
const cookieStoreId = identityState.keyboardShortcut[key];
27+
if (command === key) {
28+
if (cookieStoreId !== "none") {
29+
browser.tabs.create({cookieStoreId});
30+
}
31+
break;
32+
}
3133
}
32-
33-
if (command === key) {
34-
browser.tabs.create({cookieStoreId});
35-
return;
36-
}
37-
38-
if (command === reopenKey) {
39-
backgroundLogic.reopenInContainer(cookieStoreId);
40-
return;
34+
} else if (command.startsWith(MAC_CONSTANTS.REOPEN_IN_CONTAINER_PREFIX)) {
35+
for (let i = 0; i < MAC_CONSTANTS.NUMBER_OF_KEYBOARD_SHORTCUTS; i++) {
36+
const key = MAC_CONSTANTS.REOPEN_IN_CONTAINER_PREFIX + i;
37+
const cookieStoreId = identityState.keyboardShortcut[key];
38+
if (command === key) {
39+
if (cookieStoreId !== "none") {
40+
backgroundLogic.reopenInContainer(cookieStoreId);
41+
}
42+
break;
43+
}
4144
}
4245
}
4346
});
@@ -80,7 +83,7 @@ const backgroundLogic = {
8083
},
8184

8285
async reopenInContainer(cookieStoreId) {
83-
const currentTab = await browser.tabs.query({ active: true, currentWindow: true })
86+
const currentTab = await browser.tabs.query({active: true, currentWindow: true});
8487

8588
if (currentTab.length > 0) {
8689
const tab = currentTab[0];
@@ -97,14 +100,14 @@ const backgroundLogic = {
97100
},
98101

99102
updateTranslationInManifest() {
100-
for (let index = 0; index < 10; index++) {
103+
for (let index = 0; index < MAC_CONSTANTS.NUMBER_OF_KEYBOARD_SHORTCUTS; index++) {
101104
const adjustedIndex = index + 1; // We want to start from 1 instead of 0 in the UI.
102105
browser.commands.update({
103-
name: `open_container_${index}`,
106+
name: `${MAC_CONSTANTS.OPEN_CONTAINER_PREFIX}${index}`,
104107
description: browser.i18n.getMessage("containerShortcut", `${adjustedIndex}`)
105108
});
106109
browser.commands.update({
107-
name: `reopen_in_container_${index}`,
110+
name: `${MAC_CONSTANTS.REOPEN_IN_CONTAINER_PREFIX}${index}`,
108111
description: browser.i18n.getMessage("reopenInContainerShortcut", `${adjustedIndex}`)
109112
});
110113
}
@@ -216,9 +219,9 @@ const backgroundLogic = {
216219
const protocol = new URL(url).protocol;
217220
// We can't open these we just have to throw them away
218221
if (protocol === "about:"
219-
|| protocol === "chrome:"
220-
|| protocol === "moz-extension:"
221-
|| protocol === "file:") {
222+
|| protocol === "chrome:"
223+
|| protocol === "moz-extension:"
224+
|| protocol === "file:") {
222225
return false;
223226
}
224227
return true;
@@ -235,7 +238,7 @@ const backgroundLogic = {
235238
async getTabs(options) {
236239
const requiredArguments = ["cookieStoreId", "windowId"];
237240
this.checkArgs(requiredArguments, options, "getTabs");
238-
const { cookieStoreId, windowId } = options;
241+
const {cookieStoreId, windowId} = options;
239242

240243
const list = [];
241244
const tabs = await browser.tabs.query({
@@ -279,7 +282,7 @@ const backgroundLogic = {
279282
async moveTabsToWindow(options) {
280283
const requiredArguments = ["cookieStoreId", "windowId"];
281284
this.checkArgs(requiredArguments, options, "moveTabsToWindow");
282-
const { cookieStoreId, windowId } = options;
285+
const {cookieStoreId, windowId} = options;
283286

284287
const list = await browser.tabs.query({
285288
cookieStoreId,
@@ -290,7 +293,7 @@ const backgroundLogic = {
290293

291294
// Nothing to do
292295
if (list.length === 0 &&
293-
containerState.hiddenTabs.length === 0) {
296+
containerState.hiddenTabs.length === 0) {
294297
return;
295298
}
296299
let newWindowObj;
@@ -301,16 +304,15 @@ const backgroundLogic = {
301304
// Pin the default tab in the new window so existing pinned tabs can be moved after it.
302305
// From the docs (https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/move):
303306
// Note that you can't move pinned tabs to a position after any unpinned tabs in a window, or move any unpinned tabs to a position before any pinned tabs.
304-
await browser.tabs.update(newWindowObj.tabs[0].id, { pinned: true });
307+
await browser.tabs.update(newWindowObj.tabs[0].id, {pinned: true});
305308

306309
browser.tabs.move(list.map((tab) => tab.id), {
307310
windowId: newWindowObj.id,
308311
index: -1
309312
});
310313
} else {
311314
// As we get a blank tab here we will need to await the tabs creation
312-
newWindowObj = await browser.windows.create({
313-
});
315+
newWindowObj = await browser.windows.create({});
314316
hiddenDefaultTabToClose = true;
315317
}
316318

@@ -371,7 +373,7 @@ const backgroundLogic = {
371373
const identities = await browser.contextualIdentities.query({});
372374
const identitiesOutput = {};
373375
const identitiesPromise = identities.map(async (identity) => {
374-
const { cookieStoreId } = identity;
376+
const {cookieStoreId} = identity;
375377
const containerState = await identityState.storageArea.get(cookieStoreId);
376378
const openTabs = await browser.tabs.query({
377379
cookieStoreId,
@@ -430,7 +432,7 @@ const backgroundLogic = {
430432

431433
if (!map.has(tab.cookieStoreId)) {
432434
const userContextId = backgroundLogic.getUserContextIdFromCookieStoreId(tab.cookieStoreId);
433-
map.set(tab.cookieStoreId, { order: userContextId, tabs: [] });
435+
map.set(tab.cookieStoreId, {order: userContextId, tabs: []});
434436
}
435437
map.get(tab.cookieStoreId).tabs.push(tab);
436438
}
@@ -449,7 +451,7 @@ const backgroundLogic = {
449451
const sortMap = new Map([...map.entries()].sort((a, b) => a[1].order > b[1].order));
450452

451453
// Let's move tabs.
452-
for (const { tabs } of sortMap.values()) {
454+
for (const {tabs} of sortMap.values()) {
453455
for (const tab of tabs) {
454456
++pos;
455457
browser.tabs.move(tab.id, {
@@ -473,7 +475,7 @@ const backgroundLogic = {
473475
async hideTabs(options) {
474476
const requiredArguments = ["cookieStoreId", "windowId"];
475477
this.checkArgs(requiredArguments, options, "hideTabs");
476-
const { cookieStoreId, windowId } = options;
478+
const {cookieStoreId, windowId} = options;
477479

478480
const userContextId = backgroundLogic.getUserContextIdFromCookieStoreId(cookieStoreId);
479481

@@ -513,7 +515,7 @@ const backgroundLogic = {
513515
},
514516

515517
cookieStoreId(userContextId) {
516-
if(userContextId === 0) return "firefox-default";
518+
if (userContextId === 0) return "firefox-default";
517519
return `firefox-container-${userContextId}`;
518520
}
519521
};

src/js/background/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Shared constants for background scripts
2+
window.MAC_CONSTANTS = {
3+
OPEN_CONTAINER_PREFIX: "open_container_",
4+
REOPEN_IN_CONTAINER_PREFIX: "reopen_in_container_",
5+
NUMBER_OF_KEYBOARD_SHORTCUTS: 10,
6+
};

src/js/background/identityState.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* global MAC_CONSTANTS */
12
window.identityState = {
23
keyboardShortcut: {},
34
storageArea: {
@@ -50,18 +51,23 @@ window.identityState = {
5051

5152
async loadKeyboardShortcuts () {
5253
const identities = await browser.contextualIdentities.query({});
53-
for (let i=0; i < backgroundLogic.NUMBER_OF_KEYBOARD_SHORTCUTS; i++) {
54-
const key = "open_container_" + i;
55-
const storageObject = await this.area.get(key);
56-
if (storageObject[key]){
57-
identityState.keyboardShortcut[key] = storageObject[key];
54+
for (let i=0; i < MAC_CONSTANTS.NUMBER_OF_KEYBOARD_SHORTCUTS; i++) {
55+
const openKey = MAC_CONSTANTS.OPEN_CONTAINER_PREFIX + i;
56+
const reopenKey = MAC_CONSTANTS.REOPEN_IN_CONTAINER_PREFIX + i;
57+
const storageObject = await this.area.get(openKey);
58+
59+
if (storageObject[openKey]){
60+
identityState.keyboardShortcut[openKey] = storageObject[openKey];
61+
identityState.keyboardShortcut[reopenKey] = storageObject[openKey];
5862
continue;
5963
}
6064
if (identities[i]) {
61-
identityState.keyboardShortcut[key] = identities[i].cookieStoreId;
65+
identityState.keyboardShortcut[openKey] = identities[i].cookieStoreId;
66+
identityState.keyboardShortcut[reopenKey] = identities[i].cookieStoreId;
6267
continue;
6368
}
64-
identityState.keyboardShortcut[key] = "none";
69+
identityState.keyboardShortcut[openKey] = "none";
70+
identityState.keyboardShortcut[reopenKey] = "none";
6571
}
6672
return identityState.keyboardShortcut;
6773
},

src/js/background/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
-->
1616
<script type="text/javascript" src="../utils.js"></script>
1717
<script type="text/javascript" src="../proxified-containers.js"></script>
18+
<script type="text/javascript" src="constants.js"></script>
1819
<script type="text/javascript" src="backgroundLogic.js"></script>
1920
<script type="text/javascript" src="mozillaVpnBackground.js"></script>
2021
<script type="text/javascript" src="assignManager.js"></script>

src/manifest.json

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -108,63 +108,33 @@
108108
"description": "__MSG_containerShortcut__"
109109
},
110110
"reopen_in_container_0": {
111-
"suggested_key": {
112-
"default": "Alt+Shift+1"
113-
},
114111
"description": "__MSG_reopenInContainerShortcut__"
115112
},
116113
"reopen_in_container_1": {
117-
"suggested_key": {
118-
"default": "Alt+Shift+2"
119-
},
120114
"description": "__MSG_reopenInContainerShortcut__"
121115
},
122116
"reopen_in_container_2": {
123-
"suggested_key": {
124-
"default": "Alt+Shift+3"
125-
},
126117
"description": "__MSG_reopenInContainerShortcut__"
127118
},
128119
"reopen_in_container_3": {
129-
"suggested_key": {
130-
"default": "Alt+Shift+4"
131-
},
132120
"description": "__MSG_reopenInContainerShortcut__"
133121
},
134122
"reopen_in_container_4": {
135-
"suggested_key": {
136-
"default": "Alt+Shift+5"
137-
},
138123
"description": "__MSG_reopenInContainerShortcut__"
139124
},
140125
"reopen_in_container_5": {
141-
"suggested_key": {
142-
"default": "Alt+Shift+6"
143-
},
144126
"description": "__MSG_reopenInContainerShortcut__"
145127
},
146128
"reopen_in_container_6": {
147-
"suggested_key": {
148-
"default": "Alt+Shift+7"
149-
},
150129
"description": "__MSG_reopenInContainerShortcut__"
151130
},
152131
"reopen_in_container_7": {
153-
"suggested_key": {
154-
"default": "Alt+Shift+8"
155-
},
156132
"description": "__MSG_reopenInContainerShortcut__"
157133
},
158134
"reopen_in_container_8": {
159-
"suggested_key": {
160-
"default": "Alt+Shift+9"
161-
},
162135
"description": "__MSG_reopenInContainerShortcut__"
163136
},
164137
"reopen_in_container_9": {
165-
"suggested_key": {
166-
"default": "Alt+Shift+0"
167-
},
168138
"description": "__MSG_reopenInContainerShortcut__"
169139
}
170140
},

0 commit comments

Comments
 (0)