-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
66 lines (60 loc) · 1.93 KB
/
background.js
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
let currentDomain = '';
let inactivityTimeout;
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({ profilePassword: 'Admin' });
chrome.storage.local.set({ isLocked: true });
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'unlockProfile') {
chrome.storage.local.set({ isLocked: false });
resetInactivityTimeout();
sendResponse({ status: 'unlocked' });
} else if (request.action === 'checkLockStatus') {
chrome.storage.local.get('isLocked', function(data) {
sendResponse({ isLocked: data.isLocked });
});
return true;
}
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
const newDomain = new URL(tab.url).hostname;
if (newDomain !== currentDomain) {
currentDomain = newDomain;
chrome.storage.local.get('isLocked', function(data) {
if (data.isLocked) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['content.js']
});
}
});
}
}
});
chrome.tabs.onActivated.addListener(activeInfo => {
chrome.tabs.get(activeInfo.tabId, tab => {
if (tab.url) {
const newDomain = new URL(tab.url).hostname;
if (newDomain !== currentDomain) {
currentDomain = newDomain;
chrome.storage.local.get('isLocked', function(data) {
if (data.isLocked) {
chrome.scripting.executeScript({
target: { tabId: activeInfo.tabId },
files: ['content.js']
});
}
});
}
}
});
});
function resetInactivityTimeout() {
clearTimeout(inactivityTimeout);
inactivityTimeout = setTimeout(() => {
chrome.storage.local.set({ isLocked: true });
}, 300000);
}
chrome.tabs.onHighlighted.addListener(resetInactivityTimeout);
chrome.windows.onFocusChanged.addListener(resetInactivityTimeout);