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
+59 -2
View File
@@ -64,6 +64,8 @@ chrome.idle.onStateChanged.addListener(async (newState) => {
console.log("[PassKeeper] System", newState, "— locking vault.");
// session.clear() also removes vault_items_cs (moved from local to session storage).
await chrome.storage.session.clear();
// Clear health status so stale warnings don't persist after lock.
await chrome.storage.local.remove(HEALTH_STATUS_KEY);
const tabs = await chrome.tabs.query({});
tabs.forEach((tab) => {
if (tab.id) chrome.action.setBadgeText({ text: "", tabId: tab.id });
@@ -73,6 +75,40 @@ chrome.idle.onStateChanged.addListener(async (newState) => {
// ── Badge helpers ────────────────────────────────────────────────────────────
const HEALTH_STATUS_KEY = 'health_status';
/**
* Read the stored health status and apply a global warning badge on tabs that
* currently have no match-count badge. Called after vault updates, after
* clearing the pending-save badge, and when the health status changes.
* Priority: pending-save "!" > per-tab match count (blue) > health warning (amber).
*/
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;
// Don't overwrite the per-tab match-count badge.
const current = await chrome.action.getBadgeText({ tabId: tab.id }).catch(() => '');
if (current && current !== '' && current !== '⚠') continue;
if (hasIssues) {
const label = health.breached > 0 ? '⚠' : '⚠';
const color = health.breached > 0 ? '#c0392b' : '#d97706';
chrome.action.setBadgeText({ text: label, tabId: tab.id });
chrome.action.setBadgeBackgroundColor({ color, tabId: tab.id });
} else {
chrome.action.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");
@@ -107,7 +143,17 @@ async function updateBadgeForTab(tabId, url) {
chrome.action.setBadgeText({ text: String(matches.length), tabId });
chrome.action.setBadgeBackgroundColor({ color: "#1a73e8", tabId });
} else {
chrome.action.setBadgeText({ text: "", tabId });
// No match for this tab — show health warning badge if applicable.
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.action.setBadgeText({ text: "⚠", tabId });
chrome.action.setBadgeBackgroundColor({ color, tabId });
} else {
chrome.action.setBadgeText({ text: "", tabId });
}
}
} catch {
/* tab may have closed */
@@ -213,9 +259,20 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
return false;
}
// Popup reports vault health check results → store and update badge.
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;
}
// Clear the pending-save badge once the popup signals it has handled the prompt.
// Re-apply the health warning badge afterwards if applicable.
if (msg.type === "CLEAR_SAVE_BADGE") {
chrome.action.setBadgeText({ text: "" });
applyHealthBadge();
sendResponse({ ok: true });
return false;
}
@@ -270,4 +327,4 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
}
return false;
});
});