05/18 Enhanced codes and functionalities 9 (extension)

This commit is contained in:
2026-05-18 21:13:43 -04:00
parent e8b386b2c8
commit 304881a6c2
3 changed files with 212 additions and 5 deletions
+46 -2
View File
@@ -66,6 +66,7 @@ async function resetIdleTimer() {
console.log('[PassKeeper] Idle lock triggered after', seconds, 's');
await chrome.storage.session.clear();
await chrome.storage.local.remove('vault_items_cs');
await chrome.storage.local.remove(HEALTH_STATUS_KEY);
const tabs = await chrome.tabs.query({});
tabs.forEach((tab) => {
if (tab.id) chrome.browserAction.setBadgeText({ text: '', tabId: tab.id });
@@ -77,6 +78,33 @@ resetIdleTimer();
// ── Badge helpers ─────────────────────────────────────────────────────────────
const HEALTH_STATUS_KEY = 'health_status';
async function applyHealthBadge() {
try {
const stored = await chrome.storage.local.get(HEALTH_STATUS_KEY);
const health = stored[HEALTH_STATUS_KEY] || { breached: 0, weak: 0, reused: 0 };
const hasIssues = (health.breached || 0) + (health.weak || 0) + (health.reused || 0) > 0;
const tabs = await chrome.tabs.query({});
for (const tab of tabs) {
if (!tab.url?.startsWith('http')) continue;
const current = await new Promise((res) =>
chrome.browserAction.getBadgeText({ tabId: tab.id }, res)
).catch(() => '');
if (current && current !== '' && current !== '⚠') continue;
if (hasIssues) {
const color = (health.breached || 0) > 0 ? '#c0392b' : '#d97706';
chrome.browserAction.setBadgeText({ text: '⚠', tabId: tab.id });
chrome.browserAction.setBadgeBackgroundColor({ color, tabId: tab.id });
} else {
chrome.browserAction.setBadgeText({ text: '', tabId: tab.id });
}
}
} catch (err) {
console.warn('[PassKeeper] applyHealthBadge error:', err);
}
}
async function updateBadgeForTab(tabId, url) {
try {
const { vault_items } = await chrome.storage.session.get('vault_items');
@@ -99,7 +127,16 @@ async function updateBadgeForTab(tabId, url) {
chrome.browserAction.setBadgeText({ text: String(matches.length), tabId });
chrome.browserAction.setBadgeBackgroundColor({ color: '#1a73e8', tabId });
} else {
chrome.browserAction.setBadgeText({ text: '', tabId });
const stored = await chrome.storage.local.get(HEALTH_STATUS_KEY);
const health = stored[HEALTH_STATUS_KEY] || {};
const hasIssues = (health.breached || 0) + (health.weak || 0) + (health.reused || 0) > 0;
if (hasIssues) {
const color = (health.breached || 0) > 0 ? '#c0392b' : '#d97706';
chrome.browserAction.setBadgeText({ text: '⚠', tabId });
chrome.browserAction.setBadgeBackgroundColor({ color, tabId });
} else {
chrome.browserAction.setBadgeText({ text: '', tabId });
}
}
} catch { /* tab may have closed */ }
}
@@ -167,8 +204,15 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
chrome.browserAction.setBadgeBackgroundColor({ color: '#c0392b' });
sendResponse({ ok: true }); return false;
}
if (msg.type === 'HEALTH_UPDATE') {
const { breached = 0, weak = 0, reused = 0 } = msg;
chrome.storage.local.set({ [HEALTH_STATUS_KEY]: { breached, weak, reused } });
applyHealthBadge();
sendResponse({ ok: true }); return false;
}
if (msg.type === 'CLEAR_SAVE_BADGE') {
chrome.browserAction.setBadgeText({ text: '' });
applyHealthBadge();
sendResponse({ ok: true }); return false;
}
if (msg.type === 'WEB_SESSION_SYNC') {
@@ -190,4 +234,4 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
sendResponse({ ok: true }); return false;
}
return false;
});
});