@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.
39 lines (38 loc) • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Scripting = void 0;
/**
* A class that provides a type-safe wrapper around Chrome's scripting API.
* This class allows you to execute JavaScript code in the context of web pages
* loaded in the browser. It's particularly useful for content scripts and
* page manipulations.
*/
class Scripting {
/**
* Executes a function in the context of a web page.
* @param tabId - The ID of the tab to execute the function in
* @param func - The function to execute
* @param args - Arguments to pass to the function
* @returns A promise that resolves to the result of the function execution
* @throws {Error} If there's an error executing the function
*/
static async executeFunction(tabId, func, ...args) {
console.log(`Executing function in tab ${tabId}:`, func.name || 'anonymous function');
console.log('Arguments:', args);
try {
const [injectionResult] = await chrome.scripting.executeScript({
target: { tabId },
func,
args,
world: 'MAIN',
});
console.log('Injection result:', injectionResult);
return injectionResult === null || injectionResult === void 0 ? void 0 : injectionResult.result;
}
catch (error) {
console.error('Error executing function via Scripting API:', error);
throw error;
}
}
}
exports.Scripting = Scripting;