|
@@ -1,5 +1,103 @@
|
|
|
// Background script for the extension
|
|
// 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 to navigate a tab to a new URL
|
|
|
function navigateTab(tabId, url) {
|
|
function navigateTab(tabId, url) {
|
|
|
return browser.tabs.update(tabId, { url: url });
|
|
return browser.tabs.update(tabId, { url: url });
|