From ef41018ac1c8e42e0c04d2a7e427a01ffc729dcb Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Wed, 5 Feb 2025 11:16:53 +0100 Subject: [PATCH] Add context menu with password generator (#541) --- browser-extensions/chrome/src/background.ts | 53 +++++++++++++++++++++ browser-extensions/chrome/src/manifest.json | 6 ++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/browser-extensions/chrome/src/background.ts b/browser-extensions/chrome/src/background.ts index ef4d9c5c5..3654ca87a 100644 --- a/browser-extensions/chrome/src/background.ts +++ b/browser-extensions/chrome/src/background.ts @@ -2,6 +2,7 @@ import { Vault } from './types/webapi/Vault'; import EncryptionUtility from './utils/EncryptionUtility'; import SqliteClient from './utils/SqliteClient'; import { WebApiService } from './utils/WebApiService'; +import { PasswordGenerator } from './generators/Password/PasswordGenerator'; let vaultState: { derivedKey: string | null; @@ -15,6 +16,58 @@ let vaultState: { vaultRevisionNumber: 0, }; +chrome.runtime.onInstalled.addListener(() => { + chrome.contextMenus.create({ + id: "aliasvault-root", + title: "AliasVault", + contexts: ["all"] + }); + + chrome.contextMenus.create({ + id: "aliasvault-generate-password", + parentId: "aliasvault-root", + title: "Generate Password (copy to clipboard)", + contexts: ["all"] + }); +}); + +chrome.contextMenus.onClicked.addListener((info, tab) => { + if (info.menuItemId === "aliasvault-generate-password") { + // Initialize password generator + const passwordGenerator = new PasswordGenerator(); + const password = passwordGenerator.generateRandomPassword(); + + // Use chrome.scripting to write to clipboard from active tab + if (tab?.id) { + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + func: (generatedPassword) => { + // Write to clipboard + navigator.clipboard.writeText(generatedPassword).then(() => { + // Show notification + const notification = document.createElement('div'); + notification.textContent = 'Password copied to clipboard'; + notification.style.cssText = ` + position: fixed; + top: 20px; + right: 20px; + padding: 16px; + background: #4CAF50; + color: white; + border-radius: 4px; + z-index: 9999; + box-shadow: 0 2px 5px rgba(0,0,0,0.2); + `; + document.body.appendChild(notification); + setTimeout(() => notification.remove(), 3000); + }); + }, + args: [password] + }); + } + } +}); + // Listen for messages from popup chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { switch (message.type) { diff --git a/browser-extensions/chrome/src/manifest.json b/browser-extensions/chrome/src/manifest.json index 36126e7b9..25ac794fd 100644 --- a/browser-extensions/chrome/src/manifest.json +++ b/browser-extensions/chrome/src/manifest.json @@ -7,7 +7,8 @@ "extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';" }, "action": { - "default_popup": "index.html" + "default_popup": "index.html", + "default_title": "AliasVault" }, "icons": { "16": "assets/icons/icon-16.png", @@ -23,7 +24,8 @@ }, "permissions": [ "storage", - "activeTab" + "activeTab", + "contextMenus" ], "content_scripts": [ {