04/20/2026 enhance the extension 2

This commit is contained in:
2026-04-20 17:51:01 -04:00
parent df8628e71c
commit 27e1c16851
5 changed files with 1488 additions and 1404 deletions
+29 -7
View File
@@ -10,20 +10,35 @@
// ── Idle lock ─────────────────────────────────────────────────────────────────
// Lock after 10 minutes of system idle or when the screen is locked.
const IDLE_LOCK_SECONDS = 600;
// Default: 10 minutes. User can change via the Account view in the popup.
const DEFAULT_IDLE_LOCK_SECONDS = 600;
const IDLE_TIMEOUT_KEY = 'idle_lock_seconds';
chrome.idle.setDetectionInterval(IDLE_LOCK_SECONDS);
/** Apply the idle detection interval, reading the user's saved preference. */
async function applyIdleInterval() {
const stored = await chrome.storage.local.get(IDLE_TIMEOUT_KEY);
const seconds = stored[IDLE_TIMEOUT_KEY] ?? DEFAULT_IDLE_LOCK_SECONDS;
if (seconds === 0) {
// "Never" — unregister by setting to Chrome's maximum (the API requires a value).
chrome.idle.setDetectionInterval(3600);
} else {
chrome.idle.setDetectionInterval(Math.max(15, seconds));
}
console.log('[PassKeeper] Idle lock interval:', seconds === 0 ? 'never' : seconds + 's');
}
// Apply on every SW startup (SW can be killed and restarted at any time).
applyIdleInterval();
chrome.idle.onStateChanged.addListener(async (newState) => {
if (newState === 'idle' || newState === 'locked') {
// Check if the user set "Never" before locking.
const stored = await chrome.storage.local.get(IDLE_TIMEOUT_KEY);
if ((stored[IDLE_TIMEOUT_KEY] ?? DEFAULT_IDLE_LOCK_SECONDS) === 0) return;
console.log('[PassKeeper] System', newState, '— locking vault.');
// Clear the session (vault key, access token, vault items) so the popup
// requires master password re-entry on next open.
await chrome.storage.session.clear();
// Clear the content-script vault cache so suggestions stop showing.
await chrome.storage.local.remove('vault_items_cs');
// Clear all badge text — vault is now locked.
const tabs = await chrome.tabs.query({});
tabs.forEach(tab => {
if (tab.id) chrome.action.setBadgeText({ text: '', tabId: tab.id });
@@ -88,6 +103,13 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
// Popup updated the idle lock timeout → re-apply immediately.
if (msg.type === 'SET_IDLE_TIMEOUT') {
applyIdleInterval();
sendResponse({ ok: true });
return false;
}
// Content script requests opening the vault tab (from suggestion dropdown).
if (msg.type === 'OPEN_VAULT') {
chrome.tabs.create({ url: 'https://pwkeeper.ngodanguyen.tech/vault' });