-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
184 lines (169 loc) · 6.14 KB
/
popup.js
File metadata and controls
184 lines (169 loc) · 6.14 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
document.addEventListener("DOMContentLoaded", () => {
const toggleSwitch = document.getElementById("toggleSwitch");
const statusIndicator = document.getElementById("statusIndicator");
const instructionsCard = document.getElementById("instructionsCard");
const messageToast = document.getElementById("messageToast");
const helpBtn = document.getElementById("helpBtn");
const testBtn = document.getElementById("testBtn");
const maskCountElement = document.getElementById("maskCount");
const currentStyleElement = document.getElementById("currentStyle");
const blurStyleBtn = document.getElementById("blurStyle");
const solidStyleBtn = document.getElementById("solidStyle");
const maskStyleSection = document.getElementById("maskStyleSection");
const statsSection = document.getElementById("statsSection");
let currentMaskStyle = "blur";
// Function to show toast messages
function showToast(message, type = "success") {
messageToast.textContent = message;
messageToast.className = `message-toast show ${
type === "error" ? "error" : ""
}`;
setTimeout(() => {
messageToast.classList.remove("show");
}, 3000);
}
// Function to update UI state
function updateUIState(isActive, maskCount = 0, maskStyle = "blur") {
if (isActive) {
toggleSwitch.classList.add("active");
statusIndicator.textContent = "Active";
statusIndicator.className = "status-indicator active";
instructionsCard.classList.add("active");
maskStyleSection.style.display = "block";
statsSection.style.display = "block";
} else {
toggleSwitch.classList.remove("active");
statusIndicator.textContent = "Inactive";
statusIndicator.className = "status-indicator inactive";
instructionsCard.classList.remove("active");
maskStyleSection.style.display = "none";
statsSection.style.display = "none";
}
// Update mask count
maskCountElement.textContent = maskCount;
// Update current style
currentMaskStyle = maskStyle;
currentStyleElement.textContent =
maskStyle.charAt(0).toUpperCase() + maskStyle.slice(1);
updateStyleButtons();
}
// Function to update style buttons
function updateStyleButtons() {
blurStyleBtn.classList.toggle("active", currentMaskStyle === "blur");
solidStyleBtn.classList.toggle("active", currentMaskStyle === "solid");
}
// Get the initial state from content script (if active on page load)
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(
tabs[0].id,
{ type: "GET_MASKING_STATE" },
(response) => {
if (chrome.runtime.lastError) {
// This error typically means the content script isn't loaded yet on the current tab,
// or the tab is a special page (e.g., chrome://extensions) where content scripts don't run.
console.warn(
"Could not get masking state from content script:",
chrome.runtime.lastError.message
);
updateUIState(false);
} else if (response && response.isMaskingActive) {
updateUIState(
response.isMaskingActive,
response.maskCount || 0,
response.maskStyle || "blur"
);
} else {
updateUIState(false);
}
}
);
}
});
// Toggle switch click handler
toggleSwitch.addEventListener("click", () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
const tabId = tabs[0].id;
// Send a message to the content script in the active tab
chrome.tabs.sendMessage(
tabId,
{ type: "TOGGLE_MASKING" },
(response) => {
if (chrome.runtime.lastError) {
showToast(
"Cannot activate masking on this page (e.g., Chrome internal pages).",
"error"
);
console.error(
"Error sending message to content script:",
chrome.runtime.lastError.message
);
return;
}
if (response && response.isMaskingActive !== undefined) {
updateUIState(
response.isMaskingActive,
response.maskCount || 0,
response.maskStyle || "blur"
);
if (response.isMaskingActive) {
showToast(
"Masking mode activated! Hold Ctrl/⌘ + Click elements to mask them."
);
} else {
showToast("Masking mode deactivated. All masks cleared.");
}
} else {
showToast("Failed to toggle masking mode.", "error");
}
}
);
} else {
showToast("No active tab found.", "error");
}
});
});
// Help button handler
helpBtn.addEventListener("click", () => {
chrome.tabs.create({ url: chrome.runtime.getURL("welcome.html") });
window.close();
});
// Test page button handler
testBtn.addEventListener("click", () => {
chrome.tabs.create({ url: chrome.runtime.getURL("test-page.html") });
window.close();
});
// Style button handlers
function handleStyleChange(newStyle) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(
tabs[0].id,
{ action: "toggleMaskStyle" },
(response) => {
if (!chrome.runtime.lastError) {
currentMaskStyle = newStyle;
updateStyleButtons();
currentStyleElement.textContent =
newStyle.charAt(0).toUpperCase() + newStyle.slice(1);
showToast(`Mask style changed to ${newStyle}`);
}
}
);
}
});
}
blurStyleBtn.addEventListener("click", () => {
if (currentMaskStyle !== "blur") {
handleStyleChange("blur");
}
});
solidStyleBtn.addEventListener("click", () => {
if (currentMaskStyle !== "solid") {
handleStyleChange("solid");
}
});
// Initialize style buttons
updateStyleButtons();
});