// Nano Banana Cookie Sync - Background Service Worker
// Monitors Google cookies and syncs to localhost MCP server
const SYNC_URL = 'http://localhost:8765/sync-cookie';
const TARGET_COOKIES = ['__Secure-1PSID', '__Secure-1PSIDTS'];
const TARGET_DOMAIN = '.google.com';
// Track last synced values to avoid duplicate requests
let lastSyncedValues = {};
// Sync cookie to localhost server
async function syncCookie(name, value) {
// Skip if value hasn't changed
if (lastSyncedValues[name] === value) {
return;
}
try {
const response = await fetch(SYNC_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: name,
value: value,
timestamp: Date.now()
})
});
if (response.ok) {
lastSyncedValues[name] = value;
console.log(`[Nano Banana] Synced ${name} successfully`);
// Update badge to show success
chrome.action.setBadgeText({ text: '✓' });
chrome.action.setBadgeBackgroundColor({ color: '#4CAF50' });
setTimeout(() => chrome.action.setBadgeText({ text: '' }), 2000);
} else {
console.error(`[Nano Banana] Failed to sync ${name}: ${response.status}`);
showError();
}
} catch (error) {
// Server might not be running - that's OK
console.log(`[Nano Banana] Server not available: ${error.message}`);
}
}
function showError() {
chrome.action.setBadgeText({ text: '!' });
chrome.action.setBadgeBackgroundColor({ color: '#F44336' });
setTimeout(() => chrome.action.setBadgeText({ text: '' }), 3000);
}
// Listen for cookie changes
chrome.cookies.onChanged.addListener((changeInfo) => {
const cookie = changeInfo.cookie;
// Only process target cookies from google.com
if (!cookie.domain.includes('google.com')) {
return;
}
if (!TARGET_COOKIES.includes(cookie.name)) {
return;
}
// Only sync when cookie is set (not removed)
if (changeInfo.removed) {
console.log(`[Nano Banana] Cookie ${cookie.name} was removed`);
return;
}
console.log(`[Nano Banana] Cookie changed: ${cookie.name}`);
syncCookie(cookie.name, cookie.value);
});
// Sync all cookies on extension startup/install
async function syncAllCookies() {
console.log('[Nano Banana] Syncing all cookies...');
for (const cookieName of TARGET_COOKIES) {
try {
const cookie = await chrome.cookies.get({
url: 'https://www.google.com',
name: cookieName
});
if (cookie) {
await syncCookie(cookie.name, cookie.value);
}
} catch (error) {
console.error(`[Nano Banana] Error getting ${cookieName}:`, error);
}
}
}
// Initial sync on startup
chrome.runtime.onStartup.addListener(syncAllCookies);
chrome.runtime.onInstalled.addListener(syncAllCookies);
// Allow manual sync from popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'syncNow') {
syncAllCookies().then(() => {
sendResponse({ success: true });
}).catch((error) => {
sendResponse({ success: false, error: error.message });
});
return true; // Keep channel open for async response
}
if (message.action === 'getStatus') {
sendResponse({
lastSynced: lastSyncedValues,
targetCookies: TARGET_COOKIES
});
return true;
}
});
console.log('[Nano Banana] Cookie sync extension loaded');