// Background script for the extension // Create context menu items when the extension is installed browser.runtime.onInstalled.addListener(() => { // Parent menu item browser.contextMenus.create({ id: "my-extension-menu", title: "My Firefox Extension", contexts: ["all"] }); // Sub-menu items browser.contextMenus.create({ id: "inject-script", parentId: "my-extension-menu", title: "Inject Script", contexts: ["all"] }); browser.contextMenus.create({ id: "save-selection", parentId: "my-extension-menu", title: "Save Selection", contexts: ["selection"] }); browser.contextMenus.create({ id: "navigate-to-homepage", parentId: "my-extension-menu", title: "Go to Homepage", contexts: ["all"] }); // Separator browser.contextMenus.create({ id: "separator-1", parentId: "my-extension-menu", type: "separator", contexts: ["all"] }); browser.contextMenus.create({ id: "custom-action", parentId: "my-extension-menu", title: "Custom Action", contexts: ["link", "image"] }); }); // Context menu click handler browser.contextMenus.onClicked.addListener((info, tab) => { switch (info.menuItemId) { case "inject-script": browser.tabs.sendMessage(tab.id, { command: 'injectCode' }) .catch(error => { // If content script isn't loaded yet, inject it first browser.tabs.executeScript(tab.id, { file: '/content_scripts/content.js' }) .then(() => { browser.tabs.sendMessage(tab.id, { command: 'injectCode' }); }); }); break; case "save-selection": if (info.selectionText) { browser.storage.local.set({ 'lastSelection': info.selectionText, 'selectionTime': Date.now() }).then(() => { // Optional: Show a notification browser.notifications.create({ type: "basic", iconUrl: browser.runtime.getURL("icons/icon-48.png"), title: "Selection Saved", message: "Text has been saved to extension storage" }); }); } break; case "navigate-to-homepage": const homepage = "https://example.com"; browser.tabs.update(tab.id, { url: homepage }); break; case "custom-action": // Handle different context types if (info.linkUrl) { // Custom action for links console.log("Link URL:", info.linkUrl); // For example, copy link to clipboard or open in new tab with modifications } else if (info.srcUrl) { // Custom action for images console.log("Image URL:", info.srcUrl); // For example, save image or process it } break; } }); // Function to navigate a tab to a new URL function navigateTab(tabId, url) { return browser.tabs.update(tabId, { url: url }); } // Listen for messages from popup or content scripts browser.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.command === 'navigate') { if (message.tabId && message.url) { navigateTab(message.tabId, message.url) .then(() => sendResponse({ status: 'success' })) .catch(error => sendResponse({ status: 'error', message: error.message })); return true; // Required for async sendResponse } } }); // Listen for tab updates to potentially inject scripts on certain URLs browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { if (changeInfo.status === 'complete' && tab.url) { // Check if the URL matches your patterns and inject scripts as needed const urlPatterns = [ /example\.com/, /another-site\.org/ // Add more URL patterns as needed ]; const shouldInject = urlPatterns.some(pattern => pattern.test(tab.url)); if (shouldInject) { browser.tabs.executeScript(tabId, { file: '/content_scripts/content.js' }) .catch(error => console.error('Error injecting script:', error)); } } });