04/20/2026 enhance the extension 2
This commit is contained in:
+29
-7
@@ -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' });
|
||||
|
||||
Reference in New Issue
Block a user