@mirawision/chrome-api
Version:
A comprehensive TypeScript library for Chrome Extension API, providing type-safe wrappers and utilities for bookmarks, commands, context menus, cookies, downloads, storage, notifications, runtime, scripting, and side panel functionalities.
52 lines (51 loc) • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tabs = void 0;
class Tabs {
static async getAll() {
return new Promise((resolve) => {
chrome.tabs.query({}, (tabs) => {
resolve(tabs);
});
});
}
static async getCurrent() {
return new Promise((resolve, reject) => {
chrome.tabs.getCurrent((tab) => {
if (!tab) {
return reject(new Error('No current tab context'));
}
resolve(tab);
});
});
}
static async getActive() {
return new Promise((resolve) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
resolve(tabs[0]);
});
});
}
static async sendMessageToTab(tabId, message) {
return new Promise((resolve) => {
chrome.tabs.sendMessage(tabId, message, resolve);
});
}
static async sendMessageToActiveTab(message) {
const tab = await Tabs.getActive();
if (!tab.id) {
throw new Error('No active tab found');
}
return Tabs.sendMessageToTab(tab.id, message);
}
static async sendMessageToAllTabs(message) {
const tabs = await Tabs.getAll();
return Promise.all(tabs.map((tab) => {
if (!tab.id) {
throw new Error('No tab id found');
}
return Tabs.sendMessageToTab(tab.id, message);
}));
}
}
exports.Tabs = Tabs;