@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.
48 lines (47 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Runtime = void 0;
/**
* A class that provides a type-safe wrapper around Chrome's runtime API.
* This class allows you to handle message passing between different parts of your extension,
* manage extension lifecycle events, and access extension information.
*/
class Runtime {
/**
* Sends a message to the extension's runtime.
* @param message - The message to send
* @returns A promise that resolves to the response from the message handler
* @template T - The type of the response
*/
static async sendMessage(message) {
return new Promise((resolve) => {
chrome.runtime.sendMessage(message, resolve);
});
}
/**
* Adds a listener for messages from the extension.
* @param callback - Function called when a message is received
* @returns A function that removes the listener when called
*/
static addMessageListener(callback) {
chrome.runtime.onMessage.addListener(callback);
return () => chrome.runtime.onMessage.removeListener(callback);
}
/**
* Adds a listener for messages from external extensions or apps.
* @param callback - Function called when an external message is received
* @returns A function that removes the listener when called
*/
static addExternalMessageListener(callback) {
chrome.runtime.onMessageExternal.addListener(callback);
return () => chrome.runtime.onMessageExternal.removeListener(callback);
}
/**
* Gets the manifest of the extension.
* @returns A promise that resolves to the extension's manifest
*/
static async getManifest() {
return chrome.runtime.getManifest();
}
}
exports.Runtime = Runtime;