diff --git a/api-samples/management/README.md b/api-samples/management/README.md new file mode 100644 index 0000000000..cc39cdb6cd --- /dev/null +++ b/api-samples/management/README.md @@ -0,0 +1,39 @@ +# chrome.management Extension + +The [`chrome.management`](https://developer.chrome.com/docs/extensions/reference/api/management) API provides ways to manage the list of extensions that are installed and running. It is particularly useful for extensions that [override](https://developer.chrome.com/extensions/develop/ui/override-chrome-pages) the built-in New Tab page. + +## Features + +- Lists all installed extensions. +- Displays extension icons, names, and versions. +- Provides an **Uninstall** button for each extension. + + ![Screenshot](images/screenshot.png) + +## Installation + +1. Clone or download this repository. +2. Load the extension in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked). + +## Usage + +1. Click the extension icon to open the extension manager. The manager will list all installed extensions. Each extension entry includes its icon, name, and version. +2. Click the **Uninstall** button next to an extension to remove it. + +## Code Overview + +### `popup.js` + +- Uses `chrome.management.getAll()` to get a list of all installed extensions. +- Creates a list item for each extension, including its icon, name, version, and an **Uninstall** button. +- Sends a message to the extension service worker (`background.js`) to uninstall an extension when the **Uninstall** button is clicked. + +### `background.js` + +- Listens for messages from the popup. +- Handles uninstallation requests by calling `chrome.management.uninstall()` with the extension ID. +- Sends a response back to the popup after uninstalling the extension. + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. diff --git a/api-samples/management/background.js b/api-samples/management/background.js new file mode 100644 index 0000000000..c86ebc96df --- /dev/null +++ b/api-samples/management/background.js @@ -0,0 +1,18 @@ +chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.action === 'uninstall') { + // Uninstall the extension with the given id + chrome.management.uninstall(message.id, (result) => { + if (chrome.runtime.lastError) { + // Handle error during uninstallation + sendResponse({ + success: false, + error: chrome.runtime.lastError.message + }); + } else { + // Show confirmation message when extension is successfully uninstalled + sendResponse({ success: true }); + } + }); + } + return true; // Keep the message channel open for sendResponse +}); diff --git a/api-samples/management/images/sample.png b/api-samples/management/images/sample.png new file mode 100644 index 0000000000..a732e383f1 Binary files /dev/null and b/api-samples/management/images/sample.png differ diff --git a/api-samples/management/images/screenshot.png b/api-samples/management/images/screenshot.png new file mode 100644 index 0000000000..0138088045 Binary files /dev/null and b/api-samples/management/images/screenshot.png differ diff --git a/api-samples/management/manifest.json b/api-samples/management/manifest.json new file mode 100644 index 0000000000..5365a27f50 --- /dev/null +++ b/api-samples/management/manifest.json @@ -0,0 +1,17 @@ +{ + "manifest_version": 3, + "name": "Extension Manager", + "version": "1.0", + "permissions": ["management"], + "background": { + "service_worker": "background.js" + }, + "action": { + "default_popup": "popup.html", + "default_icon": { + "16": "images/sample.png", + "48": "images/sample.png", + "128": "images/sample.png" + } + } +} diff --git a/api-samples/management/popup.html b/api-samples/management/popup.html new file mode 100644 index 0000000000..97511b234a --- /dev/null +++ b/api-samples/management/popup.html @@ -0,0 +1,14 @@ + + +
+