-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
82 lines (74 loc) · 2.5 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
let isPanicModeActive = false;
let originalWindowState = null;
let originalWindowId = null;
let safeWindowId = null;
let safeWebsite = "";
// Function to update the safe website URL
function updateSafeWebsite(url) {
safeWebsite = url;
// Save the safe website URL to localStorage
localStorage.setItem('safeWebsite', url);
}
// Function to retrieve the safe website URL from localStorage
function retrieveSafeWebsite() {
const url = localStorage.getItem('safeWebsite');
if (url) {
safeWebsite = url;
}
}
// Function to toggle panic mode
function togglePanicMode() {
if (isPanicModeActive) {
// Close the safe window and restore the original window state
if (safeWindowId) {
chrome.windows.remove(safeWindowId, function () {
safeWindowId = null;
if (originalWindowState === "minimized") {
chrome.windows.update(originalWindowId, { state: "minimized" });
} else {
chrome.windows.update(originalWindowId, { state: originalWindowState });
}
});
}
isPanicModeActive = false;
} else {
// Minimize or save the original window state and open the safe website in a new window
chrome.windows.getCurrent(function (currentWindow) {
originalWindowId = currentWindow.id;
originalWindowState = currentWindow.state;
if (originalWindowState === "minimized") {
chrome.windows.update(originalWindowId, { state: "minimized" });
} else {
chrome.windows.update(originalWindowId, { state: "normal" });
}
chrome.windows.create({ url: safeWebsite, state: "maximized" }, function (newWindow) {
safeWindowId = newWindow.id;
});
});
isPanicModeActive = true;
}
}
// Listen for the "toggle-panic" command
chrome.commands.onCommand.addListener(function (command) {
if (command === "toggle-panic") {
togglePanicMode();
}
});
// Listen for safe website URL updates from options.js
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.action === 'safeWebsiteUpdated') {
// Update the safe website URL
updateSafeWebsite(message.url);
console.log('Safe website URL updated:', safeWebsite);
}
});
// Listen for extension installation or update
chrome.runtime.onInstalled.addListener(function () {
retrieveSafeWebsite();
if (!safeWebsite) {
chrome.tabs.create({ url: 'options.html' });
alert('Please set a safe website URL in the extension options.');
}
});
// Log the safe website URL
console.log('Safe website URL:', safeWebsite);