|
1 | 1 | // Background script for Gmail Unread Counter extension |
2 | 2 |
|
| 3 | +// Handle extension icon click - open Gmail |
| 4 | +chrome.action.onClicked.addListener((tab) => { |
| 5 | + // Check if current tab is already Gmail |
| 6 | + if (tab.url && tab.url.includes('mail.google.com')) { |
| 7 | + // Already on Gmail, just reload to ensure content script is injected |
| 8 | + chrome.tabs.reload(tab.id); |
| 9 | + } else { |
| 10 | + // Open Gmail in a new tab |
| 11 | + chrome.tabs.create({ url: 'https://mail.google.com/mail/u/0/#inbox' }); |
| 12 | + } |
| 13 | +}); |
| 14 | + |
3 | 15 | // Check for unread emails and update badge |
4 | 16 | function checkUnreadEmails() { |
5 | 17 | chrome.storage.local.get(['token'], function(result) { |
@@ -51,9 +63,50 @@ function updateBadge(text) { |
51 | 63 |
|
52 | 64 |
|
53 | 65 |
|
54 | | -// Listen for messages from popup |
| 66 | +// Listen for messages from content script |
55 | 67 | chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { |
56 | | - if (message.action === 'refreshUnreadCount') { |
| 68 | + if (message.action === 'authenticate') { |
| 69 | + // Handle authentication request from content script |
| 70 | + chrome.identity.getAuthToken({ interactive: message.interactive || false }, function(token) { |
| 71 | + const lastError = chrome.runtime.lastError; |
| 72 | + |
| 73 | + if (lastError) { |
| 74 | + console.error('Chrome identity error:', lastError); |
| 75 | + sendResponse({ |
| 76 | + success: false, |
| 77 | + error: lastError.message, |
| 78 | + extensionId: chrome.runtime.id |
| 79 | + }); |
| 80 | + } else if (token) { |
| 81 | + console.log('Successfully obtained auth token'); |
| 82 | + chrome.storage.local.set({ token: token }, function() { |
| 83 | + sendResponse({ success: true, token: token }); |
| 84 | + }); |
| 85 | + } else { |
| 86 | + console.error('Failed to get auth token'); |
| 87 | + sendResponse({ |
| 88 | + success: false, |
| 89 | + error: 'Failed to get auth token', |
| 90 | + extensionId: chrome.runtime.id |
| 91 | + }); |
| 92 | + } |
| 93 | + }); |
| 94 | + return true; // Required for async sendResponse |
| 95 | + } else if (message.action === 'clearAuth') { |
| 96 | + // Handle logout request from content script |
| 97 | + chrome.identity.clearAllCachedAuthTokens(function() { |
| 98 | + chrome.storage.local.remove(['token', 'userEmail'], function() { |
| 99 | + sendResponse({ success: true }); |
| 100 | + }); |
| 101 | + }); |
| 102 | + return true; |
| 103 | + } else if (message.action === 'removeCachedToken') { |
| 104 | + // Remove a specific cached token |
| 105 | + chrome.identity.removeCachedAuthToken({ token: message.token }, function() { |
| 106 | + sendResponse({ success: true }); |
| 107 | + }); |
| 108 | + return true; |
| 109 | + } else if (message.action === 'refreshUnreadCount') { |
57 | 110 | checkUnreadEmails(); |
58 | 111 | sendResponse({ success: true }); |
59 | 112 | } else if (message.action === 'openGmail') { |
@@ -81,10 +134,6 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { |
81 | 134 | } |
82 | 135 | }); |
83 | 136 | sendResponse({ success: true }); |
84 | | - } else if (message.action === 'keepPopupOpen') { |
85 | | - // This is used to keep the popup open when interacting with Gmail |
86 | | - // We'll implement a special handling for this in the popup.js |
87 | | - sendResponse({ success: true }); |
88 | 137 | } |
89 | 138 | return true; // Required for async sendResponse |
90 | 139 | }); |
0 commit comments