Skip to content

adds an ability to reopen current tab in the specific container with a shortcut #2721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/builds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
./bin/build-addon.sh nightly.xpi

- name: Uploading
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{matrix.config.name}} Build
path: src/web-ext-artifacts
28 changes: 10 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 32 additions & 5 deletions src/js/background/backgroundLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,43 @@ const backgroundLogic = {
]),
NUMBER_OF_KEYBOARD_SHORTCUTS: 10,
unhideQueue: [],
init() {

browser.commands.onCommand.addListener(function (command) {
async init() {
browser.commands.onCommand.addListener(async function (command) {
if (command === "sort_tabs") {
backgroundLogic.sortTabs();
return;
}

for (let i=0; i < backgroundLogic.NUMBER_OF_KEYBOARD_SHORTCUTS; i++) {
const key = "open_container_" + i;
const reopenKey = "reopen_in_container_" + i;
const cookieStoreId = identityState.keyboardShortcut[key];

if (cookieStoreId === "none") {
continue;
}

if (command === key) {
if (cookieStoreId === "none") return;
browser.tabs.create({cookieStoreId});
return;
}

if (command === reopenKey) {
const currentTab = await browser.tabs.query({active: true, currentWindow: true});
if (currentTab.length > 0) {
const tab = currentTab[0];

await browser.tabs.create({
url: tab.url,
cookieStoreId: cookieStoreId,
index: tab.index + 1,
active: tab.active
});

await browser.tabs.remove(tab.id);
}
return;
}
}
});
Expand All @@ -41,10 +64,14 @@ const backgroundLogic = {

updateTranslationInManifest() {
for (let index = 0; index < 10; index++) {
const ajustedIndex = index + 1; // We want to start from 1 instead of 0 in the UI.
const adjustedIndex = index + 1;
browser.commands.update({
name: `open_container_${index}`,
description: browser.i18n.getMessage("containerShortcut", `${ajustedIndex}`)
description: browser.i18n.getMessage("containerShortcut", `${adjustedIndex}`)
});
browser.commands.update({
name: `reopen_in_container_${index}`,
description: browser.i18n.getMessage("reopenInContainerShortcut", `${adjustedIndex}`)
});
}
},
Expand Down
60 changes: 60 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,66 @@
"default": "Ctrl+Shift+0"
},
"description": "__MSG_containerShortcut__"
},
"reopen_in_container_0": {
"suggested_key": {
"default": "Alt+Shift+1"
},
"description": "__MSG_reopenInContainerShortcut__"
},
"reopen_in_container_1": {
"suggested_key": {
"default": "Alt+Shift+2"
},
"description": "__MSG_reopenInContainerShortcut__"
},
"reopen_in_container_2": {
"suggested_key": {
"default": "Alt+Shift+3"
},
"description": "__MSG_reopenInContainerShortcut__"
},
"reopen_in_container_3": {
"suggested_key": {
"default": "Alt+Shift+4"
},
"description": "__MSG_reopenInContainerShortcut__"
},
"reopen_in_container_4": {
"suggested_key": {
"default": "Alt+Shift+5"
},
"description": "__MSG_reopenInContainerShortcut__"
},
"reopen_in_container_5": {
"suggested_key": {
"default": "Alt+Shift+6"
},
"description": "__MSG_reopenInContainerShortcut__"
},
"reopen_in_container_6": {
"suggested_key": {
"default": "Alt+Shift+7"
},
"description": "__MSG_reopenInContainerShortcut__"
},
"reopen_in_container_7": {
"suggested_key": {
"default": "Alt+Shift+8"
},
"description": "__MSG_reopenInContainerShortcut__"
},
"reopen_in_container_8": {
"suggested_key": {
"default": "Alt+Shift+9"
},
"description": "__MSG_reopenInContainerShortcut__"
},
"reopen_in_container_9": {
"suggested_key": {
"default": "Alt+Shift+0"
},
"description": "__MSG_reopenInContainerShortcut__"
}
},
"browser_action": {
Expand Down
44 changes: 44 additions & 0 deletions test/features/reopen-shortcuts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const {initializeWithTab} = require("../common");

describe("Reopen Shortcuts Feature", function () {
beforeEach(async function () {
// Initialize with a tab in the default container
this.webExt = await initializeWithTab({
cookieStoreId: "firefox-default",
url: "https://example.com"
});
});

afterEach(function () {
this.webExt.destroy();
});

describe("when using keyboard shortcut to reopen in container", function () {
beforeEach(async function () {
// Simulate the keyboard shortcut command
await this.webExt.background.browser.commands.onCommand.addListener.firstCall.args[0]("reopen_in_container_0");
});

it("should open the page in the assigned container and close the original tab", async function () {
this.webExt.background.browser.tabs.create.should.have.been.calledWithMatch({
url: "https://example.com",
cookieStoreId: "firefox-container-1",
index: 1,
active: true
});

this.webExt.background.browser.tabs.remove.should.have.been.called;
});
});

describe("when container is set to 'none'", function () {
beforeEach(async function () {
await this.webExt.background.browser.commands.onCommand.addListener.firstCall.args[0]("reopen_in_container_9");
});

it("should not reopen the tab", function () {
this.webExt.background.browser.tabs.create.should.not.have.been.called;
this.webExt.background.browser.tabs.remove.should.not.have.been.called;
});
});
});