<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="tabs.css" />
</head>
<body>
<div class="panel">
<div class="panel-section panel-section-header">
<div class="text-section-header">Tabs-tabs-tabs</div>
</div>
<a href="#" id="tabs-move-beginning">
Move active tab to the beginning of the window
</a>
<br />
<!-- Define the other menu items -->
<div class="switch-tabs">
<p>Switch to tab</p>
<div id="tabs-list"></div>
</div>
</div>
<script src="tabs.js"></script>
</body>
</html>Work with the Tabs API / notebook
Work with the Tabs API
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
document.addEventListener("DOMContentLoaded", listTabs);function getCurrentWindowTabs() {
return browser.tabs.query({ currentWindow: true });
}function listTabs() {
getCurrentWindowTabs().then((tabs) => {
const tabsList = document.getElementById("tabs-list");
const currentTabs = document.createDocumentFragment();
const limit = 5;
let counter = 0;
tabsList.textContent = "";
// ...
});
}function listTabs() {
getCurrentWindowTabs().then((tabs) => {
// ...
for (const tab of tabs) {
if (!tab.active && counter <= limit) {
const tabLink = document.createElement("a");
tabLink.textContent = tab.title || tab.id;
tabLink.setAttribute("href", tab.id);
tabLink.classList.add("switch-tabs");
currentTabs.appendChild(tabLink);
}
counter += 1;
}
// ...
});
}function listTabs() {
getCurrentWindowTabs().then((tabs) => {
// ...
tabsList.appendChild(currentTabs);
});
}// Other if conditions...
if (e.target.id === "tabs-alert-info") {
callOnActiveTab((tab) => {
let props = "";
for (const item in tab) {
props += `${item} = ${tab[item]} \n`;
}
alert(props);
});
}document.addEventListener("click", (e) => {
function callOnActiveTab(callback) {
getCurrentWindowTabs().then((tabs) => {
for (const tab of tabs) {
if (tab.active) {
callback(tab, tabs);
}
}
});
}
}); <a href="#" id="tabs-move-beginning">
Move active tab to the beginning of the window
</a>
<br />
<a href="#" id="tabs-move-end">Move active tab to the end of the window</a>
<br />
<div class="panel-section-separator"></div>
<a href="#" id="tabs-duplicate">Duplicate active tab</a><br />
<a href="#" id="tabs-reload">Reload active tab</a><br />
<a href="#" id="tabs-alert-info">Alert active tab info</a><br /> document.addEventListener("click", (e) => {
function callOnActiveTab(callback) {
getCurrentWindowTabs().then((tabs) => {
for (const tab of tabs) {
if (tab.active) {
callback(tab, tabs);
}
}
});
}
}); if (e.target.id === "tabs-move-beginning") {
callOnActiveTab((tab, tabs) => {
let index = 0;
if (!tab.pinned) {
index = firstUnpinnedTab(tabs);
}
console.log(`moving ${tab.id} to ${index}`);
browser.tabs.move([tab.id], { index });
});
} function callOnActiveTab(callback) {
getCurrentWindowTabs().then((tabs) => {
for (const tab of tabs) {
if (tab.active) {
callback(tab, tabs);
}
}
});
}function firstUnpinnedTab(tabs) {
for (const tab of tabs) {
if (!tab.pinned) {
return tab.index;
}
}
}browser.tabs.move([tab.id], { index }); const ZOOM_INCREMENT = 0.2;
const MAX_ZOOM = 5;
const MIN_ZOOM = 0.3;
const DEFAULT_ZOOM = 1; // Other if conditions...
if (e.target.id === "tabs-add-zoom") {
callOnActiveTab((tab) => {
browser.tabs.getZoom(tab.id).then((zoomFactor) => {
// The maximum zoomFactor is 5, it can't go higher
if (zoomFactor >= MAX_ZOOM) {
alert("Tab zoom factor is already at max!");
} else {
let newZoomFactor = zoomFactor + ZOOM_INCREMENT;
// If the newZoomFactor is set to higher than the max accepted
// it won't change, and does not alert that it's at maximum
newZoomFactor = newZoomFactor > MAX_ZOOM ? MAX_ZOOM : newZoomFactor;
browser.tabs.setZoom(tab.id, newZoomFactor);
}
});
});
} const CSS = "body { border: 20px solid red; }";
const TITLE_APPLY = "Apply CSS";
const TITLE_REMOVE = "Remove CSS";
const APPLICABLE_PROTOCOLS = ["http:", "https:"]; browser.tabs.query({}).then((tabs) => {
for (const tab of tabs) {
initializePageAction(tab);
}
}); function protocolIsApplicable(url) {
const anchor = document.createElement("a");
anchor.href = url;
return APPLICABLE_PROTOCOLS.includes(anchor.protocol);
} function initializePageAction(tab) {
if (protocolIsApplicable(tab.url)) {
browser.pageAction.setIcon({ tabId: tab.id, path: "icons/off.svg" });
browser.pageAction.setTitle({ tabId: tab.id, title: TITLE_APPLY });
browser.pageAction.show(tab.id);
}
} browser.pageAction.onClicked.addListener(toggleCSS); function toggleCSS(tab) {
function gotTitle(title) {
if (title === TITLE_APPLY) {
browser.pageAction.setIcon({ tabId: tab.id, path: "icons/on.svg" });
browser.pageAction.setTitle({ tabId: tab.id, title: TITLE_REMOVE });
browser.tabs.insertCSS({ code: CSS });
} else {
browser.pageAction.setIcon({ tabId: tab.id, path: "icons/off.svg" });
browser.pageAction.setTitle({ tabId: tab.id, title: TITLE_APPLY });
browser.tabs.removeCSS({ code: CSS });
}
}
browser.pageAction.getTitle({ tabId: tab.id }).then(gotTitle);
} browser.tabs.onUpdated.addListener((id, changeInfo, tab) => {
initializePageAction(tab);
});