background.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Background script for the extension
  2. // Create context menu items when the extension is installed
  3. browser.runtime.onInstalled.addListener(() => {
  4. // Parent menu item
  5. browser.contextMenus.create({
  6. id: "my-extension-menu",
  7. title: "My Firefox Extension",
  8. contexts: ["all"]
  9. });
  10. // Sub-menu items
  11. browser.contextMenus.create({
  12. id: "inject-script",
  13. parentId: "my-extension-menu",
  14. title: "Inject Script",
  15. contexts: ["all"]
  16. });
  17. browser.contextMenus.create({
  18. id: "save-selection",
  19. parentId: "my-extension-menu",
  20. title: "Save Selection",
  21. contexts: ["selection"]
  22. });
  23. browser.contextMenus.create({
  24. id: "navigate-to-homepage",
  25. parentId: "my-extension-menu",
  26. title: "Go to Homepage",
  27. contexts: ["all"]
  28. });
  29. // Separator
  30. browser.contextMenus.create({
  31. id: "separator-1",
  32. parentId: "my-extension-menu",
  33. type: "separator",
  34. contexts: ["all"]
  35. });
  36. browser.contextMenus.create({
  37. id: "custom-action",
  38. parentId: "my-extension-menu",
  39. title: "Custom Action",
  40. contexts: ["link", "image"]
  41. });
  42. });
  43. // Context menu click handler
  44. browser.contextMenus.onClicked.addListener((info, tab) => {
  45. switch (info.menuItemId) {
  46. case "inject-script":
  47. browser.tabs.sendMessage(tab.id, { command: 'injectCode' })
  48. .catch(error => {
  49. // If content script isn't loaded yet, inject it first
  50. browser.tabs.executeScript(tab.id, { file: '/content_scripts/content.js' })
  51. .then(() => {
  52. browser.tabs.sendMessage(tab.id, { command: 'injectCode' });
  53. });
  54. });
  55. break;
  56. case "save-selection":
  57. if (info.selectionText) {
  58. browser.storage.local.set({
  59. 'lastSelection': info.selectionText,
  60. 'selectionTime': Date.now()
  61. }).then(() => {
  62. // Optional: Show a notification
  63. browser.notifications.create({
  64. type: "basic",
  65. iconUrl: browser.runtime.getURL("icons/icon-48.png"),
  66. title: "Selection Saved",
  67. message: "Text has been saved to extension storage"
  68. });
  69. });
  70. }
  71. break;
  72. case "navigate-to-homepage":
  73. const homepage = "https://example.com";
  74. browser.tabs.update(tab.id, { url: homepage });
  75. break;
  76. case "custom-action":
  77. // Handle different context types
  78. if (info.linkUrl) {
  79. // Custom action for links
  80. console.log("Link URL:", info.linkUrl);
  81. // For example, copy link to clipboard or open in new tab with modifications
  82. } else if (info.srcUrl) {
  83. // Custom action for images
  84. console.log("Image URL:", info.srcUrl);
  85. // For example, save image or process it
  86. }
  87. break;
  88. }
  89. });
  90. // Function to navigate a tab to a new URL
  91. function navigateTab(tabId, url) {
  92. return browser.tabs.update(tabId, { url: url });
  93. }
  94. // Listen for messages from popup or content scripts
  95. browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
  96. if (message.command === 'navigate') {
  97. if (message.tabId && message.url) {
  98. navigateTab(message.tabId, message.url)
  99. .then(() => sendResponse({ status: 'success' }))
  100. .catch(error => sendResponse({ status: 'error', message: error.message }));
  101. return true; // Required for async sendResponse
  102. }
  103. }
  104. });
  105. // Listen for tab updates to potentially inject scripts on certain URLs
  106. browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  107. if (changeInfo.status === 'complete' && tab.url) {
  108. // Check if the URL matches your patterns and inject scripts as needed
  109. const urlPatterns = [
  110. /example\.com/,
  111. /another-site\.org/
  112. // Add more URL patterns as needed
  113. ];
  114. const shouldInject = urlPatterns.some(pattern => pattern.test(tab.url));
  115. if (shouldInject) {
  116. browser.tabs.executeScript(tabId, { file: '/content_scripts/content.js' })
  117. .catch(error => console.error('Error injecting script:', error));
  118. }
  119. }
  120. });