UNPKG

@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.

223 lines (222 loc) 8.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Bookmarks = void 0; /** * A class that provides a type-safe wrapper around Chrome's bookmarks API. * This class offers methods to create, read, update, and delete bookmarks, * as well as event listeners for bookmark changes. */ class Bookmarks { /** * Retrieves one or more bookmarks by their IDs. * @param idOrIdList - A single bookmark ID or an array of bookmark IDs * @returns A promise that resolves to an array of bookmark tree nodes * @throws {Error} If any of the bookmarks are not found or if there's an error */ static async get(idOrIdList) { return new Promise((resolve, reject) => { chrome.bookmarks.get(Array.isArray(idOrIdList) ? idOrIdList : [idOrIdList], (results) => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(results); } }); }); } /** * Retrieves the children of a bookmark folder. * @param id - The ID of the bookmark folder * @returns A promise that resolves to an array of bookmark tree nodes representing the children * @throws {Error} If the folder is not found or if there's an error */ static async getChildren(id) { return new Promise((resolve, reject) => { chrome.bookmarks.getChildren(id, (results) => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(results); } }); }); } /** * Retrieves the entire bookmark tree. * @returns A promise that resolves to an array of bookmark tree nodes representing the root * @throws {Error} If there's an error retrieving the bookmark tree */ static async getTree() { return new Promise((resolve, reject) => { chrome.bookmarks.getTree((results) => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(results); } }); }); } /** * Searches for bookmarks matching the given query. * @param query - A string to search for in bookmark titles and URLs, or a BookmarkSearchQuery object * @returns A promise that resolves to an array of matching bookmark tree nodes * @throws {Error} If there's an error performing the search */ static async search(query) { return new Promise((resolve, reject) => { if (typeof query === 'string') { chrome.bookmarks.search(query, (results) => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(results); } }); } else { chrome.bookmarks.search(query, (results) => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(results); } }); } }); } /** * Creates a new bookmark or folder. * @param bookmark - The bookmark creation arguments including title, URL (optional), and parent ID * @returns A promise that resolves to the newly created bookmark tree node * @throws {Error} If there's an error creating the bookmark */ static async create(bookmark) { return new Promise((resolve, reject) => { chrome.bookmarks.create(bookmark, (result) => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(result); } }); }); } /** * Moves a bookmark or folder to a different parent folder. * @param id - The ID of the bookmark or folder to move * @param destination - The destination information including parent ID and optional index * @returns A promise that resolves to the moved bookmark tree node * @throws {Error} If there's an error moving the bookmark */ static async move(id, destination) { return new Promise((resolve, reject) => { chrome.bookmarks.move(id, destination, (result) => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(result); } }); }); } /** * Updates the properties of a bookmark or folder. * @param id - The ID of the bookmark or folder to update * @param changes - The changes to apply to the bookmark (title and/or URL) * @returns A promise that resolves to the updated bookmark tree node * @throws {Error} If there's an error updating the bookmark */ static async update(id, changes) { return new Promise((resolve, reject) => { chrome.bookmarks.update(id, changes, (result) => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(result); } }); }); } /** * Removes a bookmark or an empty bookmark folder. * @param id - The ID of the bookmark or folder to remove * @returns A promise that resolves when the bookmark is removed * @throws {Error} If there's an error removing the bookmark or if the folder is not empty */ static async remove(id) { return new Promise((resolve, reject) => { chrome.bookmarks.remove(id, () => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(); } }); }); } /** * Removes a bookmark folder and all its contents recursively. * @param id - The ID of the bookmark folder to remove * @returns A promise that resolves when the folder and its contents are removed * @throws {Error} If there's an error removing the folder or its contents */ static async removeTree(id) { return new Promise((resolve, reject) => { chrome.bookmarks.removeTree(id, () => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(); } }); }); } /** * Adds a listener for bookmark creation events. * @param callback - Function called when a bookmark is created * @returns A function that removes the listener when called */ static addCreatedListener(callback) { chrome.bookmarks.onCreated.addListener(callback); return () => chrome.bookmarks.onCreated.removeListener(callback); } /** * Adds a listener for bookmark removal events. * @param callback - Function called when a bookmark is removed * @returns A function that removes the listener when called */ static addRemovedListener(callback) { chrome.bookmarks.onRemoved.addListener(callback); return () => chrome.bookmarks.onRemoved.removeListener(callback); } /** * Adds a listener for bookmark change events. * @param callback - Function called when a bookmark is modified * @returns A function that removes the listener when called */ static addChangedListener(callback) { chrome.bookmarks.onChanged.addListener(callback); return () => chrome.bookmarks.onChanged.removeListener(callback); } /** * Adds a listener for bookmark move events. * @param callback - Function called when a bookmark is moved to a different location * @returns A function that removes the listener when called */ static addMovedListener(callback) { chrome.bookmarks.onMoved.addListener(callback); return () => chrome.bookmarks.onMoved.removeListener(callback); } } exports.Bookmarks = Bookmarks;