Skip to content

Commit db10efc

Browse files
committed
search with filters + better ux (compact)
1 parent e9cc05e commit db10efc

7 files changed

Lines changed: 1262 additions & 88 deletions

File tree

gmail-extension/background.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// Background script for Gmail Unread Counter extension
22

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+
315
// Check for unread emails and update badge
416
function checkUnreadEmails() {
517
chrome.storage.local.get(['token'], function(result) {
@@ -51,9 +63,50 @@ function updateBadge(text) {
5163

5264

5365

54-
// Listen for messages from popup
66+
// Listen for messages from content script
5567
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') {
57110
checkUnreadEmails();
58111
sendResponse({ success: true });
59112
} else if (message.action === 'openGmail') {
@@ -81,10 +134,6 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
81134
}
82135
});
83136
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 });
88137
}
89138
return true; // Required for async sendResponse
90139
});

0 commit comments

Comments
 (0)