-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
83 lines (71 loc) 路 2.23 KB
/
Copy pathbackground.js
File metadata and controls
83 lines (71 loc) 路 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Background Service Worker for Cookie Manager Extension
* Handles extension lifecycle and background tasks
*/
// Extension installation/update handler
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
console.log('Cookie Manager Extension installed');
// Initialize default settings
chrome.storage.local.set({
theme: 'light',
autoLockTime: 300000, // 5 minutes in milliseconds
requirePassword: false,
lastActivity: Date.now()
});
} else if (details.reason === 'update') {
console.log('Cookie Manager Extension updated');
}
});
// Track activity for auto-lock feature
let lockTimeout = null;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'resetLockTimer') {
resetLockTimer();
sendResponse({ success: true });
} else if (request.action === 'checkLockStatus') {
checkLockStatus().then(sendResponse);
return true; // Indicates async response
}
});
/**
* Reset the auto-lock timer
*/
function resetLockTimer() {
// Clear existing timeout
if (lockTimeout) {
clearTimeout(lockTimeout);
}
// Update last activity time
chrome.storage.local.set({ lastActivity: Date.now() });
// Set new timeout
chrome.storage.local.get(['autoLockTime', 'requirePassword'], (data) => {
if (data.requirePassword && data.autoLockTime > 0) {
lockTimeout = setTimeout(() => {
// Lock the extension
chrome.storage.local.set({ isLocked: true });
}, data.autoLockTime);
}
});
}
/**
* Check if extension should be locked
*/
async function checkLockStatus() {
return new Promise((resolve) => {
chrome.storage.local.get(['requirePassword', 'isLocked', 'lastActivity', 'autoLockTime'], (data) => {
if (!data.requirePassword) {
resolve({ shouldLock: false });
return;
}
const timeSinceActivity = Date.now() - (data.lastActivity || 0);
const shouldLock = data.isLocked || (timeSinceActivity > (data.autoLockTime || 300000));
if (shouldLock) {
chrome.storage.local.set({ isLocked: true });
}
resolve({ shouldLock });
});
});
}
// Initialize lock timer on startup
resetLockTimer();