-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
145 lines (119 loc) · 4.02 KB
/
popup.js
File metadata and controls
145 lines (119 loc) · 4.02 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
// popup.js
// Handles popup UI + messaging with background/content scripts.
(async function () {
const btn = document.getElementById("toggleBtn");
const statusText = document.getElementById("statusText");
const fieldCountEl = document.getElementById("fieldCount");
const infoBar = document.getElementById("infoBar");
const pulseRing = document.querySelector(".pulse-ring");
const EYE_OPEN =
`<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/>` +
`<circle cx="12" cy="12" r="3"/>`;
const EYE_SLASH =
`<path d="M17.94 17.94A10.07 10.07 0 0112 20c-7 0-11-8-11-8a18.45 18.45 0 015.06-5.94"/>` +
`<path d="M9.9 4.24A9.12 9.12 0 0112 4c7 0 11 8 11 8a18.5 18.5 0 01-2.16 3.19"/>` +
`<line x1="1" y1="1" x2="23" y2="23"/>`;
async function getActiveTab() {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
return tabs[0];
}
function getState(tabId) {
return new Promise(resolve => {
chrome.runtime.sendMessage({ type: "GET_STATE", tabId }, res => {
resolve(res || { active: false, fieldCount: 0 });
});
});
}
async function ensureInjected(tab) {
try {
const pong = await chrome.tabs
.sendMessage(tab.id, { type: "PING" }, { frameId: 0 })
.catch(() => null);
if (!pong || !pong.pong) {
await chrome.scripting.executeScript({
target: { tabId: tab.id, allFrames: true },
files: ["content.js"]
});
}
return true;
} catch {
return false; // restricted pages like chrome://
}
}
async function toggle(tab) {
const result = await chrome.tabs
.sendMessage(tab.id, { type: "TOGGLE" }, { frameId: 0 })
.catch(() => null);
// Try toggling sub-frames too (best effort)
chrome.webNavigation?.getAllFrames({ tabId: tab.id })
.then(frames => {
frames?.forEach(f => {
if (f.frameId !== 0) {
chrome.tabs
.sendMessage(tab.id, { type: "TOGGLE" }, { frameId: f.frameId })
.catch(() => {});
}
});
})
.catch(() => {});
return result;
}
function updateUI(active, count) {
const icon = btn.querySelector(".btn-icon");
btn.classList.toggle("active", active);
pulseRing.classList.toggle("animating", active);
statusText.classList.toggle("active", active);
statusText.textContent = active
? "Masking active"
: "Click to hide fields";
icon.innerHTML = active ? EYE_OPEN : EYE_SLASH;
if (count != null) {
infoBar.classList.toggle("has-fields", count > 0);
fieldCountEl.textContent =
count === 0
? "No sensitive fields found"
: `${count} sensitive field${count !== 1 ? "s" : ""} detected`;
}
}
// Initial load
const tab = await getActiveTab();
const state = await getState(tab.id);
updateUI(state.active, state.fieldCount);
btn.addEventListener("click", async () => {
const tab = await getActiveTab();
const ok = await ensureInjected(tab);
if (!ok) {
statusText.textContent = "Cannot run on this page";
return;
}
const result = await toggle(tab);
if (!result) {
statusText.textContent = "Communication error. Reload and try again.";
return;
}
chrome.runtime.sendMessage({
type: "SET_STATE",
tabId: tab.id,
active: result.active,
fieldCount: result.fieldCount
});
updateUI(result.active, result.fieldCount);
// Re-check count after a short delay (for lazy-loaded fields)
if (result.active) {
setTimeout(async () => {
const updated = await chrome.tabs
.sendMessage(tab.id, { type: "GET_COUNT" }, { frameId: 0 })
.catch(() => null);
if (updated && updated.fieldCount != null) {
updateUI(result.active, updated.fieldCount);
chrome.runtime.sendMessage({
type: "SET_STATE",
tabId: tab.id,
active: result.active,
fieldCount: updated.fieldCount
});
}
}, 600);
}
});
})();