From b4d0115d22bf00d9754a819a9ff4b2e70f147947 Mon Sep 17 00:00:00 2001 From: Anton Dudakov Date: Fri, 21 Feb 2025 16:19:06 +0400 Subject: [PATCH] adds an ability to reopen current tab in the specific container with a shortcut --- src/js/background/backgroundLogic.js | 37 +++++++++++++--- src/manifest.json | 60 ++++++++++++++++++++++++++ test/features/reopen-shortcuts.test.js | 44 +++++++++++++++++++ 3 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 test/features/reopen-shortcuts.test.js diff --git a/src/js/background/backgroundLogic.js b/src/js/background/backgroundLogic.js index fb90ba6..4ad3a98 100644 --- a/src/js/background/backgroundLogic.js +++ b/src/js/background/backgroundLogic.js @@ -13,9 +13,9 @@ 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; @@ -23,10 +23,33 @@ const backgroundLogic = { 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; } } }); @@ -85,10 +108,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}`) }); } }, diff --git a/src/manifest.json b/src/manifest.json index 94de8ed..60c7c64 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -109,6 +109,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": { diff --git a/test/features/reopen-shortcuts.test.js b/test/features/reopen-shortcuts.test.js new file mode 100644 index 0000000..ac3781c --- /dev/null +++ b/test/features/reopen-shortcuts.test.js @@ -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; + }); + }); +}); \ No newline at end of file