-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
124 lines (106 loc) · 2.95 KB
/
background.js
File metadata and controls
124 lines (106 loc) · 2.95 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
// Background Service Worker for Content Masking Extension
console.log("Background script loaded");
// Handle extension installation
chrome.runtime.onInstalled.addListener((details) => {
console.log("Extension installed:", details);
if (details.reason === "install") {
// Set default settings on first install
const defaultSettings = {
enableMasking: false,
maskEmails: true,
maskPhones: true,
maskCreditCards: true,
maskSSN: true,
customRules: [],
};
chrome.storage.sync.set(defaultSettings, () => {
console.log("Default settings configured:", defaultSettings);
});
// Open welcome page
chrome.tabs.create({
url: chrome.runtime.getURL("welcome.html"),
});
}
});
// Handle keyboard shortcuts
chrome.commands.onCommand.addListener((command) => {
console.log("Keyboard command received:", command);
if (command === "toggle-masking") {
toggleMaskingViaShortcut();
} else if (command === "clear-all-masks") {
clearAllMasksViaShortcut();
}
});
// Handle messages from content scripts and popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Background received message:", request);
switch (request.action) {
case "getMaskingStatus":
getMaskingStatus(sendResponse);
return true; // Indicates async response
case "toggleMasking":
toggleMasking(request.enabled, sendResponse);
return true;
case "updateBadge":
updateBadge(request.count, sender.tab.id);
break;
default:
console.log("Unknown message action:", request.action);
}
});
// Helper functions
function getMaskingStatus(sendResponse) {
chrome.storage.sync.get(
[
"enableMasking",
"maskEmails",
"maskPhones",
"maskCreditCards",
"maskSSN",
"customRules",
],
(settings) => {
sendResponse({
success: true,
enabled: settings.enableMasking,
settings: settings,
});
}
);
}
function toggleMasking(enabled, sendResponse) {
chrome.storage.sync.set({ enableMasking: enabled }, () => {
sendResponse({
success: true,
enabled: enabled,
});
});
}
// Keyboard shortcut handlers
function toggleMaskingViaShortcut() {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, { action: "toggleMaskingMode" });
}
});
}
function clearAllMasksViaShortcut() {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, { action: "clearAllMasks" });
}
});
}
// Badge counter update
function updateBadge(count, tabId) {
const badgeText = count > 0 ? count.toString() : "";
const badgeColor = count > 0 ? "#2563eb" : "#d1d5db";
chrome.action.setBadgeText({
text: badgeText,
tabId: tabId,
});
chrome.action.setBadgeBackgroundColor({
color: badgeColor,
tabId: tabId,
});
}