Und noch eine Version zur Auswahl, mit drei Einträgen.

JavaScript
// ==UserScript==
// @name KontextCloseTabs.uc.js
// @description TabContextMenu = Linke, Rechte und Alle Tabs schließen
// Alle Tabs schließen mit Warnhinweis
// @compatibility Firefox 154+
// @include main
// @charset UTF-8
// @version 2026.08.20
// ==/UserScript==
(function () {
"use strict";
if (location.href !== "chrome://browser/content/browser.xhtml")
return;
const tabContextMenu = document.getElementById("tabContextMenu");
const undoCloseTab = document.getElementById("context_undoCloseTab");
if (!tabContextMenu || !undoCloseTab)
return;
// ------------------------------------------------------------
// Menüpunkt erzeugen (XUL)
// ------------------------------------------------------------
function createMenuItem(id, label) {
let item = document.getElementById(id);
if (!item) {
item = document.createXULElement("menuitem");
item.id = id;
item.setAttribute("label", label);
// role/class kann helfen, dass das Item korrekt dargestellt wird
item.className = "menuitem";
}
return item;
}
// ------------------------------------------------------------
// Linke Tabs schließen
// ------------------------------------------------------------
const closeLeft = createMenuItem(
"context_closeLeftTabs",
"Linke Tabs schließen"
);
closeLeft.addEventListener("command", function () {
const contextTab = TabContextMenu.contextTab;
if (!contextTab)
return;
const tabs = Array.from(gBrowser.tabs);
for (const tab of tabs) {
if (tab === contextTab)
break;
if (!tab.pinned)
gBrowser.removeTab(tab, { animate: true });
}
});
// ------------------------------------------------------------
// Rechte Tabs schließen
// ------------------------------------------------------------
const closeRight = createMenuItem(
"context_closeRightTabs",
"Rechte Tabs schließen"
);
closeRight.addEventListener("command", function () {
const contextTab = TabContextMenu.contextTab;
if (!contextTab)
return;
const tabs = Array.from(gBrowser.tabs);
const index = tabs.indexOf(contextTab);
if (index === -1)
return;
for (let i = tabs.length - 1; i > index; i--) {
const tab = tabs[i];
if (!tab.pinned)
gBrowser.removeTab(tab, { animate: true });
}
});
// ------------------------------------------------------------
// Alle Tabs schließen (mit Bestätigung)
// ------------------------------------------------------------
const closeAll = createMenuItem(
"context_closeAllTabsCustom",
"Alle Tabs schließen…"
);
closeAll.addEventListener("command", function () {
// Bestätigung (einfacher confirm)
const msg = "Wirklich alle Tabs schließen? Diese Aktion kann nicht rückgängig gemacht werden.";
if (!confirm(msg))
return;
const tabs = Array.from(gBrowser.tabs);
// Schließe alle nicht-gepinnten Tabs von rechts nach links
for (let i = tabs.length - 1; i >= 0; i--) {
const tab = tabs[i];
if (!tab.pinned) {
try {
gBrowser.removeTab(tab, { animate: true });
} catch (e) {
// ignore
}
}
}
});
// ------------------------------------------------------------
// Menü einfügen
// ------------------------------------------------------------
// Falls Items bereits existieren, werden sie wiederverwendet; sonst eingefügt
tabContextMenu.insertBefore(closeLeft, undoCloseTab);
tabContextMenu.insertBefore(closeRight, undoCloseTab);
tabContextMenu.insertBefore(closeAll, undoCloseTab);
// ------------------------------------------------------------
// Menü beim Öffnen aktualisieren (aktiv/disabled)
// ------------------------------------------------------------
tabContextMenu.addEventListener("popupshowing", function () {
const contextTab = TabContextMenu.contextTab;
if (!contextTab) {
// Wenn kein Kontext-Tab, die Einträge verstecken (sicherer)
closeLeft.hidden = true;
closeRight.hidden = true;
closeAll.hidden = true;
return;
}
closeLeft.hidden = false;
closeRight.hidden = false;
closeAll.hidden = false;
const tabs = Array.from(gBrowser.tabs);
const index = tabs.indexOf(contextTab);
if (index === -1)
return;
let hasLeft = false;
let hasRight = false;
let hasAnyCloseable = false;
for (let i = 0; i < tabs.length; i++) {
const tab = tabs[i];
if (tab.pinned)
continue;
if (i < index)
hasLeft = true;
if (i > index)
hasRight = true;
hasAnyCloseable = true;
}
closeLeft.disabled = !hasLeft;
closeRight.disabled = !hasRight;
// closeAll ist aktiviert, wenn es mindestens einen schließbaren Tab gibt
closeAll.disabled = !hasAnyCloseable;
});
})();
Alles anzeigen

