From 1e0fdf43a3b48499b8cb9b3b97021ee58a839a9c Mon Sep 17 00:00:00 2001 From: Daniel Caixinha <2683004+dcaixinha@users.noreply.github.com> Date: Tue, 12 Dec 2023 11:09:13 +0000 Subject: [PATCH] Make `tabgrab` and `tabpush` move to next tabgrab Now `tabgrab` and `tabpush` will move the tab next to the current tab, except if the `tabopenpos` setting is set to `"last"`. In order to make `tabpush` work, I had to create an additional function to get the active tab for a given window ID. --- src/excmds.ts | 21 ++++++++++++++++++--- src/lib/webext.ts | 9 +++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/excmds.ts b/src/excmds.ts index 37b138e3..bbe2cd8f 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -74,7 +74,7 @@ // Shared import * as Messaging from "@src/lib/messaging" -import { ownWinTriIndex, getTriVersion, browserBg, activeTab, activeTabId, activeTabContainerId, openInNewTab, openInNewWindow, openInTab, queryAndURLwrangler, goToTab, getSortedTabs, prevActiveTab } from "@src/lib/webext" +import { ownWinTriIndex, getTriVersion, browserBg, activeTab, activeTabOnWindow, activeTabId, activeTabContainerId, openInNewTab, openInNewWindow, openInTab, queryAndURLwrangler, goToTab, getSortedTabs, prevActiveTab } from "@src/lib/webext" import * as Container from "@src/lib/containers" import state from "@src/state" import * as State from "@src/state" @@ -2639,7 +2639,15 @@ export async function tabpush(windowId?: number) { windows.sort((w1, w2) => w1.id - w2.id) const nextWindow = windows[(windows.findIndex(window => window.id === currentWindow.id) + 1) % windows.length] const tabId = await activeTabId() - return browser.tabs.move(tabId, { index: -1, windowId: windowId ?? nextWindow.id }) + const winId = windowId ?? nextWindow.id + const pos = await config.getAsync("tabopenpos") + switch (pos) { + case "last": + return browser.tabs.move(tabId, { index: -1, windowId: winId }) + default: + let index = (await activeTabOnWindow(winId)).index + 1 + return browser.tabs.move(tabId, { index: index, windowId: winId }) + } } /** Switch to the tab currently playing audio, if any. */ @@ -2699,7 +2707,14 @@ export async function tabgrab(id: string) { // Figure out where it should be put const windowId = (await browser.windows.getLastFocused({ windowTypes: ["normal"] })).id // Move window - return browser.tabs.move(tabid, { index: -1, windowId }) + const pos = await config.getAsync("tabopenpos") + switch (pos) { + case "last": + return browser.tabs.move(tabid, { index: -1, windowId }) + default: + let index = (await activeTab()).index + 1 + return browser.tabs.move(tabid, { index: index, windowId }) + } } /** Like [[open]], but in a new tab. If no address is given, it will open the newtab page, which can be set with `set newtab [url]` diff --git a/src/lib/webext.ts b/src/lib/webext.ts index b4282871..bce70268 100644 --- a/src/lib/webext.ts +++ b/src/lib/webext.ts @@ -84,6 +84,15 @@ export async function activeTab() { )[0] } +export async function activeTabOnWindow(windowId?: number) { + return ( + await browser.tabs.query({ + windowId: windowId, + active: true + }) + )[0] +} + export async function activeTabId() { return (await activeTab()).id }