Skip to content

Commit dd2788e

Browse files
authored
Merge pull request #2721 from antondudakov/antondudakov/reopen-shortcuts
Impls #1792 adds an ability to reopen current tab in the specific container with a shortcut
2 parents d432e31 + b630fd8 commit dd2788e

7 files changed

Lines changed: 151 additions & 23 deletions

File tree

src/_locales

src/js/background/backgroundLogic.js

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
/* global MAC_CONSTANTS */
6+
57
const DEFAULT_TAB = "about:newtab";
68

79
const backgroundLogic = {
@@ -11,22 +13,33 @@ const backgroundLogic = {
1113
"about:home",
1214
"about:blank"
1315
]),
14-
NUMBER_OF_KEYBOARD_SHORTCUTS: 10,
16+
NUMBER_OF_KEYBOARD_SHORTCUTS: MAC_CONSTANTS.NUMBER_OF_KEYBOARD_SHORTCUTS,
1517
unhideQueue: [],
16-
init() {
1718

18-
browser.commands.onCommand.addListener(function (command) {
19+
init() {
20+
browser.commands.onCommand.addListener(async function (command) {
1921
if (command === "sort_tabs") {
2022
backgroundLogic.sortTabs();
2123
return;
2224
}
2325

2426
for (let i=0; i < backgroundLogic.NUMBER_OF_KEYBOARD_SHORTCUTS; i++) {
25-
const key = "open_container_" + i;
26-
const cookieStoreId = identityState.keyboardShortcut[key];
27+
const key = MAC_CONSTANTS.OPEN_CONTAINER_PREFIX + i;
28+
const reopenKey = MAC_CONSTANTS.REOPEN_IN_CONTAINER_PREFIX + i;
2729
if (command === key) {
28-
if (cookieStoreId === "none") return;
29-
browser.tabs.create({cookieStoreId});
30+
const cookieStoreId = identityState.keyboardShortcut[key];
31+
if (cookieStoreId && cookieStoreId !== "none") {
32+
browser.tabs.create({cookieStoreId});
33+
}
34+
return;
35+
}
36+
37+
if (command === reopenKey) {
38+
const cookieStoreId = identityState.keyboardShortcut[reopenKey];
39+
if (cookieStoreId && cookieStoreId !== "none") {
40+
backgroundLogic.reopenInContainer(cookieStoreId);
41+
}
42+
return;
3043
}
3144
}
3245
});
@@ -69,6 +82,23 @@ const backgroundLogic = {
6982
}
7083
},
7184

85+
async reopenInContainer(cookieStoreId) {
86+
const currentTab = await browser.tabs.query({ active: true, currentWindow: true });
87+
88+
if (currentTab.length > 0) {
89+
const tab = currentTab[0];
90+
91+
browser.tabs.create({
92+
url: tab.url,
93+
cookieStoreId,
94+
index: tab.index + 1,
95+
active: tab.active
96+
});
97+
98+
browser.tabs.remove(tab.id);
99+
}
100+
},
101+
72102
/**
73103
* We left an achievement entry in storage during a user research study in
74104
* version 8.3.1. This method removes that entry to prevent broken logic in
@@ -84,11 +114,15 @@ const backgroundLogic = {
84114
},
85115

86116
updateTranslationInManifest() {
87-
for (let index = 0; index < 10; index++) {
88-
const ajustedIndex = index + 1; // We want to start from 1 instead of 0 in the UI.
117+
for (let index = 0; index < MAC_CONSTANTS.NUMBER_OF_KEYBOARD_SHORTCUTS; index++) {
118+
const adjustedIndex = index + 1; // We want to start from 1 instead of 0 in the UI.
119+
browser.commands.update({
120+
name: `${MAC_CONSTANTS.OPEN_CONTAINER_PREFIX}${index}`,
121+
description: browser.i18n.getMessage("containerShortcut", `${adjustedIndex}`)
122+
});
89123
browser.commands.update({
90-
name: `open_container_${index}`,
91-
description: browser.i18n.getMessage("containerShortcut", `${ajustedIndex}`)
124+
name: `${MAC_CONSTANTS.REOPEN_IN_CONTAINER_PREFIX}${index}`,
125+
description: browser.i18n.getMessage("reopenInContainerShortcut", `${adjustedIndex}`)
92126
});
93127
}
94128
},

src/js/background/constants.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
// Shared constants for background scripts
6+
window.MAC_CONSTANTS = {
7+
OPEN_CONTAINER_PREFIX: "open_container_",
8+
REOPEN_IN_CONTAINER_PREFIX: "reopen_in_container_",
9+
NUMBER_OF_KEYBOARD_SHORTCUTS: 10,
10+
};

src/js/background/identityState.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
/* global MAC_CONSTANTS */
16
window.identityState = {
27
keyboardShortcut: {},
38
storageArea: {
@@ -50,18 +55,22 @@ window.identityState = {
5055

5156
async loadKeyboardShortcuts () {
5257
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];
58-
continue;
59-
}
60-
if (identities[i]) {
61-
identityState.keyboardShortcut[key] = identities[i].cookieStoreId;
62-
continue;
58+
for (let i=0; i < MAC_CONSTANTS.NUMBER_OF_KEYBOARD_SHORTCUTS; i++) {
59+
const openKey = MAC_CONSTANTS.OPEN_CONTAINER_PREFIX + i;
60+
const reopenKey = MAC_CONSTANTS.REOPEN_IN_CONTAINER_PREFIX + i;
61+
62+
for (const key of [openKey, reopenKey]) {
63+
const storageObject = await this.area.get(key);
64+
65+
if (storageObject[key]){
66+
identityState.keyboardShortcut[key] = storageObject[key];
67+
} else if (identities[i]) {
68+
identityState.keyboardShortcut[key] = identities[i].cookieStoreId;
69+
} else {
70+
identityState.keyboardShortcut[key] = "none";
71+
}
6372
}
64-
identityState.keyboardShortcut[key] = "none";
73+
6574
}
6675
return identityState.keyboardShortcut;
6776
},

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,36 @@
109109
"default": "Ctrl+Shift+0"
110110
},
111111
"description": "__MSG_containerShortcut__"
112+
},
113+
"reopen_in_container_0": {
114+
"description": "__MSG_reopenInContainerShortcut__"
115+
},
116+
"reopen_in_container_1": {
117+
"description": "__MSG_reopenInContainerShortcut__"
118+
},
119+
"reopen_in_container_2": {
120+
"description": "__MSG_reopenInContainerShortcut__"
121+
},
122+
"reopen_in_container_3": {
123+
"description": "__MSG_reopenInContainerShortcut__"
124+
},
125+
"reopen_in_container_4": {
126+
"description": "__MSG_reopenInContainerShortcut__"
127+
},
128+
"reopen_in_container_5": {
129+
"description": "__MSG_reopenInContainerShortcut__"
130+
},
131+
"reopen_in_container_6": {
132+
"description": "__MSG_reopenInContainerShortcut__"
133+
},
134+
"reopen_in_container_7": {
135+
"description": "__MSG_reopenInContainerShortcut__"
136+
},
137+
"reopen_in_container_8": {
138+
"description": "__MSG_reopenInContainerShortcut__"
139+
},
140+
"reopen_in_container_9": {
141+
"description": "__MSG_reopenInContainerShortcut__"
112142
}
113143
},
114144
"browser_action": {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const {initializeWithTab} = require("../common");
2+
3+
describe("Reopen Shortcuts Feature", function () {
4+
beforeEach(async function () {
5+
// Initialize with a tab in the default container
6+
this.webExt = await initializeWithTab({
7+
cookieStoreId: "firefox-default",
8+
url: "https://example.com"
9+
});
10+
});
11+
12+
afterEach(function () {
13+
this.webExt.destroy();
14+
});
15+
16+
describe("when using keyboard shortcut to reopen in container", function () {
17+
beforeEach(async function () {
18+
// Simulate the keyboard shortcut command
19+
await this.webExt.background.browser.commands.onCommand.addListener.firstCall.args[0]("reopen_in_container_0");
20+
});
21+
22+
it("should open the page in the assigned container and close the original tab", async function () {
23+
this.webExt.background.browser.tabs.create.should.have.been.calledWithMatch({
24+
url: "https://example.com",
25+
cookieStoreId: "firefox-container-1",
26+
index: 1,
27+
active: true
28+
});
29+
30+
this.webExt.background.browser.tabs.remove.should.have.been.called;
31+
});
32+
});
33+
34+
describe("when container is set to 'none'", function () {
35+
beforeEach(async function () {
36+
await this.webExt.background.browser.commands.onCommand.addListener.firstCall.args[0]("reopen_in_container_9");
37+
});
38+
39+
it("should not reopen the tab", function () {
40+
this.webExt.background.browser.tabs.create.should.not.have.been.called;
41+
this.webExt.background.browser.tabs.remove.should.not.have.been.called;
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)